Rotating XYs

03-21-2002, 05:19 AM
I need a function like this :

Public Type XY
X As Long
Y As Long
End Type

Function RotateXY(Center as XY, Angle as long, PointToRotate as XY) as XY
End fuction

'Dim Cen,Point as XY
'Cen.X = 2
'Cen.Y = 2
'Point.X = 2
'Point.Y = 1
'RotateXY(Cen, 90, Point).X will return 3
'RotateXY(Cen, 90, Point).Y will return 2

Help me make the "RotateXY" function, thanks :)

03-21-2002, 06:38 AM
You need to use Trigonometry.

Calculate the distance between point and centre
Calculate the angle between the point and some arbitrary zero line (common values are up or right).
Add the rotation angle
Calculate the new x and y positions based on the length previous calculated.

If you have no idea about how to do any of that then I'd suggest you get a Trig book and read carefully.

03-21-2002, 07:02 AM
Can you write the code for me please? :D

03-21-2002, 07:36 AM
Er, NO! Try here

03-21-2002, 07:39 AM

03-21-2002, 07:47 AM
No. If you don't know trig then you should really give up on the idea of writing a game of this sort until you have learnt it. The concepts involved (sin, cos, etc.) are some of the most basic in the games field. You will need them to progress and I would not be doing you any favours by allowing you to skip them. It would just come back to bite you later.

03-21-2002, 08:18 AM
Not to mention that the code only requires a small bit of trigonometry.

03-21-2002, 10:53 AM
You should be able to find lots of examples of rotation if you do a bit of searching....

I had to implement rotation as part of my Kaleidescope program that I posted on PlanetSourceCode for instance...

03-21-2002, 11:44 AM
That Kaleidescope ( is most impressive BillSoo! Really cool stuff.


03-21-2002, 11:10 PM
Kaleidescope's screenshot looks impresssive...

03-21-2002, 11:38 PM
Like this?

Public Type XY
X As Long
Y As Long
End Type

Function RotateXY(Center as XY, Angle as long, PointToRotate as XY) as XY
Dim Temp as XY
Temp = PointToRotate
PointToRotate.X = PointToRotate.X - Center.X
PointToRotate.Y = PointToRotate.Y - Center.Y
RotateXY.X = (PointToRotate.X * cos(Angle) - PointToRotate.Y * sin(Angle)) + Temp.X
RotateXY.Y = (PointToRotate.X * sin(Angle) + PointToRotate.Y * cos(Angle)) + Temp.Y
'does this formula defaults the center to be 0,0 ?
'I'm rushing, so it may be wrong
End fuction

03-22-2002, 07:07 AM
how can you not know trig....

all you really need is to know how to use cos...sin


all radius does is decide how far to move the point around the Origin or wherever

if you dont want to have the main point at origin
just add where you want it to the x,y

03-22-2002, 10:57 AM
The basic method for rotating a Point around a Centre is:

a) convert the cartesian coordinates of the point to polar coordinates (ie. angle and radius)

b) add the angle you wish to rotate

c) convert the polar coordinates back to cartesian coordinates.

As AndrewW shows, the last part is easy. Just use the sin and cos functions to convert.

x = cos(angle) * radius + centre.X
y = sin(angle) * radius + centre.Y

The second step is easy too. Just add the angle you wish to rotate.

Which leaves the first step....this isn't too hard but it can be tricky because each of the 4 quadrants is slightly different.

Here is the GENERAL case:

angle=atan((point.y-centre.y)/(point.X - centre.X))
radius = sqr((point.x-centre.x)^2+(point.Y-centre.Y)^2)

One obvious problem, if the line is vertical (ie. 90 degrees) then point.X = centre.X and you have a division by 0 error. So you should handle this special case separately.

03-22-2002, 11:23 AM
angle=atan((point.y-centre.y)/(point.X - centre.X))
I think that's atn((point.y-centre.y)/(point.x - centre.X))....

Welcome back!:cool:

03-22-2002, 01:40 PM

Here's some code for a true ArcTangent function I got in the vb sourcebook

Public Function ArcTangent( _
ByVal dblIn As Double, _
ByVal dblIn2 As Double) _
As Double
' Comments : Returns the inverse tangent of the supplied numbers.
' Note that both values cannot be zero.
' Parameters: dblIn - First value
' dblIn2 - Second value
' Returns : Inverse tangent as a double
' Source : Total VB SourceBook 6
Const cdblPI As Double = 3.14159265358979

