PDA

View Full Version : Perpendicular algorithm


hyperzoanoid
2002.05.29, 11:51 AM
i need to get one NSImage to be perpendicular to another and also keep the distance in porportion, meaning that it will move so that the distance is constant. Ive tried using right triangles but im not sure if it can cut it. Im pretty frustrated. Can someone help me?

jefftkd
2002.05.29, 01:03 PM
I'm not sure exactly what you mean. However, I think what you want is like planet motion? Where if image A spins, image B will rotate around it (perpendicular) and stay the same distance away. However, you also want image B to constantly "face" image A?

If this is wrong, please clarify what you want better, if correct, here are some ideas:

I assume you have a distance (in pixels) of how far you want image B from image A (let's call it d).

I also assume you have a position (ptA) of image A and a rotation angle (thetaA) that image A is oriented at.

Now, to keep image B "exactly" d pixels from image A, its position will be defined as:

NSPoint PositionOfImageB(int d, float thetaA, NSPoint ptA)
{
NSPoint ptB;

ptB.x = ptA.x + d * cos(thetaA);
ptB.y = ptA.y + d * sin(thetaA);

return ptB;
}
Now you want to get the orientation angle (thetaB) of so that it "faces" or is perpendicular to image A:

float OrientationOfImageB(NSPoint ptA,NSPoint ptB)
{
float thetaB;

thetaB = atan2(ptB.y - ptA.y,ptB.x - ptA.x);

/* This may be needed */
thetaB = 2.0 * pi - thetaB;

return thetaB;
}
Since you are using NSImage, I assume you know how to use Quartz to rotate the images, etc. (very easy).

Remember, all angles are in radians.

HTH,
Jeff

hyperzoanoid
2002.05.29, 04:28 PM
i feel so stupid now, i knew there was a simpler way, thanx. i have my own working implementation of the second part, but what does atan2() mean and do? Do you know of a site where i can find C math functions?

jefftkd
2002.05.29, 05:21 PM
There are two functions for finding the angle of a right triangle given the height and base (x,y) coordinates:

atan(double x)
atan2(double y,double x)

The difference is that atan() will only return an angle from -pi/2 to pi/2 given the tangent of an angle:

tan(x) = x'
atan(x') = x (limited to quadrants 1 and 3)

atan2() however will give the correct angle (0..2*pi) given both the height and length.

HTH,
Jeff :cool:

Jeff Binder
2002.05.29, 06:22 PM
atan2(y, x) is the same as atan(y / x). You can find information on the C standard library in any book about C, such as K&R (http://www.amazon.com/exec/obidos/ASIN/0131103628/qid=1022714328/sr=8-2/ref=sr_8_2/104-5008841-9554349).

jefftkd
2002.05.30, 11:09 AM
They are not the same.

atan() -> returns -pi/2 to +pi/2
atan2() -> returns -pi to +pi

The angles are the same (in theory) -- just the direction of a vector will be different. For the application in question, use atan2, not atan.

Jeff :cool: