09-14-2011, 09:50 AM
#1 (permalink )
amateur
Join Date: Sep 2011
Location: China
Posts: 13
Angel between two lines wrong.
Hi.
I want to get angles between two line.
So I used this code.
------------------------------------
int posX = (ScreenWidth) >> 1;
int posY = (ScreenHeight) >> 1;
double radians, degrees;
radians = atan2f( y - posY , x - posX);
degrees = -CC_RADIANS_TO_DEGREES(radians);
NSLog(@"%f %f",degrees,radians);
-------------------------------------
But it doesn't work .
The Log is that: 146.309935 -2.553590
What's the matter?
I can't know the reason.
Please help me.
09-14-2011, 09:57 AM
#2 (permalink )
Registered Member
Join Date: Feb 2011
Posts: 60
maybe your not getting it as your trying to work out the angel?! not the angle.... :P
09-14-2011, 10:37 AM
#3 (permalink )
Senior Member
iPhone Dev SDK Supporter
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
Can you put in some sample data? What values do you use, what results do you get, and what are the correct results?
09-14-2011, 10:59 AM
#4 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 30
How do you know that is not the correct angle, it is less than 180 degrees and greater than 90, looks like it could be correct from the diagram you have drawn.
09-14-2011, 11:54 AM
#5 (permalink )
Registered Member
Join Date: Aug 2011
Location: USA
Posts: 18
Based on your drawing it looks like your code might just be off by 180 degrees. Try:
Code:
float pointAngle(float x1,float y1,float x2,float y2)
{
float angle=atan2f(x2-x1,y2-y1);
float degrees=angle*(180/M_PI);
return 180.0f-degrees;
}
where x1 and y1 are your PosX and PosY and see if you like the results.
09-14-2011, 12:12 PM
#6 (permalink )
amateur
Join Date: Sep 2011
Location: China
Posts: 13
Quote:
Originally Posted by
smithdale87
Can you put in some sample data? What values do you use, what results do you get, and what are the correct results?
x,y position is touch position.
I've touched under the (posX,posY)
Also I've touched any position on the screen.
But the result is the same.
x: 146.309935
y: -2.553590
I can't know why it is.
09-14-2011, 02:23 PM
#7 (permalink )
Registered Member
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
I think the problem is, you forgot that, on the screen, positive Y is down, not up, so in your picture, (Y - PosY) and (X - PosX) have different signs.
Based on this, atan2f should return the angle clockwise from the positive-X axis.
Last edited by That Don Guy; 09-16-2011 at 09:41 AM .
Reason: Forgot that clocks go in the other direction on this planet
09-14-2011, 08:10 PM
#8 (permalink )
amateur
Join Date: Sep 2011
Location: China
Posts: 13
Thank for your help.
I solved this problem.
The reason is simple.
Here is a code I used.
if(x < posX )
{
radians = atan2( posX - x, y - posY);
degrees = 360 - CC_RADIANS_TO_DEGREES(radians);
}
else
{
radians = atan2( x - posX,y - posY);
degrees = CC_RADIANS_TO_DEGREES(radians);
}
And it works very well.
I've inserted wrong value to the x,y.
Sorry about this.
Best.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 404
16 members and 388 guests
7twenty7 , Eclectic , eski , EvilElf , fiftysixty , HemiMG , iOS.Lover , JackReidy , jarv , Pudding , sacha1996 , teebee74 , tim0504 , UMAD , VinceYuan , yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28