-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 2.17
- Loading branch information
Showing
19 changed files
with
367 additions
and
153 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const sqrt = Math.sqrt | ||
const sin = Math.sin | ||
const cos = Math.cos | ||
|
||
module.exports = (direction) => { | ||
// @affineplane.orient2.fromPolar(direction) | ||
// @affineplane.orient2.fromVector(vec) | ||
// | ||
// Create an orientation from angle or vector. | ||
/// TODO See also affineplane.orient2.toPolar | ||
// | ||
// Parameters: | ||
// direction | ||
// a number, the azimuth angle in radians. | ||
// a dir2 | ||
// a vec2 | ||
// | ||
// Return | ||
// an orient2 | ||
// | ||
|
||
if (!direction) { | ||
return { a: 1, b: 0 } // default | ||
} | ||
|
||
if (typeof direction === 'object') { | ||
const x = direction.x | ||
const y = direction.y | ||
const norm = sqrt(x * x + y * y) // sums up to vector length | ||
return { | ||
// Normalize to 1 | ||
a: direction.x / norm, | ||
b: direction.y / norm | ||
} | ||
} | ||
|
||
if (typeof direction === 'number') { | ||
// Radians | ||
return { | ||
a: cos(direction), | ||
b: sin(direction) | ||
} | ||
} | ||
|
||
return { a: 1, b: 0 } // default | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module.exports = (c, cc) => { | ||
// @affineplane.sphere2.collisionArea(c, cc) | ||
// | ||
// Compute collision area between two spheres. | ||
// | ||
// Parameters: | ||
// c | ||
// a sphere2 | ||
// cc | ||
// a sphere2 | ||
// | ||
// Return | ||
// a scalar2, number, the area. | ||
// | ||
|
||
// For details about circle intersection, see: | ||
// https://mathworld.wolfram.com/Circle-CircleIntersection.html | ||
const dx = cc.x - c.x | ||
const dy = cc.y - c.y | ||
const d2 = dx * dx + dy * dy | ||
const r = c.r | ||
const rr = cc.r | ||
const d = Math.sqrt(d2) | ||
const r2 = r * r | ||
const rr2 = rr * rr | ||
|
||
if (r === 0 || rr === 0) { | ||
// One of the circles is a point. | ||
return 0 | ||
} | ||
|
||
if (d === 0) { | ||
// Concentric circles. Area of the smaller is the overlap. | ||
if (r <= rr) { | ||
return Math.PI * r2 | ||
} | ||
return Math.PI * rr2 | ||
} | ||
|
||
if (d > r + rr) { | ||
// Circles too distant. Just for optimization. | ||
return 0 | ||
} | ||
|
||
const area = r2 * Math.acos((d2 + r2 - rr2) / (2 * d * r)) + | ||
rr2 * Math.acos((d2 + rr2 - r2) / (2 * d * rr)) - | ||
0.5 * Math.sqrt((-d + r + rr) * (d + r - rr) * (d - r + rr) * (d + r + rr)) | ||
|
||
return area | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = (p, q) => { | ||
// @affineplane.sphere2.fromPoints(p, q) | ||
// | ||
// Create a sphere2 from two points, | ||
// the origin p and the circumference point q. | ||
// | ||
// Parameters: | ||
// p | ||
// a point2, at the circle center. | ||
// q | ||
// a point2, on the circle circumference. | ||
// | ||
// Return | ||
// a sphere2 | ||
// | ||
const dx = q.x - p.x | ||
const dy = q.y - p.y | ||
return { | ||
x: p.x, | ||
y: p.y, | ||
r: Math.sqrt(dx * dx + dy * dy) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = (p, q) => { | ||
// @affineplane.sphere3.fromPoints(p, q) | ||
// | ||
// Create a sphere3 from two points, | ||
// the origin p and the circumference point q. | ||
// | ||
// Parameters: | ||
// p | ||
// a point3, at the circle center. | ||
// q | ||
// a point3, on the circle circumference. | ||
// | ||
// Return | ||
// a sphere3 | ||
// | ||
const dx = q.x - p.x | ||
const dy = q.y - p.y | ||
const dz = q.z - p.z | ||
return { | ||
x: p.x, | ||
y: p.y, | ||
z: p.z, | ||
r: Math.sqrt(dx * dx + dy * dy + dz * dz) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const affineplane = require('../../index') | ||
const orient2 = affineplane.orient2 | ||
|
||
module.exports = (ts) => { | ||
ts.test('case: basic fromPolar', (t) => { | ||
t.almostEqualOrient( | ||
orient2.fromPolar(0), | ||
{ a: 1, b: 0 }, | ||
'zero angle' | ||
) | ||
|
||
t.almostEqualOrient( | ||
orient2.fromPolar(Math.PI / 2), | ||
{ a: 0, b: 1 }, | ||
'angle of +90deg' | ||
) | ||
|
||
t.end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const sphere2 = require('../../lib/sphere2') | ||
|
||
module.exports = (ts) => { | ||
ts.test('case: basic collisionArea', (t) => { | ||
t.equal( | ||
sphere2.collisionArea({ x: 0, y: 0, r: 0 }, { x: 0, y: 0, r: 0 }), | ||
0, | ||
'zero spheres have zero overlap' | ||
) | ||
|
||
t.equal( | ||
sphere2.collisionArea({ x: 0, y: 0, r: 1 }, { x: 0, y: 0, r: 2 }), | ||
Math.PI, | ||
'concentric spheres overlap' | ||
) | ||
|
||
t.equal( | ||
sphere2.collisionArea({ x: 0, y: 0, r: 1 }, { x: 1.5, y: 1.5, r: 1 }), | ||
0, | ||
'spheres too far' | ||
) | ||
|
||
t.almostEqual( | ||
sphere2.collisionArea({ x: 0, y: 0, r: 1 }, { x: 1, y: 0, r: 1 }), | ||
// overlap = 2 * segment | ||
// segment = π/3 - sqrt(3)/4 | ||
(2 * Math.PI / 3) - Math.sqrt(3) / 2, | ||
'spheres overlap' | ||
) | ||
|
||
t.end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const sphere2 = require('../../lib/sphere2') | ||
|
||
module.exports = (ts) => { | ||
ts.test('case: create sphere2 from two points', (t) => { | ||
const p1 = { x: 1, y: 1 } | ||
const p2 = { x: 5, y: 4 } | ||
t.deepEqual( | ||
sphere2.fromPoints(p1, p2), | ||
{ x: 1, y: 1, r: 5 }, | ||
'correct origin and radius' | ||
) | ||
|
||
t.end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const sphere3 = require('../../lib/sphere3') | ||
|
||
module.exports = (ts) => { | ||
ts.test('case: create sphere3 from two points', (t) => { | ||
const p1 = { x: 1, y: 1, z: 1 } | ||
const p2 = { x: 3, y: 3, z: 2 } | ||
t.deepEqual( | ||
sphere3.fromPoints(p1, p2), | ||
{ x: 1, y: 1, z: 1, r: 3 }, | ||
'correct origin and radius' | ||
) | ||
|
||
t.end() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const orient2 = require('../../lib/orient2') | ||
|
||
module.exports = function (actual, expected, message) { | ||
// Custom tape.js assertion. | ||
|
||
let isEqual = false | ||
if (orient2.validate(expected)) { | ||
// Expect orient2 | ||
isEqual = orient2.validate(actual) | ||
isEqual = isEqual && orient2.almostEqual(actual, expected) | ||
} | ||
|
||
this._assert(isEqual, { | ||
message: message || 'orientation should have correct elements', | ||
operator: 'almostEqualOrientation', | ||
actual, | ||
expected | ||
}) | ||
} |