Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2d point rotation

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Im not in any math clases that even mention the word rotation, but Im making quite a few VB programs that require 2 dimensional points to be rotated. I would need something that's input is (x1, y1, xO, yO, angle) and output is (newx1, newy1) any formula's or code snipets or something that might help?
 
Your example is a little vague, but it looks like you have a line with endpoints (x1, y1) and (x0, y0) and you want to rotate it by angle, so that its endpoints are now (newx1, newy1) and (x0, y0).

If this is the case, what you need to do is determine the length of the line using the Pythagorean (sp?) theorem that we learned in 9th grade geometry (and you thought you'd never need anything from that class), then determine newy1 from the sine of the angle, and newx1 from the cosine from the angle.

Thus, the VB code for this would be

Sub RotateLine(x1 as Long, y1 as Long, x0 as Long, y0 as Long, Angle as Integer)

Dim LengthX as Long
Dim LengthY as Long
Dim LineLength as Double
Dim newx1 as Long
Dim newy1 as Long

'Get the difference between the X and Y points
LengthX = Abs(x1 - x0)
LengthY = Abs(y1 - y0)

'Get the length of the line
'Square Root of X squared + Y squared
LineLength = Sqr(LengthX^2 + LengthY^2)

'Cosine is the X coefficient of an angle
newx1 = x0 + (LineLength * Cos(Angle))
'Sine is the Y coefficient of an angle
newy1 = y0 + (LineLength * Sin(Angle))

End Function

I'm not sure how you want to return the result, but this is an implementation detail.

Hope this helps you out.

Steve
 
If you came back to this message to see if I replyed, then heres a reply anyway. That was pretty much exactly what I was asking for, but even though most people don't learn from pure code, Im figuring out some stuff. By the way, Im in 10'th grade, and far from geometry. Im in a level 3 applied math class. I did hear about the pothagorean theorum in 8'th grade, but I was never told what it does. Now I know it's used to determine distances from 2 points, and knowing that is usefull.
 
for some reason, this doesn't rotate a line at the cooridnates the specified degrees. I once had it increase the angle one degree each millisecond, and it put the line at one of 6 divisions, like if angle were 90, it wouldn't be at 90 degrees, but at another angle. it put it at some strange angle, but I spent a long time trying to figure out what I did wrong; but can't figure it out.
Code:
Dim x1 As Long, y1 As Long, y0 As Long, x0 As Long, angle As Integer, newx1 As Long, newy1 As Long
Sub RotateLine(x1 As Long, y1 As Long, x0 As Long, y0 As Long, angle As Integer)
    Dim LengthX As Long
    Dim LengthY As Long
    Dim LineLength As Double

    'Get the difference between the X and Y points
    LengthX = Abs(x1 - x0)
    LengthY = Abs(y1 - y0)

    'Get the length of the line
    'Square Root of X squared + Y squared
    LineLength = Sqr(LengthX ^ 2 + LengthY ^ 2)

    'Cosine is the X coefficient of an angle
    newx1 = x0 + (LineLength * Cos(angle))
    'Sine is the Y coefficient of an angle
    newy1 = y0 + (LineLength * Sin(angle))
y1 = newy1
x1 = newx1 'to use common variables.
End Sub

Sub Form_Load()
x0 = Form1.Width / 2
y0 = Form1.Height / 2
angle = 45
x1 = x0
y1 = Form1.Height / 6
RotateLine x1, y1, x0, y0, angle
Line (x0, y0)-(x1, y1)
End Sub

Sub HScroll1_Change()
angle = HScroll1.Value
RotateLine x1, y1, x0, y0, angle
Cls
Line (x0, y0)-(x1, y1)
End Sub
 
MS programs define/require the arguments for the TRig functions to be in radians. You appear to be giving it degrees. There are 2Pi Radians per 360 degrees. This is ~~~~ 57.3 degrees per radian, so your 45 (degrees) should be ~~~~ 0.7853 (RADIANS).

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top