mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 07:01:20 +00:00
Added Triangle.Area, Triangle.CircumCenter and Triangle.CircumCircle methods, as they're all really useful when dealing with Triangles.
This commit is contained in:
parent
dc115121c4
commit
12e075ad10
5 changed files with 132 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
var CHECKSUM = {
|
||||
build: 'a69a1650-14e2-11e7-b8c3-bdb77b9e9c3e'
|
||||
build: '36a75180-14e9-11e7-ba0a-191be9c0e55a'
|
||||
};
|
||||
module.exports = CHECKSUM;
|
17
v3/src/geom/triangle/Area.js
Normal file
17
v3/src/geom/triangle/Area.js
Normal file
|
@ -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;
|
56
v3/src/geom/triangle/CircumCenter.js
Normal file
56
v3/src/geom/triangle/CircumCenter.js
Normal file
|
@ -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.
|
||||
* <p>
|
||||
* This method uses an algorithm due to J.R.Shewchuk which uses normalization to
|
||||
* the origin to improve the accuracy of computation. (See <i>Lecture Notes on
|
||||
* Geometric Robustness</i>, 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;
|
56
v3/src/geom/triangle/CircumCircle.js
Normal file
56
v3/src/geom/triangle/CircumCircle.js
Normal file
|
@ -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;
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue