From eebe7e2731bff81c12fcc73f69efa340941ed01a Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Sat, 26 Feb 2011 19:19:02 +0100 Subject: [PATCH] Implement Path.RegularPolygon constructor and add tests for it. --- src/path/Path.Constructors.js | 13 +++++++++++++ test/tests/Path_Shapes.js | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/path/Path.Constructors.js b/src/path/Path.Constructors.js index 30c44e7e..f0b0e137 100644 --- a/src/path/Path.Constructors.js +++ b/src/path/Path.Constructors.js @@ -102,6 +102,19 @@ Path.inject({ statics: new function() { path.moveTo(from); path.arcTo(through, to); return path; + }, + + RegularPolygon: function(center, numSides, radius) { + center = new Point(center); + var path = new Path(); + var three = !(numSides % 3); + var vector = new Point(0, three ? -radius : radius); + for(var i = 0; i < numSides; i++) { + var angle = (360 / numSides) * (i + (three ? -1 : 0.5)); + path.add(center.add(vector.rotate(angle))); + } + path.closed = true; + return path; } }; }}); diff --git a/test/tests/Path_Shapes.js b/test/tests/Path_Shapes.js index d5fe274c..c403bb19 100644 --- a/test/tests/Path_Shapes.js +++ b/test/tests/Path_Shapes.js @@ -37,4 +37,15 @@ test('new Path.Arc(from, through, to)', function() { var path = new Path.Arc([50, 50], [100, 100], [75, 75]); var expectedSegments = [{ point: { x: 50, y: 50 }, handleOut: { x: 10.11156, y: -10.11156 } }, { point: { x: 88.5299, y: 42.33593 }, handleIn: { x: -13.21138, y: -5.47233 }, handleOut: { x: 13.21138, y: 5.47233 } }, { point: { x: 110.35534, y: 75 }, handleIn: { x: 0, y: -14.2999 } }]; compareSegmentLists(path.segments, expectedSegments); +}); + +test('new Path.RegularPolygon(center, numSides, radius)', function() { + var doc = new Doc(); + var path = new Path.RegularPolygon(new Point(50, 50), 3, 10); + var expectedSegments = [{ point: { x: 41.33984, y: 55 } }, { point: { x: 50, y: 40 } }, { point: { x: 58.66016, y: 55 } }]; + compareSegmentLists(path.segments, expectedSegments); + + 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); }); \ No newline at end of file