Skip to content
This repository has been archived by the owner on Aug 7, 2019. It is now read-only.

Commit

Permalink
Added a way to create a BPPolygon with an array of CLLocation objects…
Browse files Browse the repository at this point in the history
…, some tests
  • Loading branch information
hungtruong committed May 8, 2014
1 parent c52c2f3 commit fca5d89
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions BorderPatrol/BPPolygon.h
Expand Up @@ -13,6 +13,7 @@
@interface BPPolygon : NSObject

+ (BPPolygon *)polygonWithCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count;
+ (BPPolygon *)polygonWithLocations:(NSArray *)locations;

@property (nonatomic, readonly) CLLocationCoordinate2D *coordinates;
@property (nonatomic, readonly) NSUInteger coordinateCount;
Expand Down
17 changes: 14 additions & 3 deletions BorderPatrol/BPPolygon.m
Expand Up @@ -20,10 +20,21 @@ + (BPPolygon *)polygonWithCoordinates:(CLLocationCoordinate2D *)coordinates coun
return [[self alloc] initWithCoordinates:coordinates count:count];
}

+ (BPPolygon *)polygonWithLocations:(NSArray *)locations
{
int count = [locations count];
CLLocationCoordinate2D cArray[count];
for (int i = 0; i < count; i++){
cArray[i] = [[locations objectAtIndex:i] coordinate];
}

return [[self alloc] initWithCoordinates:cArray count:count];
}

- (id)initWithCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUInteger)count;
{
NSAssert(count > 2, @"Need more than two coordinates to make a polygon");

self = [super init];
if (!self) {
return nil;
Expand Down Expand Up @@ -55,7 +66,7 @@ - (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate;
if (_coordinates[index].longitude == coordinate.longitude && _coordinates[index].latitude == coordinate.latitude) {
return YES;
}

if (_coordinates[index].latitude < minLatitude) {
minLatitude = _coordinates[index].latitude;
}
Expand All @@ -72,7 +83,7 @@ - (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate;
if (coordinate.latitude < minLatitude || coordinate.latitude > maxLatitude || coordinate.longitude < minLongitude || coordinate.longitude > maxLongitude) {
return NO;
}

// Step 2: cast two rays in "random" directions
// For a ray going straight to the right, loop through each side;
// the coordinate lat must be between the points on the side
Expand Down
48 changes: 48 additions & 0 deletions BorderPatrolTests/BPPolygonTests.m
Expand Up @@ -12,6 +12,7 @@

@implementation BPPolygonTests {
BPPolygon *testPolygon;
BPPolygon *testPolygon2;
}

- (void)setUp;
Expand All @@ -21,14 +22,26 @@ - (void)setUp;
CLLocationCoordinate2DMake(10, 0),
CLLocationCoordinate2DMake(0, 10)
};

NSArray *locations = @[[[CLLocation alloc] initWithLatitude:-10 longitude:0],
[[CLLocation alloc] initWithLatitude:10 longitude:0],
[[CLLocation alloc] initWithLatitude:0 longitude:10]];

testPolygon = [BPPolygon polygonWithCoordinates:coordinates count:3];
testPolygon2 = [BPPolygon polygonWithLocations:locations];

}

- (void)testContainsCoordinateInPolygon;
{
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(0.5, 0.5)], @"Should contain coordinate");
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(0, 5)], @"Should contain coordinate");
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-1, 3)], @"Should contain coordinate");


STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(0.5, 0.5)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(0, 5)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-1, 3)], @"Should contain coordinate");
}

- (void)testContainsCoordinateOnSlopes;
Expand All @@ -39,13 +52,24 @@ - (void)testContainsCoordinateOnSlopes;
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(0, 0)], @"Should contain coordinate");
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(0.000001, 0.000001)], @"Should contain coordinate");
STAssertFalse([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-0.000001, -0.000001)], @"Should not contain coordinate");

STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(5, 5)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(4.999999, 4.999999)], @"Should contain coordinate");
STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(5.000001, 5.000001)], @"Should not contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(0, 0)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(0.000001, 0.000001)], @"Should contain coordinate");
STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-0.000001, -0.000001)], @"Should not contain coordinate");
}

- (void)testContainsCoordinateOnVertices;
{
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-10, 0)], @"Should contain coordinate");
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(0, 10)], @"Should contain coordinate");
STAssertTrue([testPolygon containsCoordinate:CLLocationCoordinate2DMake(10, 0)], @"Should contain coordinate");

STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-10, 0)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(0, 10)], @"Should contain coordinate");
STAssertTrue([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(10, 0)], @"Should contain coordinate");
}

- (void)testExcludesCoordinateOutsidePolygon;
Expand All @@ -54,6 +78,11 @@ - (void)testExcludesCoordinateOutsidePolygon;
STAssertFalse([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-5, 8)], @"Should not contain coordinate");
STAssertFalse([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-10, -1)], @"Should not contain coordinate");
STAssertFalse([testPolygon containsCoordinate:CLLocationCoordinate2DMake(-20, -20)], @"Should not contain coordinate");

STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(9, 5)], @"Should not contain coordinate");
STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-5, 8)], @"Should not contain coordinate");
STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-10, -1)], @"Should not contain coordinate");
STAssertFalse([testPolygon2 containsCoordinate:CLLocationCoordinate2DMake(-20, -20)], @"Should not contain coordinate");
}

- (void)testCoordinatesOnFunkyShapes;
Expand All @@ -70,10 +99,29 @@ - (void)testCoordinatesOnFunkyShapes;
};
BPPolygon *otherTestPolygon = [BPPolygon polygonWithCoordinates:coordinates count:8];

NSMutableArray *locations = [[NSMutableArray alloc] initWithCapacity:8];
for (int i = 0; i < 8; i++) {
CLLocationCoordinate2D coordinate = coordinates[i];
[locations addObject:[[CLLocation alloc] initWithCoordinate:coordinate
altitude:0
horizontalAccuracy:0
verticalAccuracy:0
course:0
speed:0
timestamp:0]];
}

BPPolygon *otherTestPolygon2 = [BPPolygon polygonWithLocations:locations];

STAssertTrue([otherTestPolygon containsCoordinate:CLLocationCoordinate2DMake(5, 5)], @"Should contain coordinate");
STAssertFalse([otherTestPolygon containsCoordinate:CLLocationCoordinate2DMake(15, -10)], @"Should not contain coordinate");
STAssertFalse([otherTestPolygon containsCoordinate:CLLocationCoordinate2DMake(5, -20)], @"Should not contain coordinate");
STAssertFalse([otherTestPolygon containsCoordinate:CLLocationCoordinate2DMake(20, -15)], @"Should not contain coordinate");

STAssertTrue([otherTestPolygon2 containsCoordinate:CLLocationCoordinate2DMake(5, 5)], @"Should contain coordinate");
STAssertFalse([otherTestPolygon2 containsCoordinate:CLLocationCoordinate2DMake(15, -10)], @"Should not contain coordinate");
STAssertFalse([otherTestPolygon2 containsCoordinate:CLLocationCoordinate2DMake(5, -20)], @"Should not contain coordinate");
STAssertFalse([otherTestPolygon2 containsCoordinate:CLLocationCoordinate2DMake(20, -15)], @"Should not contain coordinate");
}

@end

0 comments on commit fca5d89

Please sign in to comment.