Small Basic: How to Rotate a Shape Not from the Center

Overview

This article shows how to rotate a shape not from it's center in Small Basic programing language.  Because Shapes.Rotate() rotates a shape from the center of the shape.

Sample Program

This is a simple analog stop watch which have a second hand.  This second hand is a rectangle.  This rectangle is rotated not from it's center.  In line 24, this rectangle is rotated.  But after that, the rectangle is moved appropriate position in line 28.

1.' Simple Analog Stop Watch (QPR173)
2.gw = 640
3.gh = 480
4.GraphicsWindow.Width = gw
5.GraphicsWindow.Height = gh
6.GraphicsWindow.BackgroundColor = "LightGray"
7.cx = gw  / 2
8.cy = gh  / 2
9.w = 10
10.l = 180
11.DrawMark()
12.GraphicsWindow.PenWidth = 0
13.GraphicsWindow.BrushColor = "Silver"
14.secondhand = Shapes.AddRectangle(w, l)
15.Shapes.Move(secondhand, cx -  w / 2, cy -  l + w / 2)
16.t0 = Clock.ElapsedMilliseconds
17.Timer.Interval = 1000  / 24
18.Timer.Tick = OnTick
19.Sub OnTick
20.  t1 = Clock.ElapsedMilliseconds
21.  sec = (t1 - t0) / 1000
22.  GraphicsWindow.Title = sec
23.  a = sec  / 60 * 360
24.  Shapes.Rotate(secondhand, a)
25.  rad = Math.GetRadians(a)
26.  x = cx  + ((l - w) / 2) * Math.Sin(rad) - (w / 2)
27.  y = cy  - ((l - w) / 2) * Math.Cos(rad) - (l / 2)
28.  Shapes.Move(secondhand, x, y)
29.EndSub
30.Sub DrawMark
31.  GraphicsWindow.BrushColor = "White"
32.  r = l  + 3 * w
33.  GraphicsWindow.FillEllipse(cx - r, cy -  r, 2 * r, 2 *  r)
34.  GraphicsWindow.BrushColor = "Black"
35.  For a =  0 To 11 /  12 * 360 Step 360 / 12
36.    rad = Math.GetRadians(a)
37.    x = cx  + (l + w) * Math.Sin(rad) - 5
38.    y = cy  - (l + w) * Math.Cos(rad) - 5
39.    GraphicsWindow.FillEllipse(x, y, 10, 10)
40.  EndFor
41.EndSub

Where to Move

So, most important point is to where should you move the rectangle.  Following picture shows the rectangle before rotation.

If you'd like to rotate this rectangle from rotation center (red point), you should move this rectangle as following green arrow.

New position is calculated from (1) rotation center + (2) offset from rotation center to new center of the shape + (3) offset from new center of the shape to new left top corner (position) of the rectangle.  Actually, this expression is in line 26 and 27 of the program list above.


See Also

 


Other Languages