[bmx] Rotate a hollow box by TAS [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : Rotate a hollow box
Author : TAS
Posted : 1+ years ago

Description : Seems like there should be a simpler way to do this (without using an image).

Code :
Code (blitzmax) Select
Graphics 800,600
Cls
SetBlend SOLIDBLEND
SetColor 200,200,200
SetScale 2,2
DrawRect 200,200,100,50
SetColor 255,0,0
SetHandle 0,0
SetLineWidth 2
Rot_Box(200,200,100,50,45)
Rot_Box(200,200,100,50,90)
Rot_Box(200,200,100,50,180)

Flip
WaitKey()


Function RotX#(x#,y#,deg#)
Return x*Cos(deg)-y*Sin(deg)
End Function

Function RotY#(x#,y#,deg#)
Return x*Sin(deg)+y*Cos(deg)
End Function

Function Rot_Box(bx#,by#,w#,h#,deg#)
'bx,by == upper right corner
'draw a box rotated about its center
Local s1#,s2#
GetScale(s1,s2)
SetScale 1,1
w=w/2*s1
h=h/2*s2
'calc center of rectangle
x=bx+w
y=by+h
'calc corner points after rotating about center
x1=x+RotX#(-w,-h,deg)
y1=y+RotY#(-w,-h,deg)
x2=x+RotX#( w,-h,deg)
y2=y+RotY#( w,-h,deg)
x3=x+RotX#(-w, h,deg)
y3=y+RotY#(-w, h,deg)
x4=x+RotX#( w, h,deg)
y4=y+RotY#( w, h,deg)

'draw the box
DrawLine x1,y1,x2,y2
DrawLine x1,y1,x3,y3
DrawLine x2,y2,x4,y4
DrawLine x3,y3,x4,y4
'restore scale
SetScale s1,s2
End Function


Comments :


Jesse(Posted 1+ years ago)

 it can be done with only two trig function calls plus without the rot function calls  will be quite a bit faster.


Floyd(Posted 1+ years ago)

 Exploiting the symmetry of a rectangle you only need to rotate one vertex.Suppose that (x,y) is one corner of a (possibly rotated) rectangle centered at the origin. The other three corners are then (-y,x), (-x,-y) and (y,-x).Just add (c,d) to each of these to move the rectangle so it is centered at (c,d).


Jesse(Posted 1+ years ago)

 @Floydmy logic tells me that only works for squares not rectangles. I could be wrong, Haven't tested it.


TAS(Posted 1+ years ago)

 I need to know where the corners are to detect resizing events but if have a better way please post or add a link.


Jesse(Posted 1+ years ago)

 I kind of thought you would figure it out easy.only two trig functions, no function calls and only two sides(corners) calculated:
Graphics 800,600
Cls
SetBlend SOLIDBLEND
SetColor 200,200,200
SetScale 2,2
DrawRect 200,200,100,50
SetColor 255,0,0
SetHandle 0,0
SetLineWidth 2
Rot_Box(200,200,100,50,45)
Rot_Box(200,200,100,50,90)
Rot_Box(200,200,100,50,180)

Flip
WaitKey()

Function Rot_Box(bx#,by#,w#,h#,deg#)
'bx,by == upper right corner
'draw a box rotated about its center
Local s1#,s2#
GetScale(s1,s2)
SetScale 1,1

w=w/2*s1
h=h/2*s2

'calc center of rectangle
x# = bx+w
y# = by+h

c# = Cos(deg)
s# = Sin(deg)
        'first corner
h1# = -w*c+h*s
v1# = -w*s-h*c
       'second corner
h2# =  w*c+h*s
v2# =  w*s-h*c

x1 = x + h1
y1 = y + v1
x2 = x + h2
y2 = y + v2

x3 = x - h2
y3 = y - v2
x4 = x - h1
y4 = y - v1

'draw the box
DrawLine x1,y1,x2,y2
DrawLine x1,y1,x3,y3
DrawLine x2,y2,x4,y4
DrawLine x3,y3,x4,y4
'restore scale
SetScale s1,s2
End Function
now if you are actually scalling the box and measurements don't scale the  drawing, scale the corners.


dw817(Posted 1+ years ago)

 I was looking at your program, TAS. If you don't wanna do it with all those calculations, you can let BlitzMAX's own SetRotation take over, but yes, it takes an image - created & destroyed on the spot.Might be easier to understand though.Quick & Dirty Rotating Rectangles:SetGraphicsDriver(GLMax2DDriver())
Graphics 800,600
SetBlend alphablend
Cls
SetColor 200,200,200
DrawRect 200,200,200,100
SetColor 255,0,0
SetLineWidth 2
rot_box 200,200,200,100,45,255,0,0
rot_box 200,200,200,100,90,255,0,0
Flip
WaitKey

Function rot_box(h,v,x,y,rr,r,g,b)
Local s:TImage=CreateImage(800,600),l=GetLineWidth()
Local i:TImage=CreateImage(x,y)
  GrabImage s,0,0
  Cls
  SetColor r,g,b
  DrawRect 0,0,x,y
  SetColor 0,0,0
  DrawRect l,l,x-l*2,y-l*2
  MidHandleImage i
  GrabImage i,0,0
  Cls
  SetColor 255,255,255
  DrawImage s,0,0
  SetRotation rr
  DrawImage i,h+x/2,v+y/2
  SetRotation 0
EndFunction
One thing I noticed, the diagonals do not get thin but maintain the correct size as the straight lines. [/i]