Ooops
May 28, 2020, 02:38:16 AM

### Author Topic: Find corners of box  (Read 607 times)

#### raven

• Jr. Member
• Posts: 6
##### Find corners of box
« on: February 26, 2019, 01:03:31 PM »
Hi,

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

Code: [Select]
`Graphics 640, 480Type 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) EndMethodEndTypeGlobal Boxes:TList = New TListLocal 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 FlipUntil KeyHit(KEY_ESCAPE) Or AppTerminate()`

#### Derron

• Hero Member
• Posts: 2972
##### Re: Find corners of box
« Reply #1 on: February 26, 2019, 06:37:30 PM »
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

• Hero Member
• Posts: 1032
##### Re: Find corners of box
« Reply #2 on: February 27, 2019, 05:53:18 AM »
deleted