开发者

getting min and max values

开发者 https://www.devze.com 2023-04-08 22:15 出处:网络
so i have an array of custom objects. i want to go through them to find the maxx, maxy, minx, and miny values. i need the largest of the maxes and smallest of the mins.the following code makes complet

so i have an array of custom objects. i want to go through them to find the maxx, maxy, minx, and miny values. i need the largest of the maxes and smallest of the mins. the following code makes complete sense to me, but i tend to get a random value from the list, my maxes do not get the max or min, and the mins do not get the min or the max.

so, i take the first values from the first object in the array, i then compare each objects values to my current maxx, maxy, minx, miny to see if the object's values are larger or less then, and if so, assign it:

        claimCenterBoundary = [dataCenter.claimCenterBoundaryList objectAtIndex:0];
        maxx = claimCenterBoundary.maxx;
        maxy = claimCenterBoundary.maxy;
        minx = claimCenterBoundary.minx;
        miny = claimCenterBoundary.miny;

        for (int i = 0; i < count; i++) 
        {
            claimCenterBoundary = [dataCenter.claimCenterBoundaryList objectAtIndex:i];
            NSLog(@"minx: %@ miny: %@ maxx: %@ maxy: %@", claimCenterBoundary.minx, claimCenterBoundary.miny, claimCenterBoundary.maxx, claimCenterBoundary.maxy);

            if (maxx < claimCenterBoundary.maxx)
                maxx = claimCenterBoundary.maxx;

            if (maxy < claimCenterBoundary.maxy)
                maxy = claimCenterBoundary.maxy;

            if (minx > claimCenterBoundary.minx)
                minx = claimCenterBoundary.minx;

            if (miny > claimCenterBoundary.miny)
                miny = claimCenterBoundary.miny;
        }

here is my output:

count: 8
minx: -98.9139404296875 miny: 48.51737594604492 maxx: -98.90547943115234 maxy: 48.52248382568359
minx: -98.9139404296875 miny: 48.51737594604492 maxx: -98.90547943115开发者_开发知识库234 maxy: 48.52248382568359
minx: -98.92726898193359 miny: 48.51534652709961 maxx: -98.91975402832031 maxy: 48.52249908447266
minx: -98.92726898193359 miny: 48.51534652709961 maxx: -98.91975402832031 maxy: 48.52249908447266
minx: -98.92340850830078 miny: 48.51531219482422 maxx: -98.91383361816406 maxy: 48.52248382568359
minx: -98.92340850830078 miny: 48.51531219482422 maxx: -98.91383361816406 maxy: 48.52248382568359
minx: -98.909423828125 miny: 48.51529693603516 maxx: -98.90548706054688 maxy: 48.51742553710938
minx: -98.96006774902344 miny: 48.51530075073242 maxx: -98.94926452636719 maxy: 48.52977752685547
final minx :-98.92726898193359 miny: 48.51531219482422 maxx: -98.94926452636719 maxy: 48.52977752685547

i can not figure out why this code would not work.


This line...

NSLog(@"minx: %@ miny: %@ maxx: %@ maxy: %@", claimCenterBoundary.minx, claimCenterBoundary.miny, claimCenterBoundary.maxx, claimCenterBoundary.maxy);

... implies that the return values of -minx, -miny, -maxx, -maxy are all objects, and most likely NSNumber objects.

If that's the case, then you can't use < and > to compare two NSNumbers. You'd be doing pointer comparison, which is most definitely not what you want.

So what you could do instead is this:

NSArray *objects = dataCenter.claimCenterBoundaryList;
NSNumber *minx = [objects valueForKeyPath:@"@min.minx"];
NSNumber *miny = [objects valueForKeyPath:@"@min.miny"];
NSNumber *maxx = [objects valueForKeyPath:@"@min.maxx"];
NSNumber *maxy = [objects valueForKeyPath:@"@min.maxy"];

This is technically 4 times less efficient than your original proposal, since you're going to iterate the entire list 4 times instead of once, but if your list is a reasonable size, then the difference is probably negligible.

0

精彩评论

暂无评论...
验证码 换一张
取 消