summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Merry <alex.merry@cs.ox.ac.uk>2012-05-24 12:36:48 +0100
committerAlex Merry <alex.merry@cs.ox.ac.uk>2012-05-24 12:36:48 +0100
commit7edfc757be29bd29111b898dfc87fb1b258920a3 (patch)
tree9a8f2ab2f285999316570236df321baaf39324da
parent5dcbea7d268bf1be9548226665bce6a24e2e9d55 (diff)
Fix RegularPolyShape rotation to match TikZ
In PGF/TikZ, regular polygons with rotation 0 have a side flat at the bottom. RegularPolyShape now does the same, and also takes its rotation in degrees.
-rw-r--r--tikzit/src/common/RegularPolyShape.h18
-rw-r--r--tikzit/src/common/RegularPolyShape.m38
-rw-r--r--tikzit/src/common/Shape.m4
-rw-r--r--tikzit/src/common/util.h5
-rw-r--r--tikzit/src/common/util.m4
5 files changed, 48 insertions, 21 deletions
diff --git a/tikzit/src/common/RegularPolyShape.h b/tikzit/src/common/RegularPolyShape.h
index 663561e..1fd8f1e 100644
--- a/tikzit/src/common/RegularPolyShape.h
+++ b/tikzit/src/common/RegularPolyShape.h
@@ -24,10 +24,26 @@
#import <Foundation/Foundation.h>
#import "Shape.h"
+/**
+ * A regular polygon
+ *
+ * Matches the "regular polygon" shape in the shapes.geometric
+ * PGF/TikZ library.
+ */
@interface RegularPolyShape : Shape {
}
-- (id)initWithSides:(int)s rotation:(float)r;
+/**
+ * Initialise a regular polygon
+ *
+ * A rotation of 0 will produce a polygon with one
+ * edge flat along the bottom (just like PGF/TikZ
+ * does it).
+ *
+ * @param sides the number of sides the polygon should have
+ * @param rotation the rotation of the polygon, in degrees
+ */
+- (id)initWithSides:(int)sides rotation:(int)rotation;
@end
diff --git a/tikzit/src/common/RegularPolyShape.m b/tikzit/src/common/RegularPolyShape.m
index e5aac7a..b500377 100644
--- a/tikzit/src/common/RegularPolyShape.m
+++ b/tikzit/src/common/RegularPolyShape.m
@@ -2,8 +2,9 @@
// RegularPolyShape.m
// TikZiT
//
-// Copyright 2011 Aleks Kissinger. All rights reserved.
-//
+// Copyright 2011 Aleks Kissinger
+// Copyright 2012 Alex Merry
+// All rights reserved.
//
// This file is part of TikZiT.
//
@@ -28,32 +29,34 @@
@implementation RegularPolyShape
-- (id)initWithSides:(int)sides rotation:(float)rotation {
- [super init];
-
- float rad = 0.25f;
-
+- (id)initWithSides:(int)sides rotation:(int)rotation {
+ self = [super init];
+ if (self == nil)
+ return nil;
+
+ float radius = 0.25f;
+
NSMutableArray *nodes = [NSMutableArray arrayWithCapacity:sides];
NSMutableArray *edges = [NSMutableArray arrayWithCapacity:sides];
-
+
float dtheta = (M_PI * 2.0f) / ((float)sides);
- float theta = rotation;
- int i;
+ float theta = (dtheta/2.0f) - (M_PI / 2.0f);
+ theta += degreesToRadians(rotation);
float maxY=0.0f, minY=0.0f;
NSPoint p;
- for (i = 0; i < sides; ++i) {
- p.x = rad * cos(theta);
- p.y = rad * sin(theta);
+ for (int i = 0; i < sides; ++i) {
+ p.x = radius * cos(theta);
+ p.y = radius * sin(theta);
if (p.y<minY) minY = p.y;
if (p.y>maxY) maxY = p.y;
[nodes addObject:[Node nodeWithPoint:p]];
theta += dtheta;
}
-
+
float dy = (minY + maxY) / 2.0f;
-
- for (i = 0; i < sides; ++i) {
+
+ for (int i = 0; i < sides; ++i) {
p = [[nodes objectAtIndex:i] point];
p.y -= dy;
[[nodes objectAtIndex:i] setPoint:p];
@@ -63,10 +66,9 @@
paths = [[NSSet alloc] initWithObjects:edges,nil];
- int degRot = (int)radiansToDegrees(rotation);
styleTikz = [[NSString alloc] initWithFormat:
@"regular polygon,regular polygon sides=%d,shape border rotate=%d",
- sides, degRot];
+ sides, rotation];
return self;
}
diff --git a/tikzit/src/common/Shape.m b/tikzit/src/common/Shape.m
index b74a9fc..0f1b35b 100644
--- a/tikzit/src/common/Shape.m
+++ b/tikzit/src/common/Shape.m
@@ -110,8 +110,8 @@ NSDictionary *shapeDictionary = nil;
[[CircleShape alloc] init],
[[RectangleShape alloc] init],
[[DiamondShape alloc] init],
- [[RegularPolyShape alloc] initWithSides:3 rotation:(M_PI/2.0f)],
- [[RegularPolyShape alloc] initWithSides:3 rotation:(-M_PI/2.0f)]};
+ [[RegularPolyShape alloc] initWithSides:3 rotation:0],
+ [[RegularPolyShape alloc] initWithSides:3 rotation:180]};
NSMutableDictionary *shapeDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
shapes[0], SHAPE_CIRCLE,
shapes[1], SHAPE_RECTANGLE,
diff --git a/tikzit/src/common/util.h b/tikzit/src/common/util.h
index 809e8ea..d868903 100644
--- a/tikzit/src/common/util.h
+++ b/tikzit/src/common/util.h
@@ -160,6 +160,11 @@ float roundToNearest(float stepSize, float val);
float radiansToDegrees(float radians);
/*!
+ @brief Convert degrees into radians
+ */
+float degreesToRadians(float degrees);
+
+/*!
@brief Normalises an angle (in degrees) to fall between -179 and 180
*/
int normaliseAngleDeg (int degrees);
diff --git a/tikzit/src/common/util.m b/tikzit/src/common/util.m
index ad04a96..762516f 100644
--- a/tikzit/src/common/util.m
+++ b/tikzit/src/common/util.m
@@ -310,6 +310,10 @@ float radiansToDegrees (float radians) {
return (radians * 180.0f) / M_PI;
}
+float degreesToRadians(float degrees) {
+ return (degrees * M_PI) / 180.0f;
+}
+
int normaliseAngleDeg (int degrees) {
while (degrees > 180) {
degrees -= 360;