Consider following basic structures to represent a rectangle. You can add more functions or members as per your need:
struct Rectangle {
int x;
int y;
int w;// width
int h; // height
};
1. Generate N number of rectangles with different sizes
2. Find the area of all generated rectangles and display them.
3. Sort rectangles based on their area.
4. Implement following method to return true of first rectangle is greater than the 2nd
rectangle:
bool compareRect(Rectangle r1,Rectangle r2)
5. Check which rectangles have the same area and display it.
6. Generate a random line. Find the number of rectangles that this line with.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct Rectangle {
int x;
int y;
int w; // width
int h; // height
int area() { return w*h; }
void print() {
cout << "Rectangle: x = " << x << "; y = " << y
<< "; width = " << w << "; height = " << h;
}
};
// 1. Generate N number of rectangles with different sizes
Rectangle* generateRectangle(int n) {
int x, y, w, h;
Rectangle* res = new Rectangle[n];
for (int i=0; i<n; i++) {
res[i].x = rand() % 101 - 50;
res[i].y = rand() % 101 - 50;
res[i].w = rand() % 100 + 1;
res[i].h = rand() % 100 + 1;
}
return res;
}
void printRectangles(Rectangle rectangles[], int n) {
for(int i=0; i<n; i++) {
rectangles[i].print();
cout << endl;
}
}
// 2. Find the area of all generated rectangles and display them.
void displayAreas(Rectangle rectangles[], int n) {
for (int i=0; i<n; i++) {
cout << "Area of " << i+1 << "-th rectangle is "
<< rectangles[i].area() << endl;
}
}
// 4. Implement following method to return true of first rectangle is greater
// than the 2nd rectangle
bool compareRect(Rectangle r1, Rectangle r2) {
return r1.area() > r2.area();
}
// 3. Sort rectangles based on their area.
void sortRectangles(Rectangle rectangles[], int n) {
for (int i=0; i<n-1; i++) {
int minIdx = i;
for (int j=i+1; j<n; j++) {
if (compareRect(rectangles[minIdx], rectangles[j])) {
minIdx = j;
}
}
if (minIdx != i) {
Rectangle tmp = rectangles[minIdx];
rectangles[minIdx] = rectangles[i];
rectangles[i] = tmp;
}
}
}
// 5. Check which rectangles have the same area and display it.
void rectWithSameArea(Rectangle rectangles[], int n) {
bool hasFound = false;
for (int i=0; i<n-1; i++) {
for (int j=i+1; j<n; j++) {
if (rectangles[i].area() == rectangles[j].area()) {
rectangles[i].print();
cout << " has area " << rectangles[i].area() << endl;
rectangles[j].print();
cout << " has area " << rectangles[j].area() << endl;
hasFound = true;
}
}
}
if (!hasFound) {
cout << "No rectangles with same area" << endl;
}
}
// Line a*x + b*y + c = 0
struct Line {
int a;
int b;
int c;
void print() {
cout << "Line: " << a << "*x + " << b << "*y + " << c << " = 0";
}
};
bool isLineCrossRect(Rectangle rect, Line l) {
int negative=0;
if (l.a*rect.x + l.b*rect.y + l.c < 0)
negative++;
if (l.a*rect.x + l.b*(rect.y+rect.h) + l.c < 0)
negative++;
if (l.a*(rect.x+rect.w) + l.b*rect.y + l.c < 0)
negative++;
if (l.a*(rect.x+rect.w) + l.b*(rect.y+rect.h) + l.c < 0)
negative++;
return negative != 0 && negative != 4;
}
// 6. Generate a random line.
// Find the number of rectangles that this line with.
Line randomLine() {
Line l;
l.a = rand()%41 - 20;
l.b = rand()%41 - 20;
l.c = rand()%41 - 20;
return l;
}
int countRectCroosByLine(Rectangle rectangles[], int n, Line line) {
int count = 0;
for (int i=0; i<n; i++) {
if (isLineCrossRect(rectangles[i], line)) {
count++;
}
}
return count;
}
int main() {
srand(time(NULL));
int n;
cout << "Enter N: ";
cin >> n;
Rectangle *rectangles = generateRectangle(n);
printRectangles(rectangles, n);
cout << endl;
displayAreas(rectangles, n);
cout << endl;
sortRectangles(rectangles, n);
printRectangles(rectangles, n);
cout << endl;
rectWithSameArea(rectangles, n);
cout << endl;
Line line = randomLine();
int count = countRectCroosByLine(rectangles, n, line);
cout << count << " rectangles are crossed by line ";
line.print();
cout << endl;
return 0;
}
Comments
Leave a comment