Solved

General Trigonomic Algorithm question...

Posted on 2004-03-28
3
351 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
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 …

839 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