Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
runemadsen committed Mar 28, 2016
1 parent fd6dad3 commit 777a9f7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.18

- `Rune.Polygon` method `centroid()` now calculates centroid as closed shape.

## 0.2.17

- Publish failed. Republishing.
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": "rune.js",
"version" : "0.2.17",
"version" : "0.2.18",
"description": "A JavaScript library for programming graphic design systems with SVG",
"repository": {
"type": "git",
Expand Down
25 changes: 16 additions & 9 deletions src/shapes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class Polygon {
return this.vectorAtLength(this.length() * scalar);
}

area() {
var area = 0;
for(var i = 0; i < this.vars.vectors.length-1; i++)
{
area += this.vars.vectors[i].x * this.vars.vectors[i+1].y - this.vars.vectors[i+1].x * this.vars.vectors[i].y;
}
area /= 2;
return Math.abs(area);
}

bounds() {
var xmax = undefined;
var ymax = undefined;
Expand All @@ -78,22 +88,19 @@ class Polygon {
}

centroid() {
var ps = this.vars.vectors;
var areaAcc = 0.0;
var xAcc = 0.0;
var yAcc = 0.0;

for(var i = 0; i < ps.length-1; i++)
{
areaAcc += ps[i].x * ps[i+1].y - ps[i+1].x * ps[i].y;
xAcc += (ps[i].x + ps[i+1].x) * (ps[i].x * ps[i+1].y - ps[i+1].x * ps[i].y);
yAcc += (ps[i].y + ps[i+1].y) * (ps[i].x * ps[i+1].y - ps[i+1].x * ps[i].y);
for(var i = 0; i < this.vars.vectors.length; i++) {
var start = this.vars.vectors[i];
var stop = this.vars.vectors[(i+1)%this.vars.vectors.length];
areaAcc += start.x * stop.y - stop.x * start.y;
xAcc += (start.x + stop.x) * (start.x * stop.y - stop.x * start.y);
yAcc += (start.y + stop.y) * (start.x * stop.y - stop.x * start.y);
}

areaAcc /= 2.0;
var x = xAcc/(6.0*areaAcc);
var y = yAcc/(6.0*areaAcc);

return new Vector(x, y);
}

Expand Down
7 changes: 7 additions & 0 deletions test/shared/shapes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ describe("Rune.Polygon", function() {
});
});

describe("area()", function() {
it("should return area of polygon", function() {
var area = s.area();
expect(area).toEqual(3600);
});
});

describe("bounds()", function() {

it("should return internal bounds", function() {
Expand Down

0 comments on commit 777a9f7

Please sign in to comment.