Solved

General Trigonomic Algorithm question...

Posted on 2004-03-28
3
349 Views
Last Modified: 2010-04-17
Okay so here's a question. I need an algorithm for rotating a point (p2) about an axis (p1). Here's the spaghetti code I have so far.

Radius = SquareRoot((p2.y - p1.y) ^ 2 + (p2.x - p1.x) ^ 2)
Radians = ArcCos((p1.y - p2.y) / radius) ' Angle between Vector(p1,p2) and unit vector (p1, {0, 1}). Rotating counter clockwise.
Radians = Radians + (2 * ((2 * PI) / 360)) 'Add two degrees to current angle of line
p2.y = (Cos(Radians) * Radius) + p1.y
p2.x = (Cos(Radians) * Radius) + p1.x

The code works fine for angles < 180 degrees. But as soon as I get to say 182 the line moves to an angle of 2 degrees counter clockwise from the unit vector... anyone know a better algorithm for this or how to fix this one to be able to run a full rotation? I need to be able to "add" angles to lines.
0
Comment
Question by:CyrexCore2k
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
mikkelp earned 125 total points
ID: 10702241
Hi

given a point A = (a1,a2) relative to the point 0 = (0,0) in the plane and a counter clockwise rotation of R degrees, your rotation is given by

A' = (a1 x cos R - a2 x sin R, a1 x cos R + a2 sin R)  // counterclockwise
A'' = (a1 x cosR + a2 x sin R, a1 x cos R - a2 sin R)  // clockwise

so to rotate any one point Q about the point P, find the vector D = (Q-P), rotate D R degrees and Add P.

p2' = p1 + ((p2-p1) rotated R degrees)

check this link
http://www.mapleapps.com/categories/maple_tools/animations/html/clock1.html
0
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 10705363
I'm sorry I've never done anything like this before. Do you think you could write out the spaghetti code?

In principle I understand how this works but for some reason when I try to write it out it all goes sour.
0
 
LVL 14

Author Comment

by:CyrexCore2k
ID: 10709648
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now