Rotating Object2D around rotation origin

Hey there,

How can I rotate Object2D around an origin point? Anybody written a class?

Thanks

You can use trig for this.

How is this done. I have a class Point written by someone else.

Class Point
Sub set(x As integer, y As integer)
  self.x = x
  self.y = y
End Sub


Sub Constructor()
  
End Sub


Sub draw(g As Graphics, dotRadius As double)
  dim r2 As double = dotRadius * 2 + 1
  g.FillOval(x-dotRadius, y-dotRadius, r2, r2)
End Sub


Function hit(hitX As integer, hitY As integer, hitRadius As double) As boolean
  return hitRadius > Sqrt( (x-hitX)^2 + (y-hitY)^2 )
End Function


Sub move(dx As integer, dy As integer)
  x = x + dx
  y = y + dy
End Sub


Function Operator_Add(p as Point) As Point
  dim returnP as new point
  
  returnP.x = me.x + p.x
  returnP.y = me.y + p.y
  
  return returnP
End Function


Sub rotate(rotation as double, origin as point)
  //new position relative to point of origin
  me.x = (origin.x + ((me.x - origin.x) * cos(rotation) - (me.y - origin.y) * sin(rotation)))
  me.y = (origin.y + ((me.x - origin.x) * sin(rotation) + (me.y - origin.y) * cos(rotation)))
End Sub
End Class

How can I use this class to my advantage and create an origin point for the Group2D?

Thanks

if it helps, rotation about can be considered as
rotation about followed by translation

To explain:
Imagine you start with 2 letters X L
You want to rotate the L
And that the normal point is the corner.

Rotate anti clock by 90 degrees and you get X _|

If you rotate by the top left corner instead, you get the same resulting shape, but the shape is on the ‘baseline of the row above’
_|
X

If you rotate by the bottom right corner, you get the same resulting shape, but the shape is under the baseline.
X
_|

So you can apply normal rotation to an object 2D item, and change the x,y co-ordinates afterwards by the difference between ‘normal x,y’ and ‘assumed x,y’

Indentation just doesn’t survive in this forum.

Thanks Jeff.

[quote=171389:@Jeff Tullin]if it helps, rotation about can be considered as
rotation about followed by translation

To explain:
Imagine you start with 2 letters X L
You want to rotate the L
And that the normal point is the corner.

Rotate anti clock by 90 degrees and you get X _|

If you rotate by the top left corner instead, you get the same resulting shape, but the shape is on the ‘baseline of the row above’
_|
X

If you rotate by the bottom right corner, you get the same resulting shape, but the shape is under the baseline.
X
_|

So you can apply normal rotation to an object 2D item, and change the x,y co-ordinates afterwards by the difference between ‘normal x,y’ and ‘assumed x,y’[/quote]
How do you work out the difference between normal X and assumed X?

Thanks

You can add the Object2D to a Group2D at a specific offset then use the group for transforming

[code]dim shape As new RectShape
shape.Width = 200
shape.Height = 80
shape.X = 100
shape.Y = 40

dim group As new Group2D
group.Append(shape)[/code]

Here the groups origin is at <0, 0>, which in the shapes reference frame is <-100, -40>. Rotating, scaling and translating the group effectively gives the shape the origin <-100, -40>.

Or are you trying to reposition an already appended shape, that is, you don’t want to give the shape an origin, you just want to arbitrarily rotate it. Or are you trying to have an adjustable origin?

As far as manually moving the coordinate, that code you posted has the formula

me.x = (origin.x + ((me.x - origin.x) * cos(rotation) - (me.y - origin.y) * sin(rotation))) me.y = (origin.y + ((me.x - origin.x) * sin(rotation) + (me.y - origin.y) * cos(rotation)))

First translate so that the origin point is at <0, 0>
dx = x - origin.x
dy = y - origin.y

Then apply the rotation formula
xr = dx * cos(a) - dy * sin(a)
yr = dx * sin(a) + dy * cos(a)

Then translate <0, 0> back to the origin point
x = origin.x + xr
y = origin.y + yr

As an ostensibly optimized method; maybe make it extends…

[code]Sub rotateObj2D(obj As Object2D, rotateAngle As double, origin As Point)
dim sina, cosa, dx, dy As double

sina = sin(rotateAngle) //precompute
cosa = cos(rotateAngle)
dx = obj.X - origin.x
dy = obj.Y - origin.y

obj.X = origin.x + dx * cosa - dy * sina //rotate objs position
obj.Y = origin.y + dx * sina + dy * cosa

obj.Rotation = obj.Rotation + rotateAngle //and add angle of rotation

End Sub[/code]

If the object would normally rotate around 0,0 and you are rotating around 20,10 instead, then the difference is literally

20-0 = 20
10-0 = 10

If the object normally rotates around the centre, and the object is 100 x 100 in size, then ‘normal’ x,y is 50,50

Rotating around 10,20 instead is

10-50 = -40
20- 50 = -30