Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

How to find the angle from two points?

kpeters213kpeters213 Posts: 22Registered Users
edited June 2010 in iPhone SDK Development
So I have this program where a user touches the screen and it records its coordinates, and then the user can move around the screen and when they pick up their finger it records the coordinates again. Here's the code

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

	CGPoint touchPointbegin = [[touches anyObject] locationInView:self.view];
	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
	beginData.text = locationMessage;
	[locationMessage release];
	
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	
	CGPoint touchPointend = [[touches anyObject] locationInView:self.view];
	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
	endData.text = locationMessage;
	[locationMessage release];
	
}


So I want to find the angle between the two points. I have the right equations I think but when I try to use, for example, touchPointend.x outside of touchesEnded, it says it is undeclared.

Any ideas?


-KP
Post edited by kpeters213 on

Replies

  • DuddDudd Posts: 5New Users
    edited June 2010
    Declare the touchPointend and touchPointbegin as an instance variable in your header file, then you can store their value in touchesBegin and End while still being able to access them in the rest of your program.
  • kpeters213kpeters213 Posts: 22Registered Users
    edited June 2010
    Dudd wrote: »
    Declare the touchPointend and touchPointbegin as an instance variable in your header file, then you can store their value in touchesBegin and End while still being able to access them in the rest of your program.


    Yea I'm trying to do that. In the header file I put
    CGPoint *touchPointend;
    CGPoint *touchPointbegin;
    

    But what do I change to my original code.

    It says the local declaration hides the instance variable.

    Thanks for any help.

    KP
  • Duncan CDuncan C Posts: 8,992Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited June 2010
    kpeters213 wrote: »
    Yea I'm trying to do that. In the header file I put
    CGPoint *touchPointend;
    CGPoint *touchPointbegin;
    

    But what do I change to my original code.

    It says the local declaration hides the instance variable.

    Thanks for any help.

    KP

    Get rid of the local variables touchPointend and touchPointbegin. If you have 2 variables with the same name, the compiler can't tell which one your are referring to, and uses the local variable, ignoring the instance variable.

    Once you get rid of the local variables, referring to touchPointend or touchPointbegin changes the value of the instance variable. The instance variable sticks around as long as the object sticks around, so you can do math with touchPointend and touchPointbegin.

    BTW, the function you need to calculate the angle between two points is atan2(change_in_x, change_in_y). That will give you an angle in radians. You can then convert that to degrees if you need it in degrees.


    Regards,

    Duncan C
    WareTo
    Check out our apps in the Apple App store
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • hm50hm50 Posts: 162Registered Users
    edited June 2010
    ...it looks like if you drop the "CGPoint *" from touchesBegan and touchesEnded, you will be fine with those variables...
    iPhone 3G<br />
    <br />
    Support Indie Devs!! (that goes for newbs too!)<br />
    <br />
    Apps:<br />
    <a href="http://itunes.apple.com/us/app/spell-blocks-with-sight-words/id361690226?mt=8" target="_blank">Spell Blocks</a><br />
    <a href="http://phobos.a
  • kpeters213kpeters213 Posts: 22Registered Users
    edited June 2010
    First of thanks for everyones help so far. I am just still confused because I thought I dropped the local declaration, but it stil won't work so I must be doing something wrong.

    Here's my header file:
    @interface PaperTossTestViewController : UIViewController {
    
    	IBOutlet UITextField *angleData;
    	IBOutlet UITextField *beginData;
    	IBOutlet UITextField *endData;
    	
    	CGPoint touchPointbegin;
    	CGPoint touchPointend;
    	
    
    	
    }
    
    @property (nonatomic, retain) IBOutlet UITextField *angleData;
    @property (nonatomic, retain) IBOutlet UITextField *beginData;
    @property (nonatomic, retain) IBOutlet UITextField *endData;
    
    
    
    
    @end
    


    and here is the implementation:
    @synthesize angleData;
    @synthesize beginData;
    @synthesize endData;
    
    
    
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    
    {
    
    	touchPointbegin = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
    	beginData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    float xpoint = (touchPointend.x - touchPointend.y);
    



    I feel like there is something really simple that I am missing. Sorry if I'm asking a dumb question.

    KP
  • Duncan CDuncan C Posts: 8,992Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited June 2010
    kpeters213 wrote: »
    First of thanks for everyones help so far. I am just still confused because I thought I dropped the local declaration, but it stil won't work so I must be doing something wrong.

    Here's my header file:
    @interface PaperTossTestViewController : UIViewController {
    
    	IBOutlet UITextField *angleData;
    	IBOutlet UITextField *beginData;
    	IBOutlet UITextField *endData;
    	
    	CGPoint touchPointbegin;
    	CGPoint touchPointend;
    	
    
    	
    }
    
    @property (nonatomic, retain) IBOutlet UITextField *angleData;
    @property (nonatomic, retain) IBOutlet UITextField *beginData;
    @property (nonatomic, retain) IBOutlet UITextField *endData;
    
    
    
    
    @end
    


    and here is the implementation:
    @synthesize angleData;
    @synthesize beginData;
    @synthesize endData;
    
    
    
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    
    {
    
    	touchPointbegin = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
    	beginData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    float xpoint = (touchPointend.x - touchPointend.y);
    



    I feel like there is something really simple that I am missing. Sorry if I'm asking a dumb question.

    KP


    I don't see anything obvious wrong. What doesn't work? Do you get a compiler error? Does it crash? Do you get a result different than you expect? Please be specific.


    Regards,

    Duncan C
    WareTo
    Check out our apps in the Apple App store
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • icebeingicebeing Posts: 3New Users
    edited June 2010
    kpeters213 wrote: »
    So I have this program where a user touches the screen and it records its coordinates, and then the user can move around the screen and when they pick up their finger it records the coordinates again. Here's the code

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    
    {
    
    	CGPoint touchPointbegin = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
    	beginData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	CGPoint touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    }
    


    So I want to find the angle between the two points. I have the right equations I think but when I try to use, for example, touchPointend.x outside of touchesEnded, it says it is undeclared.

    Any ideas?


    -KP

    You need 3 points to determine an angle, not 2. Unless you're looking for Euler angles?

    /ice
  • KalimbaKalimba ¿La Islas Hermosas?Posts: 2,181iPhone Dev SDK Supporter, Registered Users
    edited June 2010
    kpeters213 wrote: »
    ...and here is the implementation:
    float xpoint = (touchPointend.x - touchPointend.y);
    
    You've got this line of code floating out in the middle of nowhere. What is it supposed to be doing there?
    <a href="http://bit.ly/gAmufK" target="_blank">icon-r-64.png</a> ~~ Word Flurry ~~ <a href="http://itunes.apple.com/us/app/word-flurry/id399660201?mt=8" target="_blank">App Store</a> / <a href="http://bit.ly/bJiK99
  • kpeters213kpeters213 Posts: 22Registered Users
    edited June 2010
    I don't see anything obvious wrong. What doesn't work? Do you get a compiler error? Does it crash? Do you get a result different than you expect? Please be specific.

    Sorry I should have been more specific. When I compile it I recieve an error that says...

    touchPointend undeclared here

    Kalimba wrote: »
    You've got this line of code floating out in the middle of nowhere. What is it supposed to be doing there?

    Yes also I should have explained it. It is just a test to see if I can use touchPointend's coordinates outside of touchedEnded. So basically I have:
    float xpoint = (touchPointend.x - touchPointend.y);
    
    NSLog(@"%.0f", xpoint);
    

    I check the console to see if it works, and it works great when I put it inside touchesEnded like this....
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    float xpoint = (touchPointend.x - touchPointend.y);
    
    
    NSLog(@"%.0f", xpoint);
    	
    }
    

    but when it like this:
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    	
    }
    
    
    float xpoint = (touchPointend.x - touchPointend.y);
    
    
    NSLog(@"%.0f", xpoint);
    

    I can't seem to get it to work. It says touchPointend is undeclared even though I thought I declared it globally.

    Any ideas?
  • Duncan CDuncan C Posts: 8,992Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited June 2010
    The second form is invalid. You have:
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    	
    }
    
    
    float xpoint = (touchPointend.x - touchPointend.y);
    
    
    NSLog(@"%.0f", xpoint);
    


    The statement that begins "float xpoint =" is not inside any method or function. You can't put executable code outside of the body of a method or function. That is why you are getting an error.

    If you simply move the closing brace ("}") down below the NSLog statement, everything should be fine.


    Regards,

    Duncan C
    WareTo
    Check out our apps in the Apple App store
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • kpeters213kpeters213 Posts: 22Registered Users
    edited June 2010
    Duncan C wrote: »
    The statement that begins "float xpoint =" is not inside any method or function. You can't put executable code outside of the body of a method or function. That is why you are getting an error.

    If you simply move the closing brace ("}") down below the NSLog statement, everything should be fine.


    Regards,

    Duncan C
    WareTo
    Check out our apps in the Apple App store


    Thanks so much. That works great. Here's me revised code that works:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
    	touchPointbegin = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
    	beginData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    	float xpoint = (((atan2((touchPointend.x - touchPointbegin.x) , (touchPointend.y - touchPointbegin.y)))*180)/M_PI);
    	NSLog(@"%.0f", xpoint);
    	NSString *angleString = [[NSString alloc] initWithFormat:@"%.0f", xpoint];
    	angleData.text = angleString;
    	[angleString release];
    	
    	
    }
    



    The equation might be a little off right now. When the angle of the swipe goes past 180 degrees it goes into negative 180 and then goes back to 0. I'd rather it go all the way to 360. I think that is easily fixed though.

    Anyway thanks to Duncan C, hm50, Dudd, icebeing, and Kalimba for helping me out.

    Once I get the angle to work the way I want I'll try to put this up for open source code
  • kpeters213kpeters213 Posts: 22Registered Users
    edited June 2010
    Ok so now I have my equation all set.

    So what happens in the view is that when the user swipes anywhere on the screen it shows the angle of the swipe in a text view.

    Now what I want is to have a button at the bottom so that when the user "touches down" it records the coordinates, and then when the user "lifts up outside" it records the coordinates and does all the computations. Basically I want to replace touchesBegan and touchesEnded with my Button actions.

    Here's my code:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointbegin = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointbegin.x, touchPointbegin.y];
    	beginData.text = locationMessage;
    	[locationMessage release];
    	
    }
    
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    	
    	touchPointend = [[touches anyObject] locationInView:self.view];
    	NSString *locationMessage = [[NSString alloc] initWithFormat:@"X = %.0f Y = %.0f", touchPointend.x, touchPointend.y];
    	endData.text = locationMessage;
    	[locationMessage release];
    	
    	float xpoint = ((((atan2((touchPointend.x - touchPointbegin.x) , 
    							(touchPointend.y - touchPointbegin.y)))*180)/M_PI));
    	float xpointer;
    	if (signbit(xpoint)) {
    		NSLog(@"Negative");
    		float xpointer = xpoint + 270;
    		NSLog(@"%.0f", xpointer);
    		NSString *angleString = [[NSString alloc] initWithFormat:@"%.0f", xpointer];
    		angleData.text = angleString;
    		[angleString release];
    	}
    	else if (xpoint < 90)
    	{
    		float xpointer = xpoint + 270;
    		NSString *angleString = [[NSString alloc] initWithFormat:@"%.0f", xpointer];
    		angleData.text = angleString;
    		[angleString release];
    	}
    	else if (xpoint >= 90)
    	{
    		float xpointer = xpoint - 90;
    		NSString *angleString = [[NSString alloc] initWithFormat:@"%.0f", xpointer];
    		angleData.text = angleString;
    		[angleString release];
    	}
    	
    	NSString *angleString = [[NSString alloc] initWithFormat:@"%.0f", xpointer];
    	[angleString release];
    	
    	
    }
    
    
    - (IBAction)startTracking
    {
    	
    	
    		
    }
    
    
    - (IBAction)endTracking
    { 
    	
    	
    	
    }
    


    So I first tried replacing -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event and -(void)touchesBegan with my IBAction but it would say touches isn't declared which I understand why. But then I put
    - (IBAction)endTracking:(NSSet *)touches withEvent:(UIEvent *)
    

    and it just says the method for endTracking is not found. I know what I am doing wrong but I just don't know how to do it right.

    Any ideas?
Sign In or Register to comment.