Archived

This topic is now archived and is closed to further replies.

newbie help with a c++ problem

This topic is 5986 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello all Just wondering if someone can help me with my math please.... float rise = ((SCREEN_HEIGHT/2) - yCoord); float run = (400 - xCoord); double CurrentAngle = atan(rise/run); float length = (float)Fast_Distance_2D(run + 0.5,rise + 0.5); float NewAngle = CurrentAngle + 1; rise = (cos(NewAngle)*length); run = (sqrt((pow(length,2))+(pow(rise,2)))); xCoord = ((SCREEN_HEIGHT/2 - 11) + run); yCoord = ((PLAYABLE_AREA/2 + 160 - 11) + rise); i''m trying to fing the xy coords of 1 degree next to the current one. Am i doing this right? Jimmy

Share on other sites
Well, I really can''t work through your code right now, but just for one hint, if you''re looking for 1 degree past the current, you''re going to have to do some conversions. Remember all the trig functions in C work with radians...

Share on other sites
don''t use atan(rise/run), use atan2(rise,run), because atan is only defined for a small domain and if you use it you will have to throw in a conditional and so forth. Using atan2 fixes everything. Melraidin is right about radians. Don''t use degrees for anything, otherwise you''ll end up switching back and forth constantly, which is just stupid. So learn to do everything in radians. There are 2pi radians to a circle, so 45 degrees would be pi/4 radians for example. You can multiply degrees by pi/180 to get constants, like your "1" in this example. Just try to stick to radians as much as possible.

Your code has some strange parts. Are you calculating run using the pythagorean theorum? Neat trick, keep in mind that n*n is probably significantly faster than using power, function calls are costly, short loops are more costly than they seem(because of mispredicted branches), and so forth. The rest of your code seems to be application specific so I can''t comment.

Share on other sites
Well, here''s some sample code on how to calculate the XY Position plus one degree away, to get you started

#include <cmath>
#include <iostream>

using namespace std;

int main( void )
{
double dXorg, dYorg;
double dAngle;

// Set Initial X,Y Coordinates
dXorg = 2.00;
dYorg = 2.00;

// Calculate Angle from X-Axis
dAngle = atan2(dXorg,dYorg);

dAngle= dAngle + 1.00/360.00 * 2 * 3.14;

// Compute new x and y
cout << "New X is" << dRadius * cos(dAngle) << endl;
cout << "New Y is" << dRadius * sin(dAngle) << endl;

return 0;
}

Share on other sites
Thanks for the help all =)

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 9
• 14
• 9
• 9
• 9
• Forum Statistics

• Total Topics
633331
• Total Posts
3011394
• Who's Online (See full list)

There are no registered users currently online

×