iphone - Finding and returning the distance between two coordinates -
i'm sort of newbie--please don't hate.
the method compiles, i'm not sure how retrieve float value (i.e. distance between 2 points) method returns (or should return rather).
-(float)finddistancebetween:(coordinate *)a and:(coordinate *)b { //distance formula: //sqrt( (x2 - x1)^2 + (y2 - y1)^2 ) float resultdistance; resultdistance = sqrt( pow((b.latitude - a.latitude), 2) + pow((b.longitude - a.longitude), 2)); return resultdistance; } //somewhere else... float thedistancebetween; //below incorrect: thedistancebetween = [finddistancebetween: location1 and: location2];
thanks
is finddistancebetween:and:
defined in @implementation
block? code should like
// in .h file @interface myclass : nsobject // declaration of method - (float)finddistanceinbetween:(coordinate *)a and:(coordinate *)b; @end // in .m file @implementation myclass // definition of method - (float)finddistancebetween:(coordinate *)a and:(coordinate *)b { return sqrt(powf(a.x - b.x, 2.f) + powf(a.y - b.y, 2.f)); } @end // somewhere else needs calculate difference: // (assume coord_a , coord_b exist) // create instance of myclass myclass *myinstance = [[myclass alloc] init]; // send `finddistancebetween:and:` message instance float distance = [myinstance finddistancebetween:coord_a and:coord_b]; // when you're done instance, need clean [myinstance release];
it may make more sense put these types of methods on coordinate class can like:
coordinate *coord_a = <get coordinate somewhere>; coordinate *coord_b = <get coordinate somewhere>; float distance = [coord_a distancefrom:coord_b]; float angle = [coord_a angleto:coord_b];
don't worry hating, new @ once. :)
Comments
Post a Comment