July 20, 2019, 07:33:46 AM

### Author Topic: Rotate rectangle with mouse?  (Read 200 times)

#### wombats

• Jr. Member
• Posts: 30
##### Rotate rectangle with mouse?
« on: April 14, 2019, 09:32:09 AM »
Hi,

I'm trying to rotate a rectangle with the mouse. However, what is throwing me off is that in my project, objects have a handle (ox/oy in the following code) that is subtracted from their positions when drawing. How should I implement that into the rotation?
Code: [Select]
`Graphics 640, 480Global x:Float = 320 - 16Global y:Float = 240 - 16Global w:Int = 32Global h:Int = 32Global ox:Int = 16Global oy:Int = 16Global angle:Float = 45Global rotate:Int = FalseRepeatCls   Local lx:Float = x - ox   Local ly:Float = y - oy      If MouseDown(MOUSE_LEFT)         If MouseX() => lx And MouseX() < lx + w And MouseY() => ly And MouseY() < ly + h         rotate = True      EndIf         Else      rotate = False   EndIf         If rotate         angle = -ATan2(MouseX() - (lx + (w / 2)), MouseY() - (ly + (h / 2)))   If angle < 0 Then angle = 360 - -angle         EndIf      SetRotation(angle)   DrawRect(lx, ly, w, h)      FlipUntil KeyHit(KEY_ESCAPE) Or AppTerminate()`

#### Derron

• Hero Member
• Posts: 2209
##### Re: Rotate rectangle with mouse?
« Reply #1 on: April 14, 2019, 09:46:46 AM »
You could just backup the handle, set it to the one you like, rotate as you wish and set the handle back.

Or you pass the handle/offset to the draw command

MyDraw(offsetX, offsetY)
-> DrawRect(x + offsetX, y + offsetY, width, height)

bye
Ron

#### wombats

• Jr. Member
• Posts: 30
##### Re: Rotate rectangle with mouse?
« Reply #2 on: April 14, 2019, 11:49:12 AM »
Hi,

Thank you for the reply. I have another question. I am trying to replicate behavior like this:

If the rectangle gets bigger or smaller, do I need to recalculate the handle? How would I do that?

#### Derron

• Hero Member
• Posts: 2209
##### Re: Rotate rectangle with mouse?
« Reply #3 on: April 14, 2019, 11:52:17 AM »
Multiply with the scale change?

handle offset eg was 10,10
size changed from 100,100 to 150,100
-> scale changed by *1.5, *1.0
-> adjust offset by *1.5, *1.0
-> offset = 10*1.5, 10*1.0
-> offset = 15, 10

bye
Ron

#### wombats

• Jr. Member
• Posts: 30
##### Re: Rotate rectangle with mouse?
« Reply #4 on: April 14, 2019, 12:12:33 PM »
Hi,

True, but I wouldn't be using scale functions as the rectangle could be resized to any width and height.

Truth be told, I'm not using BlitzMax for this, but I have found this forum very friendly and approachable, so I thought I'd ask here.

#### Derron

• Hero Member
• Posts: 2209
##### Re: Rotate rectangle with mouse?
« Reply #5 on: April 14, 2019, 12:50:07 PM »
Scale = newWidth / oldWidth.

Bye
Ron

#### wombats

• Jr. Member
• Posts: 30
##### Re: Rotate rectangle with mouse?
« Reply #6 on: April 14, 2019, 02:07:36 PM »
Thank you. I appreciate your help