I can not understand how to implement a method relating to the selected text.

Make a description of a class of rectangles with sides parallel to the axes of coordinates. To provide for the possibility of moving rectangles on a plane, resizing, building the smallest rectangle containing two given rectangles, and a rectangle that is a common part (intersection) of two rectangles. The program should contain a menu that allows you to test all methods.

4 answers 4

Yes, the problem is not in a couple of lines. Made the program, checked, works as it should:

#include <iostream> using namespace std; // просто две координаты struct vec { int x, y; }; class box { // левая нижняя и правая верхняя точки vec pos1, pos2; public: // ширина и высота, чтобы вторая точка // была не ниже и не выше первой box (int x, int y, int w, int h) { pos1.x = x; pos1.y = y; pos2.x = x + w; pos2.y = y + h; } box () { // в случае, если не пересекаются, будут нули pos1.x = pos1.y = 0; pos2.x = pos2.y = 0; } // поиск пересечения box cross (box &another) { box result; // ящик, потенциально ниже и левее второго box *low = this; // ящик, потенциально выше и правее первого box *high = &another; if (another.pos1.x < pos1.x) { low = &another; high = this; } // проверки, что ящики пересекаются if (high->pos1.x < low->pos2.x) { if (low->pos1.y <= high->pos1.y) { if (high->pos1.y < low->pos2.y) { // вычисление регулирующих значений result.pos1.x = max(low->pos1.x, high->pos1.x); result.pos1.y = max(low->pos1.y, high->pos1.y); result.pos2.x = min(low->pos2.x, high->pos2.x); result.pos2.y = min(low->pos2.y, high->pos2.y); } } } return result; } // вывод крайних точек ящика void print () { cout << "x1: " << pos1.x << " y1: " << pos1.y << " x2: " << pos2.x << " y2: " << pos2.y << "\n"; } }; int main() { // проверка box a(0, 5, 10, 10), b (-5, 0, 10, 50); cout << "A: "; a.print(); cout << "B: "; b.print(); box c = a.cross(b); cout << "C: "; c.print(); return 0; } 

The result of this code is:

 A: x1: 0 y1: 5 x2: 10 y2: 15 B: x1: -5 y1: 0 x2: 5 y2: 50 C: x1: 0 y1: 5 x2: 5 y2: 15 
  • @KromStern several quick checks were successful. In general, my algorithm should be universal, errors can be in the boundary values. Although the task is not defined what should be in the case of a simple touch with rectangles. - AivanF.
  • @KromStern, thank you :) However, I think that there are few comments. Need to fix it. The university seems to have done a good job of inculcating this habit. - AivanF.
  • No, of course, it will come down for the academic version, but on the whole it turned out - “why is it simple, if it can be difficult?” - Harry

Take the coordinates (consider x, y similar) -

x1_1, x1_2 - coordinates of the left and right side of the first rectangle

x2_1, x2_2 - coordinates of the left and right side of the second rectangle

And we compare max(x1_1,x2_1) and min(x1_2,x2_2) . If the first value exceeds the second - the rectangles do not intersect (well, of course, we must also check for y). If not, then this is the x coordinate of the intersection.

"I think so" (c) Pooh

Update This is how 25 years ago Turbo Vision dealt with this :)

 inline void TRect::intersect( const TRect& r ) { ax = max( ax, rax ); ay = max( ay, ray ); bx = min( bx, rbx ); by = min( by, rby ); } inline void TRect::Union( const TRect& r ) { ax = min( ax, rax ); ay = min( ay, ray ); bx = max( bx, rbx ); by = max( by, rby ); } inline Boolean TRect::contains( const TPoint& p ) const { return Boolean( px >= ax && px < bx && py >= ay && py < by ); } 
  • @KromStern Reread. I perceived and perceived it, especially with regard to the selection, as: "Provide 1. the ability to move rectangles on a plane, 1. resize, 3. construct a smallest rectangle containing two given rectangles, and 4. a rectangle that is a common part (intersection) two rectangles. " If this is not so - write more clearly ... - Harry
  • @KromStern About "write clearer" - understandable :), not to you, but to the author ... - Harry
  • one
    @KromStern Well, when I am simultaneously overpowered by the pedagogical fuse (not to give a ready-made solution) and laziness (to write verbally) - I reply with these hints :) I slightly corrected the answer, look ... - Harry

it seems that it should be like this (coordinates grow from left to right and from top to bottom):

  c.lt.y=max(a.lt.y,b.lt.y); c.lt.x=max(a.lt.x,b.lt.x); c.rb.y=min(a.rb.y,b.rb.y); c.rb.x=min(a.rb.x,b.rb.x); 

    Something like this (from Google, but true on paper):

     struct point { int x,y; } struct rectangle { point lt; //left top point rb; //right bottom } ..... ввод координат ... if(a.rb.y>b.lt.y || a.lt.y<b.rb.y || a.lt.x>b.rb.x || a.rb.x<b.lt.x) { cout<<"Прямоугольники не пересекаются"<<endl; } else { c.lt.y=min(a.lt.y,b.lt.y); c.lt.x=max(a.lt.x,b.lt.x); c.rb.y=max(a.rb.y,b.rb.y); c.rb.x=min(a.rb.x,b.rb.x); } 

    a and b - source rectangles, c - intersection.