How to center rotation (midhandle) a rect or oval?

BlitzMax Forums/BlitzMax Programming/How to center rotation (midhandle) a rect or oval?

Using a image with midhandle works great. But for the sake of examples it is nice to use the basic drawing commands along with SetRotation(). But somehow the rect is rotated around its top,left corner. I need a function to in every case recalculate the rotation and movement as if the rect was midhandled so I can use the same SetRotaion() command as for a midhandled image.

I figured it was as simple as moving it in X,Y with Cos,Sin.. But I guess not..

Well, I think the simpliest way is to save the Rect or Oval in an Image. Maybe like this

Type TItem
Field X,Y:Int
Field W,H:Int

Field Image:TImage

Function create:TItem(x,y,w,h,Oval:Byte = False)
	Local Item:TItem = New TItem
	Item.X = X
	Item.Y = Y
	Item.W = W
	Item.H = H
	Item.Image = CreateImage(w,h)
	
	Cls
	If Oval = True Then
		DrawOval(0,0,w,h)
	Else
		DrawRect(0,0,w,h)
	EndIf
	GrabImage(Item.Image,0,0)
	
	Cls
	
	Return Item
End Function

Method Draw()
	DrawImage Image,x,y	
End Method

End Type

Graphics 800,600,0,-1

Global R:TItem = TItem.Create(400,300,100,100)

Global Rot# = 0
Global MidHandle:Byte = False

While Not KeyHit(Key_Escape)

Cls
SetRotation(Rot)

R.Draw()


If KeyHit(KEY_Space) Then 
	If MidHandle = False Then 
		MidHandleImage(R.Image)
		MidHandle = True
	Else
		SetImageHandle(R.Image,0,0)
		MidHandle = False
	EndIf
EndIf


If KeyDown(Key_A) Then Rot:+.1
If KeyDown(Key_Y) Then Rot:-.1

SetRotation 0

SetColor(255,0,0)
DrawOval 400,300,5,5
SetColor 255,255,255

Flip
Wend
	


Press Space to toggle between Midhandle and use A and Y
to Rotate

Many Thanks!

That works perfect.

You can also do something like this
Local RectXPos = 100
Local RectYPos = 100
Local RectWidth = 10
Local RectHeight = 10

DrawRect (RectXPos-RectWidth/2,RectYPos-RectHeight/2,RectWidth,RectHeight)


ETA: Then again. after testing, that doesn't seem to work.

SetHandle

Yes, never seen this command before, but it really works by using SetHandle w/2,h/2.
Thx.

Good news, SetHandle..

SetRotation(Rot)
SetHandle w/2, h/2
DrawOval( 400-w/2 , 300-h/2, w, h )

Didn't AutoMidHandle do this at one point, or did I just imagine it?

Seems that's only for images although I can't remember if it included draws at an earlier level.