Skip to content

Commit

Permalink
- Fix bug in arc creation, do not allow arc start/end to be out of 2*PI
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbol99 committed Apr 27, 2019
1 parent 37deedb commit 4615b77
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
15 changes: 14 additions & 1 deletion dist/main.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ function arcStartSweep(center, start, sweep, counterClockwise) {
endAngle += 2*Math.PI;
counterClockwise = true;
}
else if (core.Utils.GT(endAngle, 2*Math.PI)) {
endAngle -= 2*Math.PI;
}
else if (core.Utils.LT(endAngle, -2*Math.PI)) {
endAngle += 2*Math.PI;
}
let r = core.vector(center, start).length;

return new core.Arc(center, r, startAngle, endAngle, counterClockwise);
Expand All @@ -734,9 +740,16 @@ function arcEndSweep(center, end, sweep, counterClockwise) {
let endAngle = core.vector(center,end).slope;
let startAngle = endAngle - sweep;
if (core.Utils.EQ(startAngle, endAngle)) {
endAngle += 2*Math.PI;
startAngle += 2*Math.PI;
counterClockwise = true;
}
else if (core.Utils.GT(startAngle, 2*Math.PI)) {
startAngle -= 2*Math.PI;
}
else if (core.Utils.LT(startAngle, -2*Math.PI)) {
startAngle += 2*Math.PI;
}

let r = core.vector(center, end).length;

return new core.Arc(center, r, startAngle, endAngle, counterClockwise);
Expand Down
15 changes: 14 additions & 1 deletion dist/main.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,12 @@ function arcStartSweep(center, start, sweep, counterClockwise) {
endAngle += 2*Math.PI;
counterClockwise = true;
}
else if (Utils.GT(endAngle, 2*Math.PI)) {
endAngle -= 2*Math.PI;
}
else if (Utils.LT(endAngle, -2*Math.PI)) {
endAngle += 2*Math.PI;
}
let r = vector(center, start).length;

return new Arc(center, r, startAngle, endAngle, counterClockwise);
Expand All @@ -730,9 +736,16 @@ function arcEndSweep(center, end, sweep, counterClockwise) {
let endAngle = vector(center,end).slope;
let startAngle = endAngle - sweep;
if (Utils.EQ(startAngle, endAngle)) {
endAngle += 2*Math.PI;
startAngle += 2*Math.PI;
counterClockwise = true;
}
else if (Utils.GT(startAngle, 2*Math.PI)) {
startAngle -= 2*Math.PI;
}
else if (Utils.LT(startAngle, -2*Math.PI)) {
startAngle += 2*Math.PI;
}

let r = vector(center, end).length;

return new Arc(center, r, startAngle, endAngle, counterClockwise);
Expand Down
15 changes: 14 additions & 1 deletion dist/main.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@
endAngle += 2*Math.PI;
counterClockwise = true;
}
else if (core.Utils.GT(endAngle, 2*Math.PI)) {
endAngle -= 2*Math.PI;
}
else if (core.Utils.LT(endAngle, -2*Math.PI)) {
endAngle += 2*Math.PI;
}
let r = core.vector(center, start).length;

return new core.Arc(center, r, startAngle, endAngle, counterClockwise);
Expand All @@ -734,9 +740,16 @@
let endAngle = core.vector(center,end).slope;
let startAngle = endAngle - sweep;
if (core.Utils.EQ(startAngle, endAngle)) {
endAngle += 2*Math.PI;
startAngle += 2*Math.PI;
counterClockwise = true;
}
else if (core.Utils.GT(startAngle, 2*Math.PI)) {
startAngle -= 2*Math.PI;
}
else if (core.Utils.LT(startAngle, -2*Math.PI)) {
startAngle += 2*Math.PI;
}

let r = core.vector(center, end).length;

return new core.Arc(center, r, startAngle, endAngle, counterClockwise);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flatten-js/polygon-offset",
"version": "1.0.3",
"version": "1.0.4",
"description": "Polygon equidistant offset",
"main": "dist/main.cjs.js",
"umd:main": "dist/main.umd.js",
Expand Down
15 changes: 14 additions & 1 deletion src/createArcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export function arcStartSweep(center, start, sweep, counterClockwise) {
endAngle += 2*Math.PI;
counterClockwise = true;
}
else if (Utils.GT(endAngle, 2*Math.PI)) {
endAngle -= 2*Math.PI;
}
else if (Utils.LT(endAngle, -2*Math.PI)) {
endAngle += 2*Math.PI;
}
let r = vector(center, start).length;

return new Arc(center, r, startAngle, endAngle, counterClockwise);
Expand All @@ -28,9 +34,16 @@ export function arcEndSweep(center, end, sweep, counterClockwise) {
let endAngle = vector(center,end).slope;
let startAngle = endAngle - sweep;
if (Utils.EQ(startAngle, endAngle)) {
endAngle += 2*Math.PI;
startAngle += 2*Math.PI;
counterClockwise = true;
}
else if (Utils.GT(startAngle, 2*Math.PI)) {
startAngle -= 2*Math.PI;
}
else if (Utils.LT(startAngle, -2*Math.PI)) {
startAngle += 2*Math.PI;
}

let r = vector(center, end).length;

return new Arc(center, r, startAngle, endAngle, counterClockwise);
Expand Down
4 changes: 2 additions & 2 deletions src/polygonOffset.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function offset(polygon, value) {
return offsetPolygon;
}

function offsetArc(arc, value) {
export function offsetArc(arc, value) {
let edges = [];

let w = Math.abs(value);
Expand Down Expand Up @@ -160,7 +160,7 @@ function offsetArc(arc, value) {
return polygon;
}

function offsetSegment(seg, value) {
export function offsetSegment(seg, value) {
let w = Math.abs(value);

let polygon = new Polygon();
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {point, segment, arc, circle, CW, CCW} from "@flatten-js/core";

import {offset} from "../index.js";
import {arcSE} from "../src/createArcs";
import {offsetArc} from "../src/polygonOffset";

describe('#Algorithms.Offset Polygon', function () {
it('Function offset defined', function () {
Expand Down Expand Up @@ -201,4 +202,10 @@ describe('#Algorithms.Offset Polygon', function () {
expect([...offsetPolygon.faces][0].size).to.be.equal(5);
expect([...offsetPolygon.faces][1].size).to.be.equal(11);
});

it('Method offset arc can create legal polygon. Simple case', function () {
let testArc = arc(point(133434061, 124903644), 400000, 0, 4.71238898038469, CW );
let testOffsetArc = offsetArc(testArc, 20);
expect(testOffsetArc.faces.size).to.be.equal(1);
});
});

0 comments on commit 4615b77

Please sign in to comment.