Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2011, 09:50 AM   #1 (permalink)
amateur
 
Join Date: Sep 2011
Location: China
Posts: 13
amateur.jung8 is on a distinguished road
Default 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.

amateur.jung8 is offline   Reply With Quote
Old 09-14-2011, 09:57 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 60
GFish is on a distinguished road
Default

maybe your not getting it as your trying to work out the angel?! not the angle.... :P
GFish is offline   Reply With Quote
Old 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
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

Can you put in some sample data? What values do you use, what results do you get, and what are the correct results?
smithdale87 is offline   Reply With Quote
Old 09-14-2011, 10:59 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 30
CKAmike is on a distinguished road
Default

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.
CKAmike is offline   Reply With Quote
Old 09-14-2011, 11:54 AM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: USA
Posts: 18
Ridgewood is on a distinguished road
Default

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.
__________________
Apps:
Exian - Brick out in space

www | Twitter | Facebook
Ridgewood is offline   Reply With Quote
Old 09-14-2011, 12:12 PM   #6 (permalink)
amateur
 
Join Date: Sep 2011
Location: China
Posts: 13
amateur.jung8 is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
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.
amateur.jung8 is offline   Reply With Quote
Old 09-14-2011, 02:23 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2009
Location: Benicia, CA
Age: 50
Posts: 152
That Don Guy is on a distinguished road
Default

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
That Don Guy is offline   Reply With Quote
Old 09-14-2011, 08:10 PM   #8 (permalink)
amateur
 
Join Date: Sep 2011
Location: China
Posts: 13
amateur.jung8 is on a distinguished road
Default

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.
amateur.jung8 is offline   Reply With Quote
Reply

Bookmarks

Tags
cocos2d, iphone, objective-c, xcode4

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0