March 01, 2021, 10:23:51 PM

Author Topic: [bmx] Rotate a hollow box by TAS [ 1+ years ago ]  (Read 442 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Rotate a hollow box by TAS [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
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
  1. Graphics 800,600
  2. Cls
  3. SetBlend SOLIDBLEND
  4. SetColor 200,200,200
  5. SetScale 2,2
  6. DrawRect 200,200,100,50
  7. SetColor 255,0,0
  8. SetHandle 0,0
  9. SetLineWidth 2
  10. Rot_Box(200,200,100,50,45)
  11. Rot_Box(200,200,100,50,90)
  12. Rot_Box(200,200,100,50,180)
  13.  
  14. Flip
  15. WaitKey()
  16.  
  17.  
  18. Function RotX#(x#,y#,deg#)
  19.         Return x*Cos(deg)-y*Sin(deg)
  20. End Function
  21.  
  22. Function RotY#(x#,y#,deg#)
  23.         Return x*Sin(deg)+y*Cos(deg)
  24. End Function
  25.  
  26. Function Rot_Box(bx#,by#,w#,h#,deg#)
  27.         'bx,by == upper right corner
  28.         'draw a box rotated about its center
  29.         Local s1#,s2#
  30.         GetScale(s1,s2)
  31.         SetScale 1,1
  32.         w=w/2*s1
  33.         h=h/2*s2
  34.         'calc center of rectangle
  35.         x=bx+w
  36.         y=by+h
  37.         'calc corner points after rotating about center
  38.         x1=x+RotX#(-w,-h,deg)
  39.         y1=y+RotY#(-w,-h,deg)
  40.         x2=x+RotX#( w,-h,deg)
  41.         y2=y+RotY#( w,-h,deg)
  42.         x3=x+RotX#(-w, h,deg)
  43.         y3=y+RotY#(-w, h,deg)
  44.         x4=x+RotX#( w, h,deg)
  45.         y4=y+RotY#( w, h,deg)
  46.  
  47.         'draw the box
  48.         DrawLine x1,y1,x2,y2
  49.         DrawLine x1,y1,x3,y3
  50.         DrawLine x2,y2,x4,y4
  51.         DrawLine x3,y3,x4,y4
  52.         'restore scale
  53.         SetScale s1,s2
  54. 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:
Code: [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 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:
Code: [Select]
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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal