diff --git a/README.md b/README.md index b106fac..477f8b4 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ A JavaScript library for doing geometry. [![Build Status](https://travis-ci.org/ ### Web browser In vanilla, a `geometric` global is exported. You can use the latest version from unpkg. ```html - - + + ``` If you'd rather host it yourself, download the latest release from the [`build` directory](https://github.com/HarryStevens/geometric/tree/master/build). @@ -28,7 +28,7 @@ const geometric = require("geometric"); Geometric.js uses the geometric primitives points, lines, and polygons. * [Points](#points) are represented as arrays of two numbers, such as [0, 0]. -* [Lines](#lines) are represented as arrays of two points, such as [[0, 0], [1, 0]]. Because they have endpoints, these are technically [line segments](https://www.mhschool.com/math/mathconnects/wa/assets/docs/394_397_wa_gr3_adllsn_onln.pdf), but Geometric.js refers to them as lines for simplicity's sake. +* [Lines](#lines) are represented as arrays of two points, such as [[0, 0], [1, 0]]. Because they have endpoints, these are technically [line segments](https://www.mhschool.com/math/mathconnects/wa/assets/docs/394_397_wa_gr3_adllsn_onln.pdf), but Geometric.js refers to them as lines for simplicity's sake. * [Polygons](#polygons) are represented as arrays of vertices, each of which is a point, such as [[0, 0], [1, 0], [1, 1], [0, 1]]. Polygons can be closed – the first and last vertex are the same – or open. * There are also functions to [calculate relationships](#relationships) between these primitives. @@ -40,143 +40,143 @@ You will also encounter angles, areas, distances, and le ### Points -# geometric.pointRotate(point, angle[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/points/pointRotate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointrotate "Example") +# geometric.pointRotate(point, angle[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/points/pointRotate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointrotate "Example") -Returns the coordinates resulting from rotating a point about an origin by an angle in degrees. If origin is not specified, the origin defaults to [0, 0]. +Returns the coordinates resulting from rotating a point about an origin by an angle in degrees. If origin is not specified, the origin defaults to [0, 0]. -# geometric.pointTranslate(point, angle, distance) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/points/pointTranslate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointtranslate "Example") +# geometric.pointTranslate(point, angle, distance) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/points/pointTranslate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointtranslate "Example") -Returns the coordinates resulting from translating a point by an angle in degrees and a distance. +Returns the coordinates resulting from translating a point by an angle in degrees and a distance.
### Lines -# geometric.lineAngle(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineAngle.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineangle "Example") +# geometric.lineAngle(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineAngle.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineangle "Example") -Returns the angle of a line, in degrees, with respect to the horizontal axis. +Returns the angle of a line, in degrees, with respect to the horizontal axis. -# geometric.lineInterpolate(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineInterpolate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineinterpolate "Example") +# geometric.lineInterpolate(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineInterpolate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineinterpolate "Example") Returns an interpolator function given a line [a, b]. The returned interpolator function takes a single argument t, where t is a number ranging from 0 to 1; a value of 0 returns a, while a value of 1 returns b. Intermediate values interpolate from a to b along the line segment. -# geometric.lineLength(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineLength.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-linelength "Example") +# geometric.lineLength(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineLength.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-linelength "Example") -Returns the length of a line. +Returns the length of a line. -# geometric.lineMidpoint(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineMidpoint.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-linemidpoint "Example") +# geometric.lineMidpoint(line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/lines/lineMidpoint.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-linemidpoint "Example") -Returns the midpoint of a line. +Returns the midpoint of a line.
### Polygons -# geometric.polygonArea(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonArea.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonarea "Example") +# geometric.polygonArea(polygon[, signed]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonArea.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonarea "Example") -Returns the area of a polygon. +Returns the area of a polygon. You can pass a boolean indicating whether the returned area is signed, which defaults to false. -# geometric.polygonBounds(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonBounds.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonbounds "Example") +# geometric.polygonBounds(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonBounds.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonbounds "Example") -Returns the bounds of a polygon, ignoring points with invalid values (null, undefined, NaN, Infinity). The returned bounds are represented as an array of two points, where the first point is the top-left corner and the second point is the bottom-right corner. For example: +Returns the bounds of a polygon, ignoring points with invalid values (null, undefined, NaN, Infinity). The returned bounds are represented as an array of two points, where the first point is the top-left corner and the second point is the bottom-right corner. For example: ```js const rectangle = [[0, 0], [0, 1], [1, 1], [1, 0]]; const bounds = geometric.polygonBounds(rectangle); // [[0, 0], [1, 1]] ``` -Returns null if the polygon has fewer than three points. +Returns null if the polygon has fewer than three points. -# geometric.polygonCentroid(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonCentroid.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoncentroid-geometric-polygonmean "Example") +# geometric.polygonCentroid(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonCentroid.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoncentroid-geometric-polygonmean "Example") -Returns the weighted centroid of a polygon. Not to be [confused](https://github.com/Turfjs/turf/issues/334) with a [mean center](#polygonMean). +Returns the weighted centroid of a polygon. Not to be [confused](https://github.com/Turfjs/turf/issues/334) with a [mean center](#polygonMean). -# geometric.polygonHull(points) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonHull.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonhull "Example") +# geometric.polygonHull(points) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonHull.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonhull "Example") -Returns the [convex hull](https://en.wikipedia.org/wiki/Convex_hull), represented as a polygon, for an array of points. Returns null if the input array has fewer than three points. Uses [Andrew’s monotone chain algorithm](https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#JavaScript). +Returns the [convex hull](https://en.wikipedia.org/wiki/Convex_hull), represented as a polygon, for an array of points. Returns null if the input array has fewer than three points. Uses [Andrew’s monotone chain algorithm](https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#JavaScript). -# geometric.polygonLength(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonLength.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonlength "Example") +# geometric.polygonLength(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonLength.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonlength "Example") -Returns the length of a polygon's perimeter. +Returns the length of a polygon's perimeter. -# geometric.polygonMean(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonMean.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoncentroid-geometric-polygonmean "Example") +# geometric.polygonMean(polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonMean.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoncentroid-geometric-polygonmean "Example") Returns the arithmetic mean of the vertices of a polygon. Keeps duplicate vertices, resulting in different values for open and closed polygons. Not to be [confused](https://github.com/Turfjs/turf/issues/334) with a [centroid](#polygonCentroid). -# geometric.polygonRegular([sides[, area[, center]]]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonRegular.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonregular "Example") +# geometric.polygonRegular([sides[, area[, center]]]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonRegular.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonregular "Example") -Returns the vertices of a regular polygon of the specified number of sides, area, and center coordinates. If sides is not specified, deafults to 3. If area is not specified, defaults to 100. If center is not specified, defaults to [0, 0]. +Returns the vertices of a regular polygon of the specified number of sides, area, and center coordinates. If sides is not specified, deafults to 3. If area is not specified, defaults to 100. If center is not specified, defaults to [0, 0]. -# geometric.polygonRotate(polygon, angle[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonRotate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonrotate "Example") +# geometric.polygonRotate(polygon, angle[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonRotate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonrotate "Example") -Returns the vertices resulting from rotating a polygon about an origin by an angle in degrees. If origin is not specified, the origin defaults to [0, 0]. +Returns the vertices resulting from rotating a polygon about an origin by an angle in degrees. If origin is not specified, the origin defaults to [0, 0]. -# geometric.polygonScale(polygon, scaleFactor[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonScale.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonscale "Example") +# geometric.polygonScale(polygon, scaleFactor[, origin]) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonScale.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonscale "Example") -Returns the vertices resulting from scaling a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. If origin is not specified, the origin defaults to the polygon's centroid. +Returns the vertices resulting from scaling a polygon by a scaleFactor (where 1 is the polygon's current size) from an origin point. If origin is not specified, the origin defaults to the polygon's centroid. -# geometric.polygonTranslate(polygon, angle, distance) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonTranslate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygontranslate "Example") +# geometric.polygonTranslate(polygon, angle, distance) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/polygons/polygonTranslate.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygontranslate "Example") -Returns the vertices resulting from translating a polygon by an angle in degrees and a distance. +Returns the vertices resulting from translating a polygon by an angle in degrees and a distance.
### Relationships -# geometric.lineIntersectsLine(lineA, lineB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/lineIntersectsLine.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineintersectsline "Example") +# geometric.lineIntersectsLine(lineA, lineB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/lineIntersectsLine.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineintersectsline "Example") -Returns a boolean representing whether lineA intersects lineB. +Returns a boolean representing whether lineA intersects lineB. -# geometric.lineIntersectsPolygon(line, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/lineIntersectsPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineintersectspolygon "Example") +# geometric.lineIntersectsPolygon(line, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/lineIntersectsPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-lineintersectspolygon "Example") -Returns a boolean representing whether a line intersects a polygon. +Returns a boolean representing whether a line intersects a polygon. -# geometric.pointInPolygon(point, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointInPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointinpolygon "Example") +# geometric.pointInPolygon(point, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointInPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointinpolygon "Example") -Returns a boolean representing whether a point is inside of a polygon. Uses [ray casting](https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm). +Returns a boolean representing whether a point is inside of a polygon. Uses [ray casting](https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm). -# geometric.pointOnPolygon(point, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonpolygon "Example") +# geometric.pointOnPolygon(point, polygon) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonpolygon "Example") -Returns a boolean representing whether a point is located on one of the edges of a polygon. +Returns a boolean representing whether a point is located on one of the edges of a polygon. -# geometric.pointOnLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L17 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonline-geometric-pointwithline "Example") +# geometric.pointOnLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L17 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonline-geometric-pointwithline "Example") -Returns a boolean representing whether a point is collinear with a line and is also located on the line segment. See also [pointWithLine](#pointWithLine). +Returns a boolean representing whether a point is collinear with a line and is also located on the line segment. See also [pointWithLine](#pointWithLine). [](https://observablehq.com/d/c463ce4b7cbcd048) -# geometric.pointWithLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L21 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonline-geometric-pointwithline "Example") +# geometric.pointWithLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L21 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointonline-geometric-pointwithline "Example") -Returns a boolean representing whether a point is collinear with a line. See also [pointOnLine](#pointOnLine). +Returns a boolean representing whether a point is collinear with a line. See also [pointOnLine](#pointOnLine). -# geometric.pointLeftofLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L9 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointleftofline-geometric-pointrightofline-g "Example") +# geometric.pointLeftofLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L9 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointleftofline-geometric-pointrightofline-g "Example") -Returns a boolean representing whether a point is to the left of a line. +Returns a boolean representing whether a point is to the left of a line. -# geometric.pointRightofLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L13 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointleftofline-geometric-pointrightofline-g "Example") +# geometric.pointRightofLine(point, line) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/pointOnLine.js#L13 "Source"), [Example](https://observablehq.com/@harrystevens/geometric-pointleftofline-geometric-pointrightofline-g "Example") -Returns a boolean representing whether a point is to the right of a line. +Returns a boolean representing whether a point is to the right of a line. -# geometric.polygonInPolygon(polygonA, polygonB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/polygonInPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoninpolygon "Example") +# geometric.polygonInPolygon(polygonA, polygonB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/polygonInPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygoninpolygon "Example") -Returns a boolean representing whether polygonA is contained by polygonB. +Returns a boolean representing whether polygonA is contained by polygonB. -# geometric.polygonIntersectsPolygon(polygonA, polygonB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/polygonIntersectsPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonintersectspolygon "Example") +# geometric.polygonIntersectsPolygon(polygonA, polygonB) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/relationships/polygonIntersectsPolygon.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-polygonintersectspolygon "Example") -Returns a boolean representing whether polygonA intersects but is not contained by polygonB. +Returns a boolean representing whether polygonA intersects but is not contained by polygonB.
### Angles -# geometric.angleReflect(incidence, surface) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleReflect.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-anglereflect "Example") +# geometric.angleReflect(incidence, surface) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleReflect.js "Source"), [Example](https://observablehq.com/@harrystevens/geometric-anglereflect "Example") -Returns the angle of reflection given a starting angle, also known as the angle of incidence, and the angle of the surface off of which it is reflected. +Returns the angle of reflection given a starting angle, also known as the angle of incidence, and the angle of the surface off of which it is reflected. -# geometric.angleToDegrees(angle) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleToDegrees.js "Source") +# geometric.angleToDegrees(angle) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleToDegrees.js "Source") -Returns the result of a converting an angle in radians to the same angle in degrees. +Returns the result of a converting an angle in radians to the same angle in degrees. -# geometric.angleToRadians(angle) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleToRadians.js "Source") +# geometric.angleToRadians(angle) · [Source](https://github.com/HarryStevens/geometric/blob/master/src/angles/angleToRadians.js "Source") -Returns the result of a converting an angle in degrees to the same angle in radians. \ No newline at end of file +Returns the result of a converting an angle in degrees to the same angle in radians. \ No newline at end of file diff --git a/build/geometric.js b/build/geometric.js index 103786b..2c50404 100644 --- a/build/geometric.js +++ b/build/geometric.js @@ -68,6 +68,7 @@ // Calculates the area of a polygon. function polygonArea(vertices) { + var signed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var a = 0; for (var i = 0, l = vertices.length; i < l; i++) { @@ -77,7 +78,7 @@ a -= v1[0] * v0[1]; } - return Math.abs(a / 2); + return signed ? a / 2 : Math.abs(a / 2); } // Calculates the bounds of a polygon. diff --git a/build/geometric.min.js b/build/geometric.min.js deleted file mode 100644 index b3dcd0f..0000000 --- a/build/geometric.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.geometric={})}(this,function(n){"use strict";function t(n){return 180*n/Math.PI}function r(n){return t(Math.atan2(n[1][1]-n[0][1],n[1][0]-n[0][0]))}function e(n){return Math.sqrt(Math.pow(n[1][0]-n[0][0],2)+Math.pow(n[1][1]-n[0][1],2))}function o(n){return n/180*Math.PI}function i(n,t,r){var e=o(t);return[n[0]+r*Math.cos(e),n[1]+r*Math.sin(e)]}function u(n){return function(t){return 0===t?n[0]:1===t?n[1]:i(n[0],r(n),e(n)*t)}}function a(n){return[(n[0][0]+n[1][0])/2,(n[0][1]+n[1][1])/2]}function f(n,t,r){function e(n,t){return[n[0]*Math.cos(t)-n[1]*Math.sin(t),n[0]*Math.sin(t)+n[1]*Math.cos(t)]}var i=o(t);return!r||0===r[0]&&0===r[1]?e(n,i):e(n.map(function(n,t){return n-r[t]}),i).map(function(n,t){return n+r[t]})}function l(n){for(var t=0,r=0,e=n.length;rr&&(r=l),co&&(o=c))}return i?[[t,e],[r,o]]:null}function g(n){for(var t=0,r=0,e=0,o=n.length,i=0;i=2&&h(r[r.length-2],r[r.length-1],t[e])<=0;)r.pop();r.push(t[e])}for(var o=[],i=t.length-1;i>=0;i--){for(;o.length>=2&&h(o[o.length-2],o[o.length-1],t[i])<=0;)o.pop();o.push(t[i])}return o.pop(),r.pop(),r.concat(o)}function v(n){if(0===n.length)return 0;for(var t,r,e=-1,o=n.length,i=n[o-1],u=i[0],a=i[1],f=0;++e0&&void 0!==arguments[0]?arguments[0]:3,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:100,o=arguments.length>2?arguments[2]:void 0,u=[],a=[0,0],f=[0,0],l=0,c=0;cn[0][1]?n:[n[1],n[0]]}function w(n,t){var r=j(t);return h(n,r[1],r[0])<0}function O(n,t){var r=j(t);return h(n,r[1],r[0])>0}function T(n,t){var r=e(t);return q(n,t)&&e([t[0],n])<=r&&e([t[1],n])<=r}function q(n,t){return 0===h(n,t[0],t[1])}function x(n,t){for(var r=!1,e=k(t),o=0,i=e.length-1;oe!=c>e&&r<(l-a)*(e-f)/(c-f)+a&&(o=!o)}return o}function F(n,t){for(var r=!1,e=k(t),o=0,i=e.length-1;o=360?r-360:r<0?r+360:r}n.lineAngle=r,n.lineInterpolate=u,n.lineLength=e,n.lineMidpoint=a,n.pointRotate=f,n.pointTranslate=i,n.polygonArea=l,n.polygonBounds=c,n.polygonCentroid=g,n.polygonHull=p,n.polygonLength=v,n.polygonMean=s,n.polygonRegular=M,n.polygonRotate=d,n.polygonScale=b,n.polygonTranslate=y,n.lineIntersectsLine=I,n.lineIntersectsPolygon=x,n.pointInPolygon=S,n.pointOnPolygon=F,n.pointLeftofLine=w,n.pointRightofLine=O,n.pointOnLine=T,n.pointWithLine=q,n.polygonInPolygon=_,n.polygonIntersectsPolygon=B,n.angleReflect=C,n.angleToDegrees=t,n.angleToRadians=o,Object.defineProperty(n,"__esModule",{value:!0})}); \ No newline at end of file diff --git a/build/geometric.zip b/build/geometric.zip deleted file mode 100644 index 846f764..0000000 Binary files a/build/geometric.zip and /dev/null differ diff --git a/src/polygons/polygonArea.js b/src/polygons/polygonArea.js index 2f3796c..f952cc0 100644 --- a/src/polygons/polygonArea.js +++ b/src/polygons/polygonArea.js @@ -1,5 +1,5 @@ // Calculates the area of a polygon. -export function polygonArea(vertices){ +export function polygonArea(vertices, signed = false){ let a = 0; for (let i = 0, l = vertices.length; i < l; i++) { @@ -10,5 +10,5 @@ export function polygonArea(vertices){ a -= v1[0] * v0[1]; } - return Math.abs(a / 2); + return signed ? a / 2 : Math.abs(a / 2); } \ No newline at end of file diff --git a/test/polygonArea-test.js b/test/polygonArea-test.js index 620e50e..c28ca55 100644 --- a/test/polygonArea-test.js +++ b/test/polygonArea-test.js @@ -1,7 +1,7 @@ -var tape = require("tape"), +const tape = require("tape"), geometric = require("../"); -tape("polygonArea(polygon) calculates the area of a polygon", function(test) { +tape("polygonArea(polygon) calculates the area of a polygon", test => { test.equal(geometric.polygonArea([[0, 0], [1, 0], [1, 1], [0, 1]]), 1); test.equal(geometric.polygonArea([[0, 0], [2, 0], [2, 2], [0, 2]]), 4); test.equal(geometric.polygonArea([[0, 0], [3, 0], [3, 3], [0, 3]]), 9); @@ -9,4 +9,13 @@ tape("polygonArea(polygon) calculates the area of a polygon", function(test) { test.equal(geometric.polygonArea([[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]), 4); test.equal(geometric.polygonArea([[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]]), 9); test.end(); -}); \ No newline at end of file +}); + +tape("If the polygon's winding order is counter-clockwise and signed is set to true, returns a negative area", test => { + const p = [[119, 87], [61, 150], [131, 197], [206, 135]]; + test.equal(geometric.polygonArea(p, true), -8065); + test.equal(geometric.polygonArea(p, false), 8065); + test.equal(geometric.polygonArea(p), 8065); + test.end(); +}); +