1

I have an image with two areas. I want to add now rectangles with a fixed size randomly into area2. The coordinate origin of the image is in the top-left corner. I have the cordinates of the area2. This are P1, P2, P3(0, y_max) and P4(x_max, y_max). Does anybody knows how to check, if a rectangle lies in this area? I can try to separate this area into 2 parts, a rectangle (rect_area) and a triangle (trangle_area). For the rect_area I can check with

bool intersects = ((rect_area & rect_random).area() > 0); if the random rect lies inside the area. For the triangle I have found some complicated stuff like here: How to determine if a point is in a 2D triangle?

Does anybody knows an easier way to do that?

enter image description here

HanzDieter
  • 99
  • 1
  • 11

1 Answers1

1

Does the graph you draw represent the general case of the problem?

  • P1.x == 0
  • P2.x == 0
  • q1.x == q2.x
  • q2.y == q3.y

If the above conditions hold, then you can check

  • if q1 is below the line of P1P2 (q1.y > (q1.x*(p2.y-p1.y)/x_max)+p1.y)
  • q2 is abovep2 (q2.y < P2.y)

enter image description here

Stove
  • 116
  • 4