From 12de77eb42112159c47e6f69b996a6fd825c5bb0 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Tue, 12 Apr 2011 14:18:00 +0200 Subject: [PATCH] Path.Constructors: implement Path.Star and add tests. --- src/path/Path.Constructors.js | 20 ++++++++++++++++++++ test/tests/Path_Shapes.js | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/path/Path.Constructors.js b/src/path/Path.Constructors.js index d442aa8b..6bb12e65 100644 --- a/src/path/Path.Constructors.js +++ b/src/path/Path.Constructors.js @@ -125,6 +125,26 @@ Path.inject({ statics: new function() { } path.closed = true; return path; + }, + + Star: function(center, numPoints, radius1, radius2) { + center = Point.read(arguments, 0); + numPoints *= 2; + var inner = Math.min(radius1, radius2), + outer = Math.max(radius1, radius2), + angle = 360 / numPoints, + outerFirst = radius1 < radius2, + path = new Path(); + for (var i = 0; i < numPoints; i++) { + var radius = i % 2 == outerFirst ? outer : inner, + vector = new Point({ + angle: -90 + angle * i, + length: radius + }); + path.add(center.add(vector)); + } + path.closed = true; + return path; } }; }}); diff --git a/test/tests/Path_Shapes.js b/test/tests/Path_Shapes.js index 4da5510a..7eca7db1 100644 --- a/test/tests/Path_Shapes.js +++ b/test/tests/Path_Shapes.js @@ -48,4 +48,16 @@ test('new Path.RegularPolygon(center, numSides, radius)', function() { var path = new Path.RegularPolygon(new Point(250, 250), 10, 100); var expectedSegments = [{ point: { x: 219.09814, y: 345.10547 } }, { point: { x: 169.09814, y: 308.77832 } }, { point: { x: 150, y: 250 } }, { point: { x: 169.09814, y: 191.22168 } }, { point: { x: 219.09814, y: 154.89453 } }, { point: { x: 280.90186, y: 154.89453 } }, { point: { x: 330.90186, y: 191.22168 } }, { point: { x: 350, y: 250 } }, { point: { x: 330.90186, y: 308.77832 } }, { point: { x: 280.90186, y: 345.10547 } }]; compareSegmentLists(path.segments, expectedSegments); +}); + +test('new Path.Star(center, numSides, radius1, radius2)', function() { + var doc = new Document(); + var path = new Path.Star(new Point(100, 100), 10, 10, 20); + var expectedSegments = [new Segment(new Point(100, 90)), new Segment(new Point(106.18017578125, 80.97900390625)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(116.18017578125, 88.244140625)), new Segment(new Point(109.5107421875, 96.90966796875)), new Segment(new Point(120, 100)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(116.18017578125, 111.755859375)), new Segment(new Point(105.8779296875, 108.09033203125)), new Segment(new Point(106.18017578125, 119.02099609375)), new Segment(new Point(100, 110)), new Segment(new Point(93.81982421875, 119.02099609375)), new Segment(new Point(94.1220703125, 108.09033203125)), new Segment(new Point(83.81982421875, 111.755859375)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80, 100)), new Segment(new Point(90.4892578125, 96.90966796875)), new Segment(new Point(83.81982421875, 88.244140625)), new Segment(new Point(94.1220703125, 91.90966796875)), new Segment(new Point(93.81982421875, 80.97900390625))]; + compareSegmentLists(path.segments, expectedSegments); + + var doc = new Document(); + var path = new Path.Star(new Point(100, 100), 5, 20, 10); + var expectedSegments = [new Segment(new Point(100, 80)), new Segment(new Point(105.8779296875, 91.90966796875)), new Segment(new Point(119.02099609375, 93.81982421875)), new Segment(new Point(109.5107421875, 103.09033203125)), new Segment(new Point(111.755859375, 116.18017578125)), new Segment(new Point(100, 110)), new Segment(new Point(88.244140625, 116.18017578125)), new Segment(new Point(90.4892578125, 103.09033203125)), new Segment(new Point(80.97900390625, 93.81982421875)), new Segment(new Point(94.1220703125, 91.90966796875))]; + compareSegmentLists(path.segments, expectedSegments); }); \ No newline at end of file