0

I'm trying to make a location based application and I've been trying to debug it for 3 days and still can't figure out whats wrong...In my view controller I am being passed some location points(latitude and longitude) by the delegate method:

- (void)locationManager:(CLLocationManager *)manager    didUpdateToLocation:(CLLocation *)newLocation           fromLocation:(CLLocation *)oldLocation{
    currentPoint.lat=newLocation.coordinate.latitude*M_PI/180;
    currentPoint.longit=newLocation.coordinate.longitude*M_PI/180;
}

where currentPoint is an object from the class Pointxy I have created that is nothing more than an object that has two properties lat and longit both floats.

I have then created another class called Shape that has an NSMutableArray (ptArray) and a property called area which is a long double.

I then store a bunch of pointy objects in ptArray and then preform use them in the following Shape method:

-(void)CalculateArea{
for (int i=0; i<[self NumberofPoints]-1; i++) {
    Pointxy *pointi=[ptArray objectAtIndex:i];
    Pointxy *pointiplus1=[ptArray objectAtIndex:i+1];
    area-=6371.0*6371.0/2.0*(pointiplus1.longit-pointi.longit)*(2+sinf(pointiplus1.lat)+sinf(pointi.lat));
}
Pointxy *pointzero=[ptArray objectAtIndex:0];
Pointxy *pointlast=[ptArray objectAtIndex:[ptArray count]-1];

area-=6371.0*6371.0/2.0*(pointzero.longit-pointlast.longit)*(2+sinf(pointzero.lat)+sinf(pointlast.lat));
area=area*1000000.0;
    }

The problem I am having is that I then try to display area in an UILabel using this code:

NSString *AreaString=[[NSString alloc]initWithFormat:@"%Lf",MyShape.area];
    [AreaLabel setText:AreaString];

where MyShape is a Shape object and AreaLabel is an UILabel, I am always getting 0.0000 displayed and I shouldn't can anyone see why? I guess it is maybe a matter of precision with my variables is it? Thank you very much

Mppl
  • 941
  • 10
  • 18
  • The coordinates are in degrees, no need to convert them from radians. Have you tried printing the coordinates you get using NSLog? Have you unit tested your CalculateArea function with known good data? – progrmr Jan 04 '12 at 21:14
  • I havent tested it but my source is a paper from nasa: http://trs-new.jpl.nasa.gov/dspace/bitstream/2014/40409/1/07-03.pdf so the should be no mistakes in it at all. Anyway even if there was same mistake how could it return always zero? I converted it to radians to use it in the sinf() function...Yes I actually have a Label that shows me in real time the coordinates that are stored in the array thank you. – Mppl Jan 04 '12 at 22:26
  • @user611295: The NASA paper may not have any mistakes but it's your _implementation_ of the algorithm that needs to be checked and tested. In addition, in Objective-C, there are lots of ways answers could come out zero even if the area algorithm code was correct. If some of the objects you're using have not been alloc+init-ed, they will be nil resulting in no-ops. Examples: If MyShape is nil, MyShape.area will return zero. If ptArray is nil or empty, you will get zero. Etc. As progrmr suggests, you need to add logging and do unit testing. –  Jan 05 '12 at 03:05
  • I am completly sure that the Array is not empty for I have a label that prints [ptarray count]; I am also completly sure that the pointxy objects stored in it are indeed valid objects for I have a lable that after adding a point shows me the last point lat and longit. Anyway If my array or my shape were nil objects my app would crash when calling the CalculateArea method which it doesn't . Thank you – Mppl Jan 05 '12 at 07:38
  • 1
    It may not be your issue but just be aware that in Objective-C, calling a method on a nil object does not result in a crash. See http://stackoverflow.com/questions/156395/sending-a-message-to-nil. –  Jan 05 '12 at 13:31

1 Answers1

0

I've just figured out my mistake. I was calling [ptArray addObject:currentPoint]; where currentPoint is an pointxy instance.

The thing was after that I was assigning a new value to currentPoint.lat and longit but since the addObject method passes the Object by reference when I changed the values the values were changed in the array object too and then I was adding currentPoint another time but since it was being passed by reference I had now at my array a set of equal points and the the area inside the figure inside those points was indeed zero!

stema
  • 90,351
  • 20
  • 107
  • 135
Mppl
  • 941
  • 10
  • 18