2

I have 2 squares and all positions for rotated square

sq1.x1y1
sq1.x2y2
sq1.x3y3
sq1.x4y4 
sq1.maxX
sq1.minX
sq1.maxY
sq1.minY
sq1.rotatedAngl
sq1.cX
sq1.cY

all positions before rotation

and for second square

sq2.x1y1
sq2.x2y2
sq2.x3y3
sq2.x4y4 
sq2.maxX
sq2.minX
sq2.maxY
sq2.minY
sq2.cX
sq2.cY

This is my squares

enter image description here

After dragging square 1 and stop

enter image description here

After Rotate 45 deg

enter image description here

And after drag square 1

enter image description here

i want stop dragging when the corners square 2 intersect to square 1

2 Answers2

1

You have the coordinates of every corner. You can define segments by two corners. You can get the distance from one corner (square to drag) to the segments of the other square.

The distance formula, and its sign, can be as here

You may compare signs for two parallel sides of a square. If both are "at left" or both are "right" then the point is outside of the square.

Ripi2
  • 7,031
  • 1
  • 17
  • 33
1

A square can be defined as the triple {center1, side, angle} where center = {double center.x, double center.y} is the center of the square, double side is the length of the side of the square, and finally double angle is the angle to which the square is rotated from the horizontal direction.

Given the two squares, you can represent them as square1 = {center1, side1, angle1} and square2 = {center2, side2, angle2}. The idea for an algorithm is:

Step 1: rotate square2 by angle - angle1 so that its sides are parallel to the coordinate axes, as in your first example. Obtain the new

square1 ---> sqare10 = {center1, side1, 0}

Step 2: rotate square2 also by angle - angle1. Obtain the new square

square2 ---> sqare20 = {center2, side2, angle2 - angle1}

Step 3: introduce the new square square30 = {center2, side3, 0} so that this new square is the smallest square that contains sqare20 and its sides are parallel to the coordinate axes (the four vertices of square20 lie on the edges of square30 and the two of them share the same center). Square30's side-length is calculated as

side3 = side2 * (cos(angle2 - angle1) + sin(angle2 - angle1))

Step 4: Now you are in the situation of your first example where the pair of squares square10 and square30 have edges parallel to the coordinate axes. Check the relative position of these two squares: square10 and square30:

  • if square30 is completely inside square10 then the original square2 is completely inside the original square1

  • else if square30 has an edge that touches an edge of square10, then the original square2 has a vertex that touches on of the sides of the original square1

Futurologist
  • 1,874
  • 2
  • 7
  • 9