On Error GoTo PROC_ERR

Select Case dblIn

Case 0

Select Case dblIn2
Case 0
' Undefined
ArcTangent = Null
Case Is > 0
ArcTangent = cdblPI / 2
Case Else
ArcTangent = -cdblPI / 2
End Select

Case Is > 0
If dblIn2 = 0 Then
ArcTangent = 0
ArcTangent = Atn(dblIn2 / dblIn)
End If
Case Else
If dblIn2 = 0 Then
ArcTangent = cdblPI
ArcTangent = (cdblPI - Atn(Abs(dblIn2 / dblIn))) * Sgn(dblIn2)
End If
End Select

Exit Function

MsgBox "Error: " & Err.Number & ". " & Err.Description, , _

End Function

It takes 2 inputs, deltaX and deltaY, so it can figure out which quadrant you are in and process the data appropriately.

03-22-2002, 03:51 PM
So, BillSoo--

You're saying that one could use this custom Arc Tangent function to convert cartesian coordinates into polar coordinates?

Could you give a small example to elaborate? Thanks.

03-22-2002, 04:15 PM
Well, suppose you had something like:

type Point
X as long
Y as long
end type

dim centre as Point
dim pt as Point
dim delta as double 'angle you wish to rotate
dim theta as double 'starting/ending angle
dim radius as double

centre.X = 10
centre.Y = 22

pt.X = 20
pt.Y = 9

delta = 5 * 3.14159265359 / 180# 'convert 5 degrees to radians
'since all functions require radians rather than degrees

theta = ArcTangent((pt.X - centre.X),(pt.Y - centre.Y))

radius = SQR((pt.X-centre.X)^2+(pt.Y-centre.Y)^2) 'good old pythagoras

theta = theta + delta 'add 5 degrees to the original angle

pt.X = cos(theta) * radius + centre.X
pt.Y = sin(theta) * radius + centre.Y

Point PT should now have been rotated 5 degrees around Point Centre.

03-22-2002, 04:41 PM
Very interesting. I could've wrote something like that. But I didn't;) . I like this function! [Added to lib] :cool:

03-22-2002, 04:57 PM
I've written stuff like that in the past but it's always a pain to find later. This Total Sourcebook code library we bought has fairly common stuff in it, but at least it's well organized.

03-22-2002, 05:01 PM
Would code like this be suitable in the code library (where I can't post)? <--Almost forgot my question mark:cool:
I've found myself knowing what Pi is to 8 decimal places without even trying. 3.1415926 OK 7!:D

03-22-2002, 05:09 PM
Yessss....I guess it would. But it's probably not a good idea to post the exact ArcTangent function I copied from the SourceBook. I'll write up my own version and post it, after all, I've written this function before so I don't feel that it would be copyright violation....

The actual idea of separating the parameters (x and y) comes from a C function which works in that manner so again, that should be ok...

03-22-2002, 05:15 PM
OK, here is a version that I found in an old VB3 program I wrote:

Static Function ArcTangent (op#, ad#) As Double
pi# = 3.141593
If ad# = 0 Then
If op# > 0 Then
ArcTangent = pi# / 2#
ArcTangent = pi# * 1.5
End If
at# = Atn(op# / ad#)
If ad# < 0 Then
ArcTangent = pi# + at#
ElseIf op# < 0 Then
ArcTangent = pi# + pi# + at#
ArcTangent = at#
End If
End If
End Function

Notice that this was before I started to use Option Explicit....

03-23-2002, 11:02 AM
Here is mine:
Public Function ArcTan (RealPart as Single, Complex as Single) as Currency
Dim Pi as Currency, AtnHold as Currency
Pi = 3.1415926
If RealPart = 0 Then
AtnHold = Pi / 2 * Sgn(Complex)
ElseIf RealPart = 1 Then
AtnHold = Atn(Complex/RealPart)
Else 'RealPart = -1
AtnHold = (Sgn(Complex) * Pi) - (Sgn(Complex) * Atn(Abs(Complex) / Abs(RealPart)))
End If
ArcTan = AtnHold
End Function
I haven't tested it, but I use the arctangent so much in my classes that it's almost second nature... I bet it doesn't work knowing my luck.:cool:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum