Permalink
Browse files

Add hexVerts:intoArray: method

Add some method docs to the HexGrid header
  • Loading branch information...
1 parent d4b5032 commit ca2e24727f010d6c6b77ae039a6abbcf237a6baf @caseman committed Mar 11, 2012
View
BIN ...xcodeproj/project.xcworkspace/xcuserdata/casey.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
View
16 hexgrid/hexgrid.xcodeproj/xcuserdata/casey.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -2,20 +2,4 @@
<Bucket
type = "1"
version = "1.0">
- <FileBreakpoints>
- <FileBreakpoint
- shouldBeEnabled = "No"
- ignoreCount = "0"
- continueAfterRunningActions = "No"
- isPathRelative = "1"
- filePath = "hexgrid/hexgrid.h"
- timestampString = "353175956.50195"
- startingColumnNumber = "9223372036854775807"
- endingColumnNumber = "9223372036854775807"
- startingLineNumber = "19"
- endingLineNumber = "19"
- landmarkName = "-pointCart2Hex:"
- landmarkType = "4">
- </FileBreakpoint>
- </FileBreakpoints>
</Bucket>
View
14 hexgrid/hexgrid/hexgrid.h
@@ -8,15 +8,29 @@
#import <Foundation/Foundation.h>
+typedef CGPoint HGHexPoints[6];
+
@interface HexGrid : NSObject {
CGFloat r; // Radius hex center to edge
CGFloat s; // Radius hex center to vertex
}
@property (nonatomic, readwrite) CGFloat hexWidth;
+// Initialize the grid with the specified hex cell width
- (id)initWithHexWidth:(CGFloat)w;
+
+// Return the hex grid coordinate for the specified cartesian coordinate
- (CGPoint)pointCart2Hex:(CGPoint)cartPt;
+
+// Return the cartesian coordinate for the center point of the hex cell
+// at the specified hex cell coordinate
- (CGPoint)pointHex2Cart:(CGPoint)hexPt;
+// Set an array of 6 points to contain the cartesian coordinates
+// for the hex cell vertices at the hex cell coordinate specified.
+// The points are returned in clockwise order starting from the
+// "top" (max Y) vertex.
+- (void)hexVerts:(CGPoint)hexPt intoArray:(HGHexPoints)pts;
+
@end
View
11 hexgrid/hexgrid/hexgrid.m
@@ -69,4 +69,15 @@ - (CGPoint)pointHex2Cart:(CGPoint)hexPt
return cartPt;
}
+- (void)hexVerts:(CGPoint)hexPt intoArray:(HGHexPoints)pts
+{
+ CGPoint center = [self pointHex2Cart:hexPt];
+ pts[0].x = center.x; pts[0].y = center.y + s;
+ pts[1].x = center.x + r; pts[1].y = center.y + .5 * s;
+ pts[2].x = center.x + r; pts[2].y = center.y - .5 * s;
+ pts[3].x = center.x; pts[3].y = center.y - s;
+ pts[4].x = center.x - r; pts[4].y = center.y - .5 * s;
+ pts[5].x = center.x - r; pts[5].y = center.y + .5 * s;
+}
+
@end
View
47 hexgrid/hexgridTests/hexgridTests.m
@@ -109,11 +109,52 @@ - (void)testPointHex2CartUnitHexWidth
hp.x = 0; hp.y = 2;
cp = [grid pointHex2Cart:hp];
STAssertEquals(cp.x, (CGFloat)1, @"expected x=1");
- STAssertEqualsWithAccuracy(cp.y, (CGFloat)(3 / sqrt(3)), .00001f, @"expected y=0");
+ STAssertEqualsWithAccuracy(cp.y, (CGFloat)(3 / sqrt(3)), .00001f, nil);
hp.x = 20; hp.y = -4;
cp = [grid pointHex2Cart:hp];
- STAssertEquals(cp.x, (CGFloat)18, @"expected x=1");
- STAssertEqualsWithAccuracy(cp.y, (CGFloat)(-6 / sqrt(3)), .00001f, @"expected y=0");
+ STAssertEquals(cp.x, (CGFloat)18, @"expected x=18");
+ STAssertEqualsWithAccuracy(cp.y, (CGFloat)(-6 / sqrt(3)), .00001f, nil);
+
+ [grid release];
+}
+
+- (void)testHexVertsIntoArray
+{
+ HGHexPoints v;
+ HexGrid* grid = [[HexGrid alloc] init];
+ CGPoint hp;
+ CGFloat r = 1. / sqrt(3);
+
+ hp.x = 0; hp.y = 0;
+ [grid hexVerts:hp intoArray:v];
+ STAssertEquals(v[0].x, (CGFloat)0, nil);
+ STAssertEqualsWithAccuracy(v[0].y, (CGFloat)r, .00001f, nil);
+ STAssertEquals(v[1].x, (CGFloat).5, nil);
+ STAssertEqualsWithAccuracy(v[1].y, (CGFloat)r * .5, .00001f, nil);
+ STAssertEquals(v[2].x, (CGFloat).5, nil);
+ STAssertEqualsWithAccuracy(v[2].y, (CGFloat)-r * .5, .00001f, nil);
+ STAssertEquals(v[3].x, (CGFloat)0, nil);
+ STAssertEqualsWithAccuracy(v[3].y, (CGFloat)-r, .00001f, nil);
+ STAssertEquals(v[4].x, (CGFloat)-.5, nil);
+ STAssertEqualsWithAccuracy(v[4].y, (CGFloat)-r * .5, .00001f, nil);
+ STAssertEquals(v[5].x, (CGFloat)-.5, nil);
+ STAssertEqualsWithAccuracy(v[5].y, (CGFloat)r * .5, .00001f, nil);
+
+ CGFloat cy = -6 / sqrt(3);
+ hp.x = 20; hp.y = -4;
+ [grid hexVerts:hp intoArray:v];
+ STAssertEquals(v[0].x, (CGFloat)18, nil);
+ STAssertEqualsWithAccuracy(v[0].y, (CGFloat)r + cy, .00001f, nil);
+ STAssertEquals(v[1].x, (CGFloat)18.5, nil);
+ STAssertEqualsWithAccuracy(v[1].y, (CGFloat)r * .5 + cy, .00001f, nil);
+ STAssertEquals(v[2].x, (CGFloat)18.5, nil);
+ STAssertEqualsWithAccuracy(v[2].y, (CGFloat)-r * .5 + cy, .00001f, nil);
+ STAssertEquals(v[3].x, (CGFloat)18, nil);
+ STAssertEqualsWithAccuracy(v[3].y, (CGFloat)-r + cy, .00001f, nil);
+ STAssertEquals(v[4].x, (CGFloat)17.5, nil);
+ STAssertEqualsWithAccuracy(v[4].y, (CGFloat)-r * .5 + cy, .00001f, nil);
+ STAssertEquals(v[5].x, (CGFloat)17.5, nil);
+ STAssertEqualsWithAccuracy(v[5].y, (CGFloat)r * .5 + cy, .00001f, nil);
}
@end

0 comments on commit ca2e247

Please sign in to comment.