JSFiddle - React, Tailwind, and code Playground
by eyal0
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.js"></script>
<ul id='cycleResults'>
</ul>
<div id="result">
</div>
<br>
<button id="btn">
Run Tests
</button>
JavaScript
var t0;
t0 = {
a: {
x: Math.random(),
y: Math.random()
},
b: {
x: Math.random(),
y: Math.random()
},
c: {
x: Math.random(),
y: Math.random()
}
};
var t1;
t1 = {
a: {
x: Math.random(),
y: Math.random()
},
b: {
x: Math.random(),
y: Math.random()
},
c: {
x: Math.random(),
y: Math.random()
}
};
var randomizeTriangles = function() {
t0.a.x = Math.random();
t0.a.y = Math.random();
t0.b.x = Math.random();
t0.b.y = Math.random();
t0.c.x = Math.random();
t0.c.y = Math.random();
var v0x = t0.b.x - t0.a.x;
var v0y = t0.b.y - t0.a.y;
var v1x = t0.c.x - t0.a.x;
var v1y = t0.c.y - t0.a.y;
// if v0 cross v1 is positive, triangle is counter clockwise when viewed from above.
t0.normal = v0x * v1y - v1x * v0y;
t1.a.x = Math.random();
t1.a.y = Math.random();
t1.b.x = Math.random();
t1.b.y = Math.random();
t1.c.x = Math.random();
t1.c.y = Math.random();
var v0x = t1.b.x - t1.a.x;
var v0y = t1.b.y - t1.a.y;
var v1x = t1.c.x - t1.a.x;
var v1y = t1.c.y - t1.a.y;
t1.normal = v0x * v1y - v1x * v0y;
};
var pointInTriangleBary = function(point, triangle) {
var a = triangle.a;
var b = triangle.b;
var c = triangle.c;
var px = point.x;
var py = point.y;
var ax = a.x;
var ay = a.y;
// Compute vectors
var v0x = c.x - ax;
var v0y = c.y - ay;
var v1x = b.x - ax;
var v1y = b.y - ay;
var v2x = px - ax;
var v2y = py - ay;
var dot00 = v0x * v0x + v0y * v0y;
var dot01 = v0x * v1x + v0y * v1y;
var dot02 = v0x * v2x + v0y * v2y;
var dot11 = v1x * v1x + v1y * v1y;
var dot12 = v1x * v2x + v1y * v2y;
var denom = dot00 * dot11 - dot01 * dot01;
var u = (dot11 * dot02 - dot01 * dot12) / denom;
var v = (dot00 * dot12 - dot01 * dot02) / denom;
// Check if point is in triangle
return (u >= 0) && (v >= 0) && (u + v < 1);
};
var pointInTriangleBary2 = function(point, triangle) {
var p = point;
var p0 = triangle.a;
var p1 =...