Find corners of box

Started by raven, February 26, 2019, 13:03:31

Previous topic - Next topic

raven

Hi,

i want to be able to know the coords of the corners of a box whether it is rotated or not.

Can anyone help please?

Graphics 640, 480

Type Box
Field x:Int, y:Int
Field w:Int, h:Int
Field angle:Float
Function Create:Box(x, y, w, h, angle)
Local b:Box = New Box
b.x = x
b.y = y
b.w = w
b.h = h
b.angle = angle
Return b
EndFunction
Method Draw()
SetHandle(w / 2, h / 2)
SetRotation(angle)
DrawRect(x, y, w, h)
EndMethod
EndType

Global Boxes:TList = New TList

Local b1:Box = New Box.Create(100, 100, 32, 32, 0)
Boxes.AddLast(b1)

Local b2:Box = New Box.Create(510, 320, 48, 48, 32)
Boxes.AddLast(b2)

Local b3:Box = New Box.Create(230, 380, 64, 64, 45)
Boxes.AddLast(b3)

SetClsColor(0, 0, 120)

Repeat
Cls
For Local b:Box = EachIn Boxes
b.Draw()
Next
Flip
Until KeyHit(KEY_ESCAPE) Or AppTerminate()

Derron

Reply from smartphone. So excuse the shortness of this teply.

As your handle is in the center you can easily calculate the new corner points A B C D of the rectangle by utilizing sin/cos aka trigonometry. Centered handle, midpoint between sides and a corner create a 90° triangle allowing to use sin/cos to calculate missing sides...and so new endpoints.

Then use an approach like this:
https://stackoverflow.com/questions/17136084/checking-if-a-point-is-inside-a-rotated-rectangle

Which means: sum up areas of all triangles the mouse coord can create with two corner points of the rotated rectangle. Is that sum bigger than the area of the rectangle then the cursor is outside.


Another approach is to use the rotation matrix to retrieve that information.
My approach above is easier to understand and only requires knowledge of 7/8th grade school mathematics ...you just need to remember.


Bye
Ron

Matty

#2
deleted