diff --git a/v3/src/checksum.js b/v3/src/checksum.js index d867fc927..7f15f412b 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: 'a69a1650-14e2-11e7-b8c3-bdb77b9e9c3e' +build: '36a75180-14e9-11e7-ba0a-191be9c0e55a' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/geom/triangle/Area.js b/v3/src/geom/triangle/Area.js new file mode 100644 index 000000000..aa9d1334e --- /dev/null +++ b/v3/src/geom/triangle/Area.js @@ -0,0 +1,17 @@ +// The 2D area of a triangle. The area value is always non-negative. + +var Area = function (triangle) +{ + var x1 = triangle.x1; + var y1 = triangle.y1; + + var x2 = triangle.x2; + var y2 = triangle.y2; + + var x3 = triangle.x3; + var y3 = triangle.y3; + + return Math.abs(((x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)) / 2); +}; + +module.exports = Area; diff --git a/v3/src/geom/triangle/CircumCenter.js b/v3/src/geom/triangle/CircumCenter.js new file mode 100644 index 000000000..f75b415b4 --- /dev/null +++ b/v3/src/geom/triangle/CircumCenter.js @@ -0,0 +1,56 @@ +// Adapted from http://bjornharrtell.github.io/jsts/doc/api/jsts_geom_Triangle.js.html + +/** + * Computes the determinant of a 2x2 matrix. Uses standard double-precision + * arithmetic, so is susceptible to round-off error. + * + * @param {Number} + * m00 the [0,0] entry of the matrix. + * @param {Number} + * m01 the [0,1] entry of the matrix. + * @param {Number} + * m10 the [1,0] entry of the matrix. + * @param {Number} + * m11 the [1,1] entry of the matrix. + * @return {Number} the determinant. + */ +function det (m00, m01, m10, m11) +{ + return (m00 * m11) - (m01 * m10); +} + +/** + * Computes the circumcentre of a triangle. The circumcentre is the centre of + * the circumcircle, the smallest circle which encloses the triangle. It is also + * the common intersection point of the perpendicular bisectors of the sides of + * the triangle, and is the only point which has equal distance to all three + * vertices of the triangle. + *

+ * This method uses an algorithm due to J.R.Shewchuk which uses normalization to + * the origin to improve the accuracy of computation. (See Lecture Notes on + * Geometric Robustness, Jonathan Richard Shewchuk, 1999). + */ +var CircumCenter = function (triangle, out) +{ + if (out === undefined) { out = { x: 0, y: 0 }; } + + var cx = triangle.x3; + var cy = triangle.y3; + + var ax = triangle.x1 - cx; + var ay = triangle.y1 - cy; + + var bx = triangle.x2 - cx; + var by = triangle.y2 - cy; + + var denom = 2 * det(ax, ay, bx, by); + var numx = det(ay, ax * ax + ay * ay, by, bx * bx + by * by); + var numy = det(ax, ax * ax + ay * ay, bx, bx * bx + by * by); + + out.x = cx - numx / denom; + out.y = cy + numy / denom; + + return out; +}; + +module.exports = CircumCenter; diff --git a/v3/src/geom/triangle/CircumCircle.js b/v3/src/geom/triangle/CircumCircle.js new file mode 100644 index 000000000..46fac509f --- /dev/null +++ b/v3/src/geom/triangle/CircumCircle.js @@ -0,0 +1,56 @@ +// Adapted from https://gist.github.com/mutoo/5617691 + +var CircumCircle = function (triangle, out) +{ + if (out === undefined) { out = { x: 0, y: 0, radius: 0 }; } + + // A + var x1 = triangle.x1; + var y1 = triangle.y1; + + // B + var x2 = triangle.x2; + var y2 = triangle.y2; + + // C + var x3 = triangle.x3; + var y3 = triangle.y3; + + var A = x2 - x1; + var B = y2 - y1; + var C = x3 - x1; + var D = y3 - y1; + var E = A * (x1 + x2) + B * (y1 + y2); + var F = C * (x1 + x3) + D * (y1 + y3); + var G = 2 * (A * (y3 - y2) - B * (x3 - x2)); + + var dx; + var dy; + + // If the points of the triangle are collinear, then just find the + // extremes and use the midpoint as the center of the circumcircle. + + if (Math.abs(G) < 0.000001) + { + var minX = Math.min(x1, x2, x3); + var minY = Math.min(y1, y2, y3); + dx = (Math.max(x1, x2, x3) - minX) * 0.5; + dy = (Math.max(y1, y2, y3) - minY) * 0.5; + + out.x = minX + dx; + out.y = minY + dy; + out.radius = Math.sqrt(dx * dx + dy * dy); + } + else + { + out.x = (D * E - B * F) / G; + out.y = (A * F - C * E) / G; + dx = out.x - x1; + dy = out.y - y1; + out.radius = Math.sqrt(dx * dx + dy * dy); + } + + return out; +}; + +module.exports = CircumCircle; diff --git a/v3/src/geom/triangle/index.js b/v3/src/geom/triangle/index.js index b6d5cb0e2..e812ba3f4 100644 --- a/v3/src/geom/triangle/index.js +++ b/v3/src/geom/triangle/index.js @@ -3,6 +3,8 @@ var Triangle = require('./Triangle'); Triangle.Centroid = require('./Centroid'); +Triangle.CircumCenter = require('./CircumCenter'); +Triangle.CircumCircle = require('./CircumCircle'); Triangle.Contains = require('./Contains'); Triangle.ContainsPoint = require('./ContainsPoint'); Triangle.InCenter = require('./InCenter');