Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Port tests to JS.Test and do some more reorganization.

  • Loading branch information...
commit 7c9cb8c89d2751bfbe02fa5f05da09f07b8377af 1 parent 3710b3c
@jcoglan authored
View
25 test/demo.html → examples/demo.html
@@ -1,19 +1,16 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!doctype html>
<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
- <title>Polygon demo</title>
- <script src="../lib/sylvester.js" type="text/javascript" language="javascript" charset="utf-8"></script>
-</head>
-<body>
-
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>Polygon demo</title>
+ <script type="text/javascript" src="../lib/sylvester-min.js"></script>
+ </head>
+ <body>
+
<canvas id="screen" width="800" height="600"></canvas>
<script type="text/javascript">
- (function() {
-
var P = Polygon.create([
[0,0], [5,0], [5,5], [0,5], [0,10],
@@ -60,9 +57,7 @@
document.onclick = function() {
trigs[i++].copyVertices();
};
-
- })();
</script>
-
-</body>
+
+ </body>
</html>
View
0  test/dot.png → examples/dot.png
File renamed without changes
View
42 examples/dots.html
@@ -0,0 +1,42 @@
+<!doctype html>
+
+<html>
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>Vectors / Matrices</title>
+ <script type="text/javascript" src="../lib/sylvester-min.js"></script>
+ </head>
+ <body style="background: #000;">
+
+ <script type="text/javascript">
+ var dots = [], images = [], h = 500;
+ var axis = Vector.create([0.7,1,0.3]);
+ var rot = Matrix.Rotation(0.1, axis);
+
+ for (var i = 0; i < 16; i++) {
+ dots[i] = Vector.create([h*(Math.random()-0.5), h*(Math.random()-0.5), h*(Math.random()-0.5)]);
+ image = document.createElement('img');
+ image.id = 'img_' + i;
+ image.src = './dot.png';
+ document.body.appendChild(image);
+ images[i] = image;
+ }
+
+ setInterval(function() {
+ for (var i = 0; i < dots.length; i++) {
+ f = (dots[i].e(3) + h)/(2*h) + 0.1;
+
+ images[i].style.opacity = f - 0.1;
+ images[i].style.width = (100 * f) + 'px';
+ images[i].style.height = (100 * f) + 'px';
+ images[i].style.position = 'absolute';
+ images[i].style.top = (300 - dots[i].e(2)*f) + 'px';
+ images[i].style.left = (600 + dots[i].e(1)*f) + 'px';
+
+ dots[i] = rot.x(dots[i]);
+ }
+ }, 20);
+ </script>
+
+ </body>
+</html>
View
72 examples/polygons.html
@@ -0,0 +1,72 @@
+<!doctype html>
+
+<html>
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>Vectors / Matrices</title>
+ <script type="text/javascript" src="../lib/sylvester-min.js"></script>
+ </head>
+ <body>
+
+ <canvas id="canv" width="600" height="450" style="display: block; margin: 20px auto;" onclick="rotate();"></canvas>
+
+ <script type="text/javascript">
+ var O = $V([300,225,0]);
+
+ var Foo = Polygon.create([ [0,0], [5,0], [5,5], [0,5], [0,10], [5,10], [0,5],
+ [5,5], [10,10], [10,15], [-5,15], [-5,5], [-10,5], [-10,0], [-5,0],
+ [-5,-5], [0,-5] ]).scale(20, $V([0,5])).rotate(Math.PI/3, $L([0,5], [0,2,1]));
+
+ var canvas, ctx;
+
+ window.onload = function() {
+ canvas = document.getElementById('canv');
+ ctx = canvas.getContext('2d');
+ ctx.fillColor = '#000000';
+ ctx.fillRect(0,0, canvas.width, canvas.height);
+
+ draw(ctx, Foo, 100);
+
+ var trigs;
+ trigs = Foo.toTriangles();
+ var C = 255;
+ for (var i = 0; i < trigs.length; i++) {
+ draw(ctx, trigs[i], C);
+ C -= Math.round((255 - 100) / trigs.length);
+ }
+
+ var c = Foo.centroid();
+ ctx.fillStyle = 'red';
+ ctx.fillRect(300 + c.e(1) - 5, 225 - c.e(2) - 5, 10, 10);
+ };
+
+ function draw(ctx, poly, color) {
+ ctx.fillStyle = 'rgb(' + color +',' + color + ',' + color + ')';
+ ctx.beginPath();
+ poly.vertices.each(function(node) {
+ point = O.add(node.data).reflectionIn($L(O, Vector.i));
+ if (node.data == poly.vertices.first.data) {
+ ctx.moveTo(point.e(1), point.e(2));
+ } else {
+ ctx.lineTo(point.e(1), point.e(2));
+ }
+ } );
+ ctx.fill();
+ }
+
+ function rotate() {
+ ctx.fillStyle = '#000000';
+ ctx.fillRect(0,0, canvas.width, canvas.height);
+ Foo.rotate(Math.PI/20, $L([0,5], [0,2,1]));
+ var trigs = Foo.toTriangles();
+ var C = 255;
+ for (var i = 0; i < trigs.length; i++) {
+ draw(ctx, trigs[i], C);
+ C -= Math.round((255 - 100) / trigs.length);
+ }
+ setTimeout(rotate, 20);
+ }
+ </script>
+
+ </body>
+</html>
View
2  src/matrix.js
@@ -225,7 +225,7 @@ Sylvester.Matrix.prototype = {
}
}
var M = Sylvester.Matrix.create(elements);
- return returnSylvester.Vector ? M.col(1) : M;
+ return returnVector ? M.col(1) : M;
},
x: function(matrix) { return this.multiply(matrix); },
View
6 src/vector.js
@@ -180,7 +180,7 @@ Sylvester.Vector.prototype = {
// Returns a diagonal matrix with the vector's elements as its diagonal elements
toDiagonalMatrix: function() {
- return Matrix.Diagonal(this.elements);
+ return Sylvester.Matrix.Diagonal(this.elements);
},
// Returns the result of rounding the elements of the vector
@@ -228,7 +228,7 @@ Sylvester.Vector.prototype = {
case 2:
V = obj.elements || obj;
if (V.length != 2) { return null; }
- if (!R) { R = Matrix.Rotation(t).elements; }
+ if (!R) { R = Sylvester.Matrix.Rotation(t).elements; }
x = this.elements[0] - V[0];
y = this.elements[1] - V[1];
return Sylvester.Vector.create([
@@ -239,7 +239,7 @@ Sylvester.Vector.prototype = {
case 3:
if (!obj.direction) { return null; }
var C = obj.pointClosestTo(this).elements;
- if (!R) { R = Matrix.Rotation(t, obj.direction).elements; }
+ if (!R) { R = Sylvester.Matrix.Rotation(t, obj.direction).elements; }
x = this.elements[0] - C[0];
y = this.elements[1] - C[1];
z = this.elements[2] - C[2];
View
46 test/dots.html
@@ -1,46 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
-<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
-<title>Vectors / Matrices</title>
-<script src="../lib/sylvester.js" type="text/javascript"></script>
-<script src="lib/prototype.js" type="text/javascript"></script>
-<script type="text/javascript">
-
- var dots = [], images = [], h = 500;
- var axis = Vector.create([0.7,1,0.3]);
- var rot = Matrix.Rotation(0.1, axis);
-
- window.onload = function() {
-
- for (var i = 0; i < 16; i++) {
- dots[i] = Vector.create([h*(Math.random()-0.5), h*(Math.random()-0.5), h*(Math.random()-0.5)]);
- insert = new Insertion.Bottom(document.body, '<img id="img_' + i + '" src="dot.png" />');
- images[i] = $('img_' + i);
- }
-
- new PeriodicalExecuter(function() {
- for (var i = 0; i < dots.length; i++) {
- f = (dots[i].e(3) + h)/(2*h) + 0.1;
- images[i].setStyle({
- opacity: f - 0.1,
- width: (100 * f) + 'px',
- height: (100 * f) + 'px',
- position: 'absolute',
- top: (300 - dots[i].e(2)*f) + 'px',
- left: (600 + dots[i].e(1)*f) + 'px'
- });
- dots[i] = rot.x(dots[i]);
- }
- }, 0.01);
- };
-
-</script>
-</head>
-<body style="background: #000;">
-
-
-
-</body>
-</html>
View
115 test/line.html
@@ -1,115 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
- <title>Line tests</title>
- <script src="lib/prototype.js" type="text/javascript" charset="utf-8"></script>
- <script src="lib/unittest.js" type="text/javascript" charset="utf-8"></script>
- <script src="../lib/sylvester.js" type="text/javascript" charset="utf-8"></script>
- <link rel="stylesheet" href="lib/test.css" type="text/css"/>
-</head>
-<body>
-
-<h1 id="line_test_suite">Line test suite</h1>
-
-<div id="testlog">
-</div>
-
-<script type="text/javascript" charset="utf-8">
-
-new Test.Unit.Runner({
- testDuplication: function() { with(this) {
- var L = Line.X.dup();
- assert(L.eql(Line.X));
- L.anchor.setElements([8,2,5]);
- L.direction.setElements([2,5,6]);
- assert(Line.X.anchor.eql([0,0,0]));
- assert(Line.X.direction.eql(Vector.i));
- assert(!L.eql(Line.X));
- }},
-
- testEqualityWithAntiparallelLines: function() { with(this) {
- assert(Line.X.eql($L([0,0,0], [-12,0,0])));
- }},
-
- testContainment: function() { with(this) {
- assert(Line.X.contains([99,0,0]));
- assert(!Line.X.contains([99,1,0]));
- assert(!Line.X.contains([99,0,2]));
- assert($L([0,0,0], [1,1,1]).contains(Line.Segment.create([-2,-2,-2], [13,13,13])));
- }},
-
- testParallelity: function() { with(this) {
- assert(Line.X.isParallelTo($L([0,0,-12], [-4,0,0])));
- assert(Line.X.isParallelTo($P([0,0,-4], Vector.k)));
- assert(!Line.Z.isParallelTo($P([0,0,-4], Vector.k)));
- assert(Line.Z.isParallelTo(Line.Segment.create([9,2,6], [9,2,44])));
- assert(!Line.Z.isParallelTo(Line.Segment.create([9,3,6], [9,2,44])));
- }},
-
- testTranslation: function() { with(this) {
- assert(Line.X.dup().translate([0,0,12]).eql($L([0,0,12], Vector.i)));
- }},
-
- testLineIntersection: function() { with(this) {
- for (var i = 0, O, V, V1, V2, L1, L2; i < 10; i++) {
- O = $V([-5,-5,-5]);
- V = O.add(Vector.Random(3).x(10));
- V1 = O.add(Vector.Random(3).x(10));
- V2 = O.add(Vector.Random(3).x(10));
- L1 = $L(V, V1);
- L2 = $L(V.add(V1.x(-20 + 40*Math.random())), V2);
- V = L1.intersectionWith(L2);
- assert(L1.contains(V));
- assert(L2.contains(V));
- }
- }},
-
- testIntersectionTesting: function() { with(this) {
- assert($L([5,0], [0,1]).intersectionWith($L([0,0], [-1,-1])).eql([5,5,0]));
- assert(Line.X.intersects(Line.Segment.create([7,-4,0], [7,5,0])));
- assert(!Line.X.intersects(Line.Segment.create([7,-4,-1], [7,5,0])));
- }},
-
- testPositionOfPoint: function() { with(this) {
- assert($L([0,0,0], [1,1,-1]).positionOf([3,3,-3]) - Math.sqrt(27) <= Sylvester.precision);
- }},
-
- testPointClosestTo: function() { with(this) {
- assert(Line.X.pointClosestTo($V([26,-2,18])).eql([26,0,0]));
- assert($L([0,0,0], [1,0,0]).pointClosestTo($L([0,0,24], [1,1,0])).eql([0,0,0]));
- assert($L([0,0,24], [1,1,0]).pointClosestTo($L([0,0,0], [-1,0,0])).eql([0,0,24]));
- assert(Line.X.pointClosestTo(Line.Segment.create([3,5], [9,9])).eql([3,0,0]));
- assert(Line.X.pointClosestTo(Line.Segment.create([2,-2,2], [4,2,2])).eql([3,0,0]));
- }},
-
- testDistanceFrom: function() { with(this) {
- assertIdentical(24, $L([0,0,0], [1,0,0]).distanceFrom($L([0,0,24], [1,1,0])));
- assertIdentical(12, $L([12,0,0], Vector.k).distanceFrom(Plane.YZ));
- assertIdentical(0, $L([12,0,0], [1,0,200]).distanceFrom(Plane.YZ));
- assert(Math.abs(Math.sqrt(18) - Line.X.distanceFrom(Line.Segment.create([12,3,3], [15,4,3]))) <= Sylvester.precision);
- }},
-
- testReflection: function() { with(this) {
- assert(Line.Z.reflectionIn([28,0,-12]).eql($L([56,0,0], Vector.k.x(-1))));
- assert(Line.X.reflectionIn($L([0,0,0],[1,0,1])).eql(Line.Z));
- var L1 = Line.X.dup();
- var L2 = $L([5,0,0], Vector.k);
- assert(L1.reflectionIn($P([5,0,0], [1,0,1])).eql(L2));
- assert(L2.reflectionIn($P([5,0,0], [1,0,1])).eql(L1));
- assert($L([-4,3], [0,-1]).reflectionIn($V([0,0])).eql($L([4,100], [0,4])));
- }},
-
- testRotation: function() { with(this) {
- assert(Line.X.rotate(Math.PI, $L([12,0,0],[1,0,1])).eql($L([12,0,0], Vector.k)));
- assert($L([10,0,0], [0,1,1]).rotate(-Math.PI/2, Line.Y).eql($L([0,0,10], [1,-1,0])));
- assert($L([9,0], Vector.j).rotate(Math.PI/2, $V([9,9])).eql($L([0,9], Vector.i)));
- }}
-});
-
-</script>
-
-</body>
-</html>
View
127 test/line.segment.html
@@ -1,127 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
- <title>Line.Segment tests</title>
- <script src="lib/prototype.js" type="text/javascript" charset="utf-8"></script>
- <script src="lib/unittest.js" type="text/javascript" charset="utf-8"></script>
- <script src="../lib/sylvester.js" type="text/javascript" charset="utf-8"></script>
- <link rel="stylesheet" href="lib/test.css" type="text/css"/>
-</head>
-<body>
-
-<h1 id="line.segment_test_suite">Line.Segment test suite</h1>
-
-<div id="testlog">
-</div>
-
-<script type="text/javascript" charset="utf-8">
-
-new Test.Unit.Runner({
- setup: function() {
- this.segment = Line.Segment.create([5,5,5], [10,10,10]);
- this.segment2 = Line.Segment.create([1,1,0], [1,2,0]);
- },
-
- testEquality: function() { with(this) {
- assert(this.segment.eql(this.segment));
- assert(!this.segment.eql(this.segment2));
- assert(this.segment.eql(Line.Segment.create(this.segment.end, this.segment.start)));
- }},
-
- testDuplication: function() { with(this) {
- var seg = this.segment.dup();
- assert(this.segment.eql(seg));
- seg.start.setElements([23,87,56]);
- assert(!this.segment.eql(seg));
- assert(this.segment.start.eql([5,5,5]));
- }},
-
- testLength: function() { with(this) {
- assert(this.segment.length() - Math.sqrt(75) <= Sylvester.precision);
- }},
-
- testConversionToVector: function() { with(this) {
- assert(this.segment.toVector().eql([5,5,5]));
- }},
-
- testMidpoint: function() { with(this) {
- assert(this.segment.midpoint().eql([7.5,7.5,7.5]));
- }},
-
- testBisectingPlane: function() { with(this) {
- assert(this.segment.bisectingPlane().eql($P([7.5,7.5,7.5], [1,1,1])));
- }},
-
- testTranslation: function() { with(this) {
- assert(this.segment.translate([9,2,7]).eql(Line.Segment.create([14,7,12], [19,12,17])));
- }},
-
- testParallelity: function() { with(this) {
- assert(this.segment2.isParallelTo(Line.Y));
- assert(!this.segment2.isParallelTo(Line.Z));
- assert(this.segment2.isParallelTo(Plane.XY));
- assert(this.segment2.isParallelTo(Plane.YZ));
- assert(!this.segment2.isParallelTo(Plane.ZX));
- assert(!this.segment.isParallelTo(this.segment2));
- assert(this.segment2.isParallelTo(this.segment2));
- }},
-
- testContainment: function() { with(this) {
- assert(this.segment.contains(this.segment.midpoint()));
- assert(this.segment.contains([5,5,5]));
- assert(this.segment.contains([10,10,10]));
- assert(!this.segment.contains([4.9999,4.9999,4.9999]));
- assert(!this.segment.contains([10.00001, 10.00001, 10.00001]));
- assert(this.segment.contains(Line.Segment.create([5,5,5], [8,8,8])));
- assert(this.segment.contains(Line.Segment.create([7,7,7], [10,10,10])));
- assert(!this.segment.contains(Line.Segment.create([4,4,4], [8,8,8])));
- }},
-
- testDistances: function() { with(this) {
- assertIdentical(5, this.segment.distanceFrom([5,5,0]));
- assertIdentical(2, this.segment.distanceFrom([10,12,10]));
- assertIdentical(Math.sqrt(2* 25), this.segment.distanceFrom(Line.X));
- assertIdentical(1, this.segment.distanceFrom($L([11,10,10], [0,1])));
- assertIdentical(5, this.segment.distanceFrom(Plane.XY));
- assertIdentical(Math.sqrt(4 + 25 + 25), this.segment.distanceFrom(Line.Segment.create([7,0,0], [9,0,0])));
- }},
-
- testIntersection: function() { with(this) {
- assert(!this.segment.intersects(Line.X));
- assert(!this.segment.intersects(Line.Y));
- assert(!this.segment.intersects(Line.Z));
- assert(!this.segment.intersects(Plane.XY));
- assert(!this.segment.intersects(Plane.YZ));
- assert(!this.segment.intersects(Plane.ZX));
- assert(this.segment.intersectionWith(this.segment.bisectingPlane()).eql(this.segment.midpoint()));
- assert(
- Line.Segment.create([0,4,4], [0,8,4]).intersectionWith(
- Line.Segment.create([0,6,2], [0,6,6])
- ).eql([0,6,4])
- );
- assertNull(
- Line.Segment.create([0,4,4], [0,8,4]).intersectionWith(
- Line.Segment.create([2,6,2], [0,6,6])
- )
- );
- assert(this.segment.intersects(Line.Segment.create([6,7,7], [9,7,7])));
- assert(!this.segment.intersects(this.segment2));
- }},
-
- testPointClosestTo: function() { with(this) {
- assertNull(this.segment2.pointClosestTo(Line.Y));
- assert(this.segment2.pointClosestTo(Line.X).eql([1,1,0]));
- assert(this.segment2.pointClosestTo(Line.X.translate([0,10])).eql([1,2,0]));
- assert(this.segment2.pointClosestTo($L([0,1.5,0], [0,0,1])).eql([1,1.5,0]));
- assert(this.segment2.pointClosestTo(Plane.XZ).eql([1,1,0]));
- assertNull(this.segment2.pointClosestTo(Plane.YZ));
- }}
-});
-
-</script>
-
-</body>
-</html>
View
343 test/matrix.html
@@ -1,343 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
- <title>Matrix tests</title>
- <script src="lib/prototype.js" type="text/javascript" charset="utf-8"></script>
- <script src="lib/unittest.js" type="text/javascript" charset="utf-8"></script>
- <script src="../lib/sylvester.js" type="text/javascript" charset="utf-8"></script>
- <link rel="stylesheet" href="lib/test.css" type="text/css"/>
-</head>
-<body>
-
-<h1 id="matrix_test_suite">Matrix test suite</h1>
-
-<div id="testlog">
-
-</div>
-
-<script type="text/javascript" charset="utf-8">
-
-new Test.Unit.Runner({
- testCreation: function() { with(this) {
- var M = $M([
- [0,3,4,8],
- [3,9,7,3]
- ]);
- assertIdentical('[0, 3, 4, 8]\n[3, 9, 7, 3]', M.inspect());
- assertIdentical('[0, 2, 7, 5]', $M([[0,2,7,5]]).inspect());
- assertIdentical('[0]\n[2]\n[7]\n[5]', $M([[0],[2],[7],[5]]).inspect());
- assertIdentical('[128]', $M([[128]]).inspect());
- }},
-
- testElementRetrieval: function() { with(this) {
- var M = $M([
- [0,3,4,8],
- [3,9,7,3]
- ]);
- assertIdentical(0, M.e(1,1));
- assertIdentical(8, M.e(1,4));
- assertNull(M.e(2,6));
- }},
-
- testRowAndColumnRetrieval: function() { with(this) {
- var M = $M([
- [0,3,4,8],
- [3,9,7,3]
- ]);
- assert(M.row(2).eql([3,9,7,3]));
- assertNull(M.row(3));
- assert(M.col(2).eql([3,9]));
- assertNull(M.col(6));
- }},
-
- testDimensions: function() { with(this) {
- var M = $M([
- [0,3,4,8],
- [3,9,7,3]
- ]);
- assertIdentical(2, M.rows());
- assertIdentical(4, M.cols());
- }},
-
- testDuplication: function() { with(this) {
- var M1 = $M([
- [2,3,8],
- [7,0,2],
- [6,3,0]
- ]);
- var M2 = M1.dup();
- assert(M1.eql(M2));
- M2.elements[1][1] = 99;
- assert(!M1.eql(M2));
- assertIdentical(0, M1.elements[1][1]);
- }},
-
- testEquality: function() { with(this) {
- var M = $M([
- [2,3,8],
- [7,0,2],
- [6,3,0]
- ]);
- assert(M.eql([[2,3,8], [7,0,2], [6,3,0]]));
- assert(!M.eql([[7,3,8], [7,0,2], [6,3,0]]));
- assert(!M.eql([[2,7,8], [7,0,2], [6,3,0]]));
- assert(!M.eql([[2,3,7], [7,0,2], [6,3,0]]));
- assert(!M.eql([[2,3,8], [8,0,2], [6,3,0]]));
- assert(!M.eql([[2,3,8], [7,7,2], [6,3,0]]));
- assert(!M.eql([[2,3,8], [7,0,7], [6,3,0]]));
- assert(!M.eql([[2,3,8], [7,0,2], [7,3,0]]));
- assert(!M.eql([[2,3,8], [7,0,2], [6,7,0]]));
- assert(!M.eql([[2,3,8], [7,0,2], [6,3,7]]));
- }},
-
- testMapping: function() { with(this) {
- assert(
- $M([
- [2,3,8],
- [7,0,2],
- [6,3,0]
- ]).map(function(x, i, j) { return x + j; }).eql([
- [3,5,11],
- [8,2,5],
- [7,5,3]
- ])
- );
- }},
-
- testRandomMatrix: function() { with(this) {
- var M;
- for (var i = 1; i < 5; i++) {
- M = Matrix.Random(4,i);
- assertIdentical(4, M.rows());
- assertIdentical(i, M.cols());
- M = Matrix.Random(i,3);
- assertIdentical(i, M.rows());
- assertIdentical(3, M.cols());
- }
- }},
-
- testZeroMatrix: function() { with(this) {
- var M;
- for (var i = 1; i < 5; i++) {
- M = Matrix.Random(5,i);
- assertIdentical(5, M.rows());
- assertIdentical(i, M.cols());
- M = Matrix.Random(i,2);
- assertIdentical(i, M.rows());
- assertIdentical(2, M.cols());
- }
- }},
-
- testSizeComparison: function() { with(this) {
- assert(Matrix.Random(2,5).isSameSizeAs(Matrix.Zero(2,5)));
- assert(!Matrix.Random(2,6).isSameSizeAs(Matrix.Zero(2,5)));
- assert(!Matrix.Random(1,5).isSameSizeAs(Matrix.Zero(2,5)));
- }},
-
- testArithmetic: function() { with(this) {
- var M1 = $M([
- [2,5,9,3],
- [9,2,8,5]
- ]);
- var M2 = $M([
- [7,1,0,8],
- [0,4,3,8]
- ]);
- var M = $M([
- [9,6, 9,11],
- [9,6.0,11,13]
- ]);
- assert(M1.add(M2).eql(M));
- assert(M2.add(M1).eql(M));
- assertNull(M1.add(Matrix.Zero(2,5)));
- M = $M([
- [-5,4,9.0,-5],
- [9,-2,5,-3]
- ]);
- assert(M1.subtract(M2).eql(M));
- assert(M2.subtract(M1).eql(M.x(-1)));
- assertNull(M1.subtract(Matrix.Zero(2,7)));
- assert(M2.x(3).eql([
- [21,3,0,24],
- [0,12,9,24]
- ]));
- }},
-
- testMultiplication: function() { with(this) {
- var M1 = $M([
- [2,5,9,3],
- [9,2,8,5]
- ]);
- var M2 = $M([
- [2,9],
- [0,2],
- [8,1],
- [0,6]
- ]);
- assertIdentical(2, M1.x(M2).rows());
- assertIdentical(2, M1.x(M2).cols());
- assert(M1.x(M2).eql([
- [76, 55],
- [82, 123]
- ]));
- assertIdentical(4, M2.x(M1).rows());
- assertIdentical(4, M2.x(M1).cols());
- assertNull(M1.x(M1.x(M2)));
- assertNotNull(M1.x(M2.x(M1)));
- }},
-
- testMinor: function() { with(this) {
- var M2 = $M([
- [2,9],
- [0,2],
- [8,1],
- [0,6]
- ]);
- var M = $M([
- [9,2,9],
- [2,0,2],
- [1,8,1]
- ]);
- assert(M2.minor(1,2,3,3).eql(M));
- }},
-
- testSquareness: function() { with(this) {
- assert(Matrix.Zero(9,9).isSquare());
- assert(!Matrix.Zero(4,9).isSquare());
- assert(!Matrix.Zero(9,3).isSquare());
- }},
-
- testMaximumAndIndex: function() { with(this) {
- var M = $M([
- [2,5,9,3],
- [9,2,8,5]
- ]);
- assertIdentical(9, M.max());
- assert(M.indexOf(8).i == 2 && M.indexOf(8).j == 3);
- assert(M.indexOf(9).i == 1 && M.indexOf(9).j == 3);
- }},
-
- testDiagonalVector: function() { with(this) {
- var M = $M([
- [9,2,9],
- [2,0,2],
- [1,8,1]
- ]);
- assert(M.diagonal().eql([9,0,1]));
- }},
-
- testTriangulation: function() { with(this) {
- for (var i = 0, M; i < 8; i++) {
- M = Matrix.Random(3,3);
- assertMatch(/^\[[0-9\-\.]+, [0-9\-\.]+, [0-9\-\.]+\]\n\[0, [0-9\-\.]+, [0-9\-\.]+\]\n\[0, 0, [0-9\-\.]+\]$/, M.toRightTriangular().inspect())
- };
- }},
-
- testTransposing: function() { with(this) {
- var M1 = $M([
- [3,9,8,4],
- [2,0,1,5]
- ]);
- var M2 = $M([
- [3,2],
- [9,0],
- [8,1],
- [4,5]
- ]);
- assert(M1.transpose().eql(M2));
- assert(M2.transpose().eql(M1));
- }},
-
- testDeterminant: function() { with(this) {
- for (var i = 0, M; i < 5; i++) {
- M = Matrix.Random(3,3).x(10).elements;
- assert(
- M[0][0] * (M[1][1]*M[2][2] - M[1][2]*M[2][1]) +
- M[0][1] * (M[1][2]*M[2][0] - M[1][0]*M[2][2]) +
- M[0][2] * (M[1][0]*M[2][1] - M[1][1]*M[2][0]) -
- $M(M).determinant()
- < Sylvester.precision
- );
- }
- assertNull(Matrix.Random(3,4).determinant());
- }},
-
- testSingularity: function() { with(this) {
- var M = Matrix.Random(3,3).x(10);
- M.elements[0][0] = 0; M.elements[1][0] = 0; M.elements[2][0] = 0;
- assert(M.isSingular());
- assert(!Matrix.Zero(4,3).isSingular())
- }},
-
- testTrace: function() { with(this) {
- var M = $M([
- [8,1,6],
- [0,1,7],
- [0,1,5]
- ]);
- assertIdentical(14, M.tr());
- assertNull(Matrix.Random(4,5).tr());
- }},
-
- testRankWithInterdependentRows: function() { with(this) {
- var M = $M([
- [1,9,4,6],
- [9,2,7,4],
- [18,4,14,8]
- ]);
- assertIdentical(2, M.rk());
- }},
-
- testAugmentation: function() { with(this) {
- assert($M([
- [7,2,9,4],
- [4,8,2,6],
- [9,2,5,6]
- ]).augment([
- [4,6],
- [5,2],
- [8,2]
- ]).eql([
- [7,2,9,4,4,6],
- [4,8,2,6,5,2],
- [9,2,5,6,8,2]
- ]));
- }},
-
- testInversion: function() { with(this) {
- for (var i = 0, M; i < 10; i++) {
- M = Matrix.Random(4,4).x(5);
- if (M.isSingular()) { continue; }
- assert(M.x(M.inv()).eql(Matrix.I(4)));
- assert(M.inv().x(M).eql(Matrix.I(4)));
- }
- }},
-
- testRotation: function() { with(this) {
- assert(Matrix.Rotation(Math.PI/2).eql([
- [0,-1], [1,0]
- ]));
- assert(Matrix.Rotation(Math.PI/2, Vector.j).eql([
- [0,0,1],
- [0,1,0],
- [-1,0,0]
- ]));
- }},
-
- testDiagonal: function() { with(this) {
- assert(Matrix.Diagonal([3,9,5,7]).eql([
- [3,0,0,0],
- [0,9,0,0],
- [0,0,5,0],
- [0,0,0,7]
- ]));
- }}
-});
-
-</script>
-
-</body>
-</html>
View
96 test/plane.html
@@ -1,96 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
- <title>Plane tests</title>
- <script src="lib/prototype.js" type="text/javascript" charset="utf-8"></script>
- <script src="lib/unittest.js" type="text/javascript" charset="utf-8"></script>
- <script src="../lib/sylvester.js" type="text/javascript" charset="utf-8"></script>
- <link rel="stylesheet" href="lib/test.css" type="text/css"/>
-</head>
-<body>
-
-<h1 id="plane_test_suite">Plane test suite</h1>
-
-<div id="testlog">
-</div>
-
-<script type="text/javascript" charset="utf-8">
-
-new Test.Unit.Runner({
- testEqualityWithDifferingPoints: function() { with(this) {
- assert(Plane.XY.dup().eql($P([34,-99,0], [0,0,-4])));
- assert(!Plane.XY.dup().eql($P([34,-99,1], [0,0,-4])));
- assert(!Plane.XY.dup().eql($P([34,-99,0], [1,0,-4])));
- assert(!Plane.XY.dup().eql($P([34,-99,0], [0,-1,-4])))
- }},
-
- testDuplication: function() { with(this) {
- var P = Plane.XY.dup();
- P.anchor.setElements([3,4,5]);
- P.normal.setElements([0,2,6]);
- assert(Plane.XY.anchor.eql([0,0,0]));
- assert(Plane.XY.normal.eql(Vector.k));
- }},
-
- testTranslation: function() { with(this) {
- assert(Plane.XY.translate([5,12,-14]).eql($P([89,-34,-14], Vector.k)));
- assert(Plane.XY.anchor.eql(Vector.Zero(3)));
- }},
-
- testParallelity: function() { with(this) {
- assert(Plane.XY.dup().translate([5,12,-14]).isParallelTo(Plane.XY));
- assert(Plane.XY.isParallelTo($L([4,8,10], [2,-6,0])));
- }},
-
- testDistanceFrom: function() { with(this) {
- assertIdentical(14, Plane.XY.dup().translate([5,12,-14]).distanceFrom(Plane.XY));
- assertIdentical(0, Plane.XY.dup().translate([5,12,-14]).distanceFrom($P([0,0,0], [1,0,1])));
- assertIdentical(10, Plane.XY.distanceFrom($L([4,8,10], [2,-6,0])));
- assertIdentical(0, Plane.XY.distanceFrom($L([4,8,10], [2,-6,2])));
- }},
-
- testContainment: function() { with(this) {
- assert(Plane.XY.contains(Line.X));
- assert(Plane.XY.contains(Vector.i));
- }},
-
- testPointClosestTo: function() { with(this) {
- assert(Plane.YZ.pointClosestTo([3,6,-3]).eql([0,6,-3]));
- }},
-
- testRotation: function() { with(this) {
- assert(Plane.XY.rotate(Math.PI/2, Line.Y).eql(Plane.YZ));
- }},
-
- testReflection: function() { with(this) {
- assert(Plane.XY.reflectionIn($V([12,65,-4])).eql($P([0,0,-8], Vector.k)));
- assert(Plane.XY.reflectionIn(Line.Z).eql(Plane.XY));
- assert(Plane.XY.reflectionIn($L([0,0,0], [1,0,1])).eql(Plane.YZ));
- assert($P([5,0,0], [1,1,0]).reflectionIn($P([5,0,0], [0,1,0])).eql($P([5,0,0], [-1,1,0])));
- assert($P([0,5,0], [0,1,1]).reflectionIn($P([0,5,0], [0,0,1])).eql($P([0,5,0], [0,-1,1])));
- assert($P([0,0,5], [1,0,1]).reflectionIn($P([0,0,5], [1,0,0])).eql($P([0,0,5], [1,0,-1])));
- }},
-
- testIntersection: function() { with(this) {
- var i, P1, P2, L1, L2;
- for (i = 0; i < 10; i++) {
- P1 = $P($V([-50,-50,-50]).add(Vector.Random(3).x(100)), $V([-50,-50,-50]).add(Vector.Random(3).x(100)));
- P2 = $P($V([-50,-50,-50]).add(Vector.Random(3).x(100)), $V([-50,-50,-50]).add(Vector.Random(3).x(100)));
- if (P1.intersects(P2)) {
- L1 = P1.intersectionWith(P2);
- L2 = P2.intersectionWith(P1);
- assert(L1.eql(L2));
- assert(L1.liesIn(P1));
- assert(P2.contains(L1));
- }
- }
- }}
-});
-
-</script>
-
-</body>
-</html>
View
74 test/polygons.html
@@ -1,74 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
-<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
-<title>Vectors / Matrices</title>
-<script src="../lib/sylvester.js" type="text/javascript"></script>
-<script src="lib/test/test.js" type="text/javascript"></script>
-<!--[if IE]><script src="excanvas-compressed.js" type="text/javascript"></script><![endif]-->
-<script type="text/javascript">
-
-var O = $V([300,225,0]);
-var Foo = Polygon.create([ [0,0], [5,0], [5,5], [0,5], [0,10], [5,10], [0,5],
- [5,5], [10,10], [10,15], [-5,15], [-5,5], [-10,5], [-10,0], [-5,0],
- [-5,-5], [0,-5] ]).scale(20, $V([0,5])).rotate(Math.PI/3, $L([0,5], [0,2,1]));
-var canvas, ctx;
-
-window.onload = function() {
-
- canvas = document.getElementById('canv');
- ctx = canvas.getContext('2d');
- ctx.fillColor = '#000000';
- ctx.fillRect(0,0, canvas.width, canvas.height);
-
- draw(ctx, Foo, 100);
-
- var trigs;
- trigs = Foo.toTriangles();
- var C = 255;
- for (var i = 0; i < trigs.length; i++) {
- draw(ctx, trigs[i], C);
- C -= Math.round((255 - 100) / trigs.length);
- }
-
- var c = Foo.centroid();
- ctx.fillStyle = 'red';
- ctx.fillRect(300 + c.e(1) - 5, 225 - c.e(2) - 5, 10, 10);
-};
-
-function draw(ctx, poly, color) {
- ctx.fillStyle = 'rgb(' + color +',' + color + ',' + color + ')';
- ctx.beginPath();
- poly.vertices.each(function(node) {
- point = O.add(node.data).reflectionIn($L(O, Vector.i));
- if (node.data == poly.vertices.first.data) {
- ctx.moveTo(point.e(1), point.e(2));
- } else {
- ctx.lineTo(point.e(1), point.e(2));
- }
- } );
- ctx.fill();
-}
-
-function rotate() {
- ctx.fillStyle = '#000000';
- ctx.fillRect(0,0, canvas.width, canvas.height);
- Foo.rotate(Math.PI/20, $L([0,5], [0,2,1]));
- var trigs = Foo.toTriangles();
- var C = 255;
- for (var i = 0; i < trigs.length; i++) {
- draw(ctx, trigs[i], C);
- C -= Math.round((255 - 100) / trigs.length);
- }
- setTimeout('rotate()', 20);
-}
-
-</script>
-</head>
-<body>
-
-<canvas id="canv" width="600" height="450" style="display: block; margin: 20px auto;" onclick="rotate();"></canvas>
-
-</body>
-</html>
View
11 test/runner.js
@@ -3,7 +3,10 @@ JS.Packages(function() { with(this) {
autoload(/.*Spec$/, {from: ROOT + '/test/specs', require: 'JS.Test'})
}})
-JS.require('VectorSpec', function() {
-
- JS.Test.autorun()
-})
+JS.require( 'VectorSpec',
+ 'MatrixSpec',
+ 'LineSpec',
+ 'LineSegmentSpec',
+ 'PlaneSpec',
+
+ function() { JS.Test.autorun() })
View
106 test/specs/line_segment_spec.js
@@ -0,0 +1,106 @@
+JS.ENV.LineSegmentSpec = JS.Test.describe("Line.Segment", function() { with(this) {
+ before(function() { with(this) {
+ this.Line = Sylvester.Line
+ this.$L = Line.create
+ }})
+
+ before(function() { with(this) {
+ segment = Line.Segment.create([5,5,5], [10,10,10])
+ segment2 = Line.Segment.create([1,1,0], [1,2,0])
+ }})
+
+ test("eql", function() { with(this) {
+ assert( segment.eql(segment) )
+ assert( ! segment.eql(segment2) )
+ assert( segment.eql(Line.Segment.create(segment.end, segment.start)) )
+ }})
+
+ test("dup", function() { with(this) {
+ var seg = segment.dup()
+ assert( segment.eql(seg) )
+ seg.start.setElements([23,87,56])
+ assert( ! segment.eql(seg) )
+ assert( segment.start.eql([5,5,5]) )
+ }})
+
+ test("length", function() { with(this) {
+ assert( segment.length() - Math.sqrt(75) <= Sylvester.precision )
+ }})
+
+ test("toVector", function() { with(this) {
+ assert( segment.toVector().eql([5,5,5]) )
+ }})
+
+ test("midpoint", function() { with(this) {
+ assert( segment.midpoint().eql([7.5,7.5,7.5]) )
+ }})
+
+ test("bisectingPlane", function() { with(this) {
+ assert( segment.bisectingPlane().eql(Sylvester.Plane.create([7.5,7.5,7.5], [1,1,1])) )
+ }})
+
+ test("translate", function() { with(this) {
+ assert( segment.translate([9,2,7]).eql(Line.Segment.create([14,7,12], [19,12,17])) )
+ }})
+
+ test("isParallelTo", function() { with(this) {
+ assert( segment2.isParallelTo(Line.Y) )
+ assert( ! segment2.isParallelTo(Line.Z) )
+ assert( segment2.isParallelTo(Sylvester.Plane.XY) )
+ assert( segment2.isParallelTo(Sylvester.Plane.YZ) )
+ assert( ! segment2.isParallelTo(Sylvester.Plane.ZX) )
+ assert( ! segment.isParallelTo(segment2) )
+ assert( segment2.isParallelTo(segment2) )
+ }})
+
+ test("contains", function() { with(this) {
+ assert( segment.contains(segment.midpoint()) )
+ assert( segment.contains([5,5,5]) )
+ assert( segment.contains([10,10,10]) )
+ assert( ! segment.contains([4.9999,4.9999,4.9999]) )
+ assert( ! segment.contains([10.00001, 10.00001, 10.00001]) )
+ assert( segment.contains(Line.Segment.create([5,5,5], [8,8,8])) )
+ assert( segment.contains(Line.Segment.create([7,7,7], [10,10,10])) )
+ assert( ! segment.contains(Line.Segment.create([4,4,4], [8,8,8])) )
+ }})
+
+ test("distanceFrom", function() { with(this) {
+ assertEqual( 5, segment.distanceFrom([5,5,0]) )
+ assertEqual( 2, segment.distanceFrom([10,12,10]) )
+ assertEqual( Math.sqrt(2* 25), segment.distanceFrom(Line.X) )
+ assertEqual( 1, segment.distanceFrom($L([11,10,10], [0,1])) )
+ assertEqual( 5, segment.distanceFrom(Sylvester.Plane.XY) )
+ assertEqual( Math.sqrt(4 + 25 + 25), segment.distanceFrom(Line.Segment.create([7,0,0], [9,0,0])) )
+ }})
+
+ test("intersection", function() { with(this) {
+ assert( ! segment.intersects(Line.X) )
+ assert( ! segment.intersects(Line.Y) )
+ assert( ! segment.intersects(Line.Z) )
+ assert( ! segment.intersects(Sylvester.Plane.XY) )
+ assert( ! segment.intersects(Sylvester.Plane.YZ) )
+ assert( ! segment.intersects(Sylvester.Plane.ZX) )
+ assert( segment.intersectionWith(segment.bisectingPlane()).eql(segment.midpoint()) )
+ assert(
+ Line.Segment.create([0,4,4], [0,8,4]).intersectionWith(
+ Line.Segment.create([0,6,2], [0,6,6])
+ ).eql([0,6,4])
+ )
+ assertNull(
+ Line.Segment.create([0,4,4], [0,8,4]).intersectionWith(
+ Line.Segment.create([2,6,2], [0,6,6])
+ )
+ )
+ assert( segment.intersects(Line.Segment.create([6,7,7], [9,7,7])) )
+ assert( ! segment.intersects(segment2) )
+ }})
+
+ test("pointClosestTo", function() { with(this) {
+ assertNull( segment2.pointClosestTo(Line.Y) )
+ assert( segment2.pointClosestTo(Line.X).eql([1,1,0]) )
+ assert( segment2.pointClosestTo(Line.X.translate([0,10])).eql([1,2,0]) )
+ assert( segment2.pointClosestTo($L([0,1.5,0], [0,0,1])).eql([1,1.5,0]) )
+ assert( segment2.pointClosestTo(Sylvester.Plane.XZ).eql([1,1,0]) )
+ assertNull( segment2.pointClosestTo(Sylvester.Plane.YZ) )
+ }})
+}})
View
91 test/specs/line_spec.js
@@ -0,0 +1,91 @@
+JS.ENV.LineSpec = JS.Test.describe("Line", function() { with(this) {
+ before(function() { with(this) {
+ this.Line = Sylvester.Line
+ this.$L = Line.create
+ }})
+
+ test("dup", function() { with(this) {
+ var L = Line.X.dup()
+ assert( L.eql(Line.X) )
+ L.anchor.setElements([8,2,5])
+ L.direction.setElements([2,5,6])
+ assert( Line.X.anchor.eql([0,0,0]) )
+ assert( Line.X.direction.eql(Sylvester.Vector.i) )
+ assert( ! L.eql(Line.X) )
+ }})
+
+ test("equality with antiparallel lines", function() { with(this) {
+ assert( Line.X.eql($L([0,0,0], [-12,0,0])) )
+ }})
+
+ test("contains", function() { with(this) {
+ assert( Line.X.contains([99,0,0]) )
+ assert( ! Line.X.contains([99,1,0]) )
+ assert( ! Line.X.contains([99,0,2]) )
+ assert( $L([0,0,0], [1,1,1]).contains(Line.Segment.create([-2,-2,-2], [13,13,13])) )
+ }})
+
+ test("isParallelTo", function() { with(this) {
+ assert( Line.X.isParallelTo($L([0,0,-12], [-4,0,0])) )
+ assert( Line.X.isParallelTo(Sylvester.Plane.create([0,0,-4], Sylvester.Vector.k)) )
+ assert( ! Line.Z.isParallelTo(Sylvester.Plane.create([0,0,-4], Sylvester.Vector.k)) )
+ assert( Line.Z.isParallelTo(Line.Segment.create([9,2,6], [9,2,44])) )
+ assert( ! Line.Z.isParallelTo(Line.Segment.create([9,3,6], [9,2,44])) )
+ }})
+
+ test("translate", function() { with(this) {
+ assert( Line.X.dup().translate([0,0,12]).eql($L([0,0,12], Sylvester.Vector.i)) )
+ }})
+
+ test("intersectionWith", function() { with(this) {
+ for (var i = 0, O, V, V1, V2, L1, L2; i < 5; i++) {
+ O = Sylvester.Vector.create([-5,-5,-5])
+ V = O.add(Sylvester.Vector.Random(3).x(10))
+ V1 = O.add(Sylvester.Vector.Random(3).x(10))
+ V2 = O.add(Sylvester.Vector.Random(3).x(10))
+ L1 = $L(V, V1)
+ L2 = $L(V.add(V1.x(-20 + 40*Math.random())), V2)
+ V = L1.intersectionWith(L2)
+ assert( L1.contains(V) )
+ assert( L2.contains(V) )
+ }
+ assert( $L([5,0], [0,1]).intersectionWith($L([0,0], [-1,-1])).eql([5,5,0]) )
+ assert( Line.X.intersects(Line.Segment.create([7,-4,0], [7,5,0])) )
+ assert( ! Line.X.intersects(Line.Segment.create([7,-4,-1], [7,5,0])) )
+ }})
+
+ test("positionOf", function() { with(this) {
+ assert( $L([0,0,0], [1,1,-1]).positionOf([3,3,-3]) - Math.sqrt(27) <= Sylvester.precision )
+ }})
+
+ test("pointClosestTo", function() { with(this) {
+ assert( Line.X.pointClosestTo(Sylvester.Vector.create([26,-2,18])).eql([26,0,0]) )
+ assert( $L([0,0,0], [1,0,0]).pointClosestTo($L([0,0,24], [1,1,0])).eql([0,0,0]) )
+ assert( $L([0,0,24], [1,1,0]).pointClosestTo($L([0,0,0], [-1,0,0])).eql([0,0,24]) )
+ assert( Line.X.pointClosestTo(Line.Segment.create([3,5], [9,9])).eql([3,0,0]) )
+ assert( Line.X.pointClosestTo(Line.Segment.create([2,-2,2], [4,2,2])).eql([3,0,0]) )
+ }})
+
+ test("distanceFrom", function() { with(this) {
+ assertEqual( 24, $L([0,0,0], [1,0,0] ).distanceFrom($L([0,0,24], [1,1,0])) )
+ assertEqual( 12, $L([12,0,0], Sylvester.Vector.k).distanceFrom(Sylvester.Plane.YZ) )
+ assertEqual( 0, $L([12,0,0], [1,0,200]).distanceFrom(Sylvester.Plane.YZ) )
+ assert( Math.abs(Math.sqrt(18) - Line.X.distanceFrom(Line.Segment.create([12,3,3], [15,4,3]))) <= Sylvester.precision )
+ }})
+
+ test("reflectionIn", function() { with(this) {
+ assert( Line.Z.reflectionIn([28,0,-12]).eql($L([56,0,0], Sylvester.Vector.k.x(-1))) )
+ assert( Line.X.reflectionIn($L([0,0,0],[1,0,1])).eql(Line.Z) )
+ var L1 = Line.X.dup()
+ var L2 = $L([5,0,0], Sylvester.Vector.k)
+ assert( L1.reflectionIn(Sylvester.Plane.create([5,0,0], [1,0,1])).eql(L2) )
+ assert( L2.reflectionIn(Sylvester.Plane.create([5,0,0], [1,0,1])).eql(L1) )
+ assert( $L([-4,3], [0,-1]).reflectionIn(Sylvester.Vector.create([0,0])).eql($L([4,100], [0,4])) )
+ }})
+
+ test("rotate", function() { with(this) {
+ assert( Line.X.rotate(Math.PI, $L([12,0,0],[1,0,1])).eql($L([12,0,0], Sylvester.Vector.k)) )
+ assert( $L([10,0,0], [0,1,1]).rotate(-Math.PI/2, Line.Y).eql($L([0,0,10], [1,-1,0])) )
+ assert( $L([9,0], Sylvester.Vector.j).rotate(Math.PI/2, Sylvester.Vector.create([9,9])).eql($L([0,9], Sylvester.Vector.i)) )
+ }})
+}})
View
322 test/specs/matrix_spec.js
@@ -0,0 +1,322 @@
+JS.ENV.MatrixSpec = JS.Test.describe("Matrix", function() { with(this) {
+ before(function() { with(this) {
+ this.Matrix = Sylvester.Matrix
+ this.$M = Matrix.create
+ }})
+
+ test("create", function() { with(this) {
+ var M = $M([
+ [0,3,4,8],
+ [3,9,7,3]
+ ])
+ assertEqual( '[0, 3, 4, 8]\n[3, 9, 7, 3]', M.inspect() )
+ assertEqual( '[0, 2, 7, 5]', $M([[0,2,7,5]]).inspect() )
+ assertEqual( '[0]\n[2]\n[7]\n[5]', $M([[0],[2],[7],[5]]).inspect() )
+ assertEqual( '[128]', $M([[128]]).inspect() )
+ }})
+
+ test("e", function() { with(this) {
+ var M = $M([
+ [0,3,4,8],
+ [3,9,7,3]
+ ])
+ assertEqual( 0, M.e(1,1) )
+ assertEqual( 8, M.e(1,4) )
+ assertNull( M.e(2,6) )
+ }})
+
+ test("rows and columns", function() { with(this) {
+ var M = $M([
+ [0,3,4,8],
+ [3,9,7,3]
+ ])
+ assert( M.row(2).eql([3,9,7,3]) )
+ assertNull( M.row(3) )
+ assert( M.col(2).eql([3,9]) )
+ assertNull( M.col(6) )
+ }})
+
+ test("dimensions", function() { with(this) {
+ var M = $M([
+ [0,3,4,8],
+ [3,9,7,3]
+ ])
+ assertEqual( 2, M.rows() )
+ assertEqual( 4, M.cols() )
+ }})
+
+ test("dup", function() { with(this) {
+ var M1 = $M([
+ [2,3,8],
+ [7,0,2],
+ [6,3,0]
+ ])
+ var M2 = M1.dup()
+ assert( M1.eql(M2) )
+ M2.elements[1][1] = 99
+ assert( ! M1.eql(M2) )
+ assertEqual( 0, M1.elements[1][1] )
+ }})
+
+ test("eql", function() { with(this) {
+ var M = $M([
+ [2,3,8],
+ [7,0,2],
+ [6,3,0]
+ ])
+ assert( M.eql([[2,3,8], [7,0,2], [6,3,0]]) )
+ assert( ! M.eql([[7,3,8], [7,0,2], [6,3,0]]) )
+ assert( ! M.eql([[2,7,8], [7,0,2], [6,3,0]]) )
+ assert( ! M.eql([[2,3,7], [7,0,2], [6,3,0]]) )
+ assert( ! M.eql([[2,3,8], [8,0,2], [6,3,0]]) )
+ assert( ! M.eql([[2,3,8], [7,7,2], [6,3,0]]) )
+ assert( ! M.eql([[2,3,8], [7,0,7], [6,3,0]]) )
+ assert( ! M.eql([[2,3,8], [7,0,2], [7,3,0]]) )
+ assert( ! M.eql([[2,3,8], [7,0,2], [6,7,0]]) )
+ assert( ! M.eql([[2,3,8], [7,0,2], [6,3,7]]) )
+ }})
+
+ test("map", function() { with(this) {
+ assert(
+ $M([
+ [2,3,8],
+ [7,0,2],
+ [6,3,0]
+ ]).map(function(x, i, j) { return x + j }).eql([
+ [3,5,11],
+ [8,2,5],
+ [7,5,3]
+ ])
+ )
+ }})
+
+ test("Random", function() { with(this) {
+ var M
+ for (var i = 1; i < 5; i++) {
+ M = Matrix.Random(4,i)
+ assertEqual( 4, M.rows() )
+ assertEqual( i, M.cols() )
+ M = Matrix.Random(i,3)
+ assertEqual( i, M.rows() )
+ assertEqual( 3, M.cols() )
+ }
+ }})
+
+ test("Zero", function() { with(this) {
+ var M
+ for (var i = 1; i < 5; i++) {
+ M = Matrix.Random(5,i)
+ assertEqual( 5, M.rows() )
+ assertEqual( i, M.cols() )
+ M = Matrix.Random(i,2)
+ assertEqual( i, M.rows() )
+ assertEqual( 2, M.cols() )
+ }
+ }})
+
+ test("isSameSizeAs", function() { with(this) {
+ assert( Matrix.Random(2,5).isSameSizeAs(Matrix.Zero(2,5)) )
+ assert( ! Matrix.Random(2,6).isSameSizeAs(Matrix.Zero(2,5)) )
+ assert( ! Matrix.Random(1,5).isSameSizeAs(Matrix.Zero(2,5)) )
+ }})
+
+ test("arithmetic", function() { with(this) {
+ var M1 = $M([
+ [2,5,9,3],
+ [9,2,8,5]
+ ])
+ var M2 = $M([
+ [7,1,0,8],
+ [0,4,3,8]
+ ])
+ var M = $M([
+ [9,6, 9,11],
+ [9,6.0,11,13]
+ ])
+ assert( M1.add(M2).eql(M) )
+ assert( M2.add(M1).eql(M) )
+ assertNull( M1.add(Matrix.Zero(2,5)) )
+ M = $M([
+ [-5,4,9.0,-5],
+ [9,-2,5,-3]
+ ])
+ assert( M1.subtract(M2).eql(M) )
+ assert( M2.subtract(M1).eql(M.x(-1)) )
+ assertNull( M1.subtract(Matrix.Zero(2,7)) )
+ assert(M2.x(3).eql([
+ [21,3,0,24],
+ [0,12,9,24]
+ ]))
+ }})
+
+ test("multiplication", function() { with(this) {
+ var M1 = $M([
+ [2,5,9,3],
+ [9,2,8,5]
+ ])
+ var M2 = $M([
+ [2,9],
+ [0,2],
+ [8,1],
+ [0,6]
+ ])
+ assertEqual( 2, M1.x(M2).rows() )
+ assertEqual( 2, M1.x(M2).cols() )
+ assert(M1.x(M2).eql([
+ [76, 55],
+ [82, 123]
+ ]))
+ assertEqual( 4, M2.x(M1).rows() )
+ assertEqual( 4, M2.x(M1).cols() )
+ assertNull( M1.x(M1.x(M2)) )
+ assertNotNull( M1.x(M2.x(M1)) )
+ }})
+
+ test("minor", function() { with(this) {
+ var M2 = $M([
+ [2,9],
+ [0,2],
+ [8,1],
+ [0,6]
+ ])
+ var M = $M([
+ [9,2,9],
+ [2,0,2],
+ [1,8,1]
+ ])
+ assert( M2.minor(1,2,3,3).eql(M) )
+ }})
+
+ test("isSquare", function() { with(this) {
+ assert( Matrix.Zero(9,9).isSquare() )
+ assert( ! Matrix.Zero(4,9).isSquare() )
+ assert( ! Matrix.Zero(9,3).isSquare() )
+ }})
+
+ test("max and index", function() { with(this) {
+ var M = $M([
+ [2,5,9,3],
+ [9,2,8,5]
+ ])
+ assertEqual( 9, M.max() )
+ assert( M.indexOf(8).i == 2 && M.indexOf(8).j == 3 )
+ assert( M.indexOf(9).i == 1 && M.indexOf(9).j == 3 )
+ }})
+
+ test("diagonal", function() { with(this) {
+ var M = $M([
+ [9,2,9],
+ [2,0,2],
+ [1,8,1]
+ ])
+ assert( M.diagonal().eql([9,0,1]) )
+ }})
+
+ test("toRightTriangular", function() { with(this) {
+ for (var i = 0, M; i < 8; i++) {
+ M = Matrix.Random(3,3);
+ assertMatch( /^\[[0-9\-\.]+, [0-9\-\.]+, [0-9\-\.]+\]\n\[0, [0-9\-\.]+, [0-9\-\.]+\]\n\[0, 0, [0-9\-\.]+\]$/,
+ M.toRightTriangular().inspect() )
+ }
+ }})
+
+ test("transpose", function() { with(this) {
+ var M1 = $M([
+ [3,9,8,4],
+ [2,0,1,5]
+ ])
+ var M2 = $M([
+ [3,2],
+ [9,0],
+ [8,1],
+ [4,5]
+ ])
+ assert( M1.transpose().eql(M2) )
+ assert( M2.transpose().eql(M1) )
+ }})
+
+ test("determinant", function() { with(this) {
+ for (var i = 0, M; i < 5; i++) {
+ M = Matrix.Random(3,3).x(10).elements
+ assert(
+ M[0][0] * (M[1][1]*M[2][2] - M[1][2]*M[2][1]) +
+ M[0][1] * (M[1][2]*M[2][0] - M[1][0]*M[2][2]) +
+ M[0][2] * (M[1][0]*M[2][1] - M[1][1]*M[2][0]) -
+ $M(M).determinant()
+ < Sylvester.precision
+ )
+ }
+ assertNull( Matrix.Random(3,4).determinant() )
+ }})
+
+ test("isSingular", function() { with(this) {
+ var M = Matrix.Random(3,3).x(10)
+ M.elements[0][0] = M.elements[1][0] = M.elements[2][0] = 0
+ assert( M.isSingular() )
+ assert( ! Matrix.Zero(4,3).isSingular() )
+ }})
+
+ test("trace", function() { with(this) {
+ var M = $M([
+ [8,1,6],
+ [0,1,7],
+ [0,1,5]
+ ])
+ assertEqual( 14, M.tr() )
+ assertNull( Matrix.Random(4,5).tr() )
+ }})
+
+ test("rank", function() { with(this) {
+ var M = $M([
+ [1,9,4,6],
+ [9,2,7,4],
+ [18,4,14,8]
+ ])
+ assertEqual( 2, M.rk() )
+ }})
+
+ test("augment", function() { with(this) {
+ assert($M([
+ [7,2,9,4],
+ [4,8,2,6],
+ [9,2,5,6]
+ ]).augment([
+ [4,6],
+ [5,2],
+ [8,2]
+ ]).eql([
+ [7,2,9,4,4,6],
+ [4,8,2,6,5,2],
+ [9,2,5,6,8,2]
+ ]))
+ }})
+
+ test("inverse", function() { with(this) {
+ for (var i = 0, M; i < 10; i++) {
+ M = Matrix.Random(4,4).x(5)
+ if (M.isSingular()) { continue; }
+ assert( M.x(M.inv()).eql(Matrix.I(4)) )
+ assert( M.inv().x(M).eql(Matrix.I(4)) )
+ }
+ }})
+
+ test("Rotation", function() { with(this) {
+ assert(Matrix.Rotation(Math.PI/2).eql([
+ [0,-1], [1,0]
+ ]))
+ assert(Matrix.Rotation(Math.PI/2, Sylvester.Vector.j).eql([
+ [0,0,1],
+ [0,1,0],
+ [-1,0,0]
+ ]))
+ }})
+
+ test("Diagonal", function() { with(this) {
+ assert(Matrix.Diagonal([3,9,5,7]).eql([
+ [3,0,0,0],
+ [0,9,0,0],
+ [0,0,5,0],
+ [0,0,0,7]
+ ]))
+ }})
+}})
View
75 test/specs/plane_spec.js
@@ -0,0 +1,75 @@
+JS.ENV.PlaneSpec = JS.Test.describe("Plane", function() { with(this) {
+ before(function() { with(this) {
+ this.Plane = Sylvester.Plane
+ this.$P = Plane.create
+ }})
+
+ test("eql", function() { with(this) {
+ assert( Plane.XY.dup().eql($P([34,-99,0], [0,0,-4])) )
+ assert( ! Plane.XY.dup().eql($P([34,-99,1], [0,0,-4])) )
+ assert( ! Plane.XY.dup().eql($P([34,-99,0], [1,0,-4])) )
+ assert( ! Plane.XY.dup().eql($P([34,-99,0], [0,-1,-4])) )
+ }})
+
+ test("dup", function() { with(this) {
+ var P = Plane.XY.dup()
+ P.anchor.setElements([3,4,5])
+ P.normal.setElements([0,2,6])
+ assert( Plane.XY.anchor.eql([0,0,0]) )
+ assert( Plane.XY.normal.eql(Sylvester.Vector.k) )
+ }})
+
+ test("translate", function() { with(this) {
+ assert( Plane.XY.translate([5,12,-14]).eql($P([89,-34,-14], Sylvester.Vector.k)) )
+ assert( Plane.XY.anchor.eql(Sylvester.Vector.Zero(3)) )
+ }})
+
+ test("isParallelTo", function() { with(this) {
+ assert( Plane.XY.dup().translate([5,12,-14]).isParallelTo(Plane.XY) )
+ assert( Plane.XY.isParallelTo(Sylvester.Line.create([4,8,10], [2,-6,0])) )
+ }})
+
+ test("distanceFrom", function() { with(this) {
+ assertEqual( 14, Plane.XY.dup().translate([5,12,-14]).distanceFrom(Plane.XY) )
+ assertEqual( 0, Plane.XY.dup().translate([5,12,-14]).distanceFrom($P([0,0,0], [1,0,1])) )
+ assertEqual( 10, Plane.XY.distanceFrom(Sylvester.Line.create([4,8,10], [2,-6,0])) )
+ assertEqual( 0, Plane.XY.distanceFrom(Sylvester.Line.create([4,8,10], [2,-6,2])) )
+ }})
+
+ test("contains", function() { with(this) {
+ assert( Plane.XY.contains(Sylvester.Line.X) )
+ assert( Plane.XY.contains(Sylvester.Vector.i) )
+ }})
+
+ test("pointClosestTo", function() { with(this) {
+ assert( Plane.YZ.pointClosestTo([3,6,-3]).eql([0,6,-3]) )
+ }})
+
+ test("rotate", function() { with(this) {
+ assert( Plane.XY.rotate(Math.PI/2, Sylvester.Line.Y).eql(Plane.YZ) )
+ }})
+
+ test("reflectionIn", function() { with(this) {
+ assert( Plane.XY.reflectionIn(Sylvester.Vector.create([12,65,-4])).eql($P([0,0,-8], Sylvester.Vector.k)) )
+ assert( Plane.XY.reflectionIn(Sylvester.Line.Z).eql(Plane.XY) )
+ assert( Plane.XY.reflectionIn(Sylvester.Line.create([0,0,0], [1,0,1])).eql(Plane.YZ) )
+ assert( $P([5,0,0], [1,1,0]).reflectionIn($P([5,0,0], [0,1,0])).eql($P([5,0,0], [-1,1,0])) )
+ assert( $P([0,5,0], [0,1,1]).reflectionIn($P([0,5,0], [0,0,1])).eql($P([0,5,0], [0,-1,1])) )
+ assert( $P([0,0,5], [1,0,1]).reflectionIn($P([0,0,5], [1,0,0])).eql($P([0,0,5], [1,0,-1])) )
+ }})
+
+ test("containment", function() { with(this) {
+ var i, P1, P2, L1, L2, Vector = Sylvester.Vector
+ for (i = 0; i < 10; i++) {
+ P1 = $P(Vector.create([-50,-50,-50]).add(Vector.Random(3).x(100)), Vector.create([-50,-50,-50]).add(Vector.Random(3).x(100)))
+ P2 = $P(Vector.create([-50,-50,-50]).add(Vector.Random(3).x(100)), Vector.create([-50,-50,-50]).add(Vector.Random(3).x(100)))
+ if (P1.intersects(P2)) {
+ L1 = P1.intersectionWith(P2)
+ L2 = P2.intersectionWith(P1)
+ assert( L1.eql(L2) )
+ assert( L1.liesIn(P1) )
+ assert( P2.contains(L1) )
+ }
+ }
+ }})
+}})
View
169 test/specs/vector_spec.js
@@ -4,8 +4,175 @@ JS.ENV.VectorSpec = JS.Test.describe("Vector", function() { with(this) {
this.$V = Vector.create
}})
- it("create", function() { with(this) {
+ test("create", function() { with(this) {
assertEqual( '[0, 1, 7, 5]', $V([0, 1, 7, 5]).inspect() )
assertEqual( '[0, 1.4, 7.034, 5.28638]', $V([0, 1.4, 7.034, 5.28638]).inspect() )
}})
+
+ test("e", function() { with(this) {
+ var V = $V([0,3,4,5])
+ assertEqual( 0, V.e(1) )
+ assertEqual( 5, V.e(4) )
+ assertEqual( null, V.e(5) )
+ }})
+
+ test("Zero", function() { with(this) {
+ assertEqual( '[0, 0, 0, 0]', Vector.Zero(4).inspect() )
+ for (var i = 1; i < 8; i++) {
+ assertEqual( 0, Vector.Zero(i).modulus() )
+ assertEqual( i, Vector.Zero(i).dimensions() )
+ }
+ }})
+
+ test("Random", function() { with(this) {
+ for (var i = 1; i < 8; i++) {
+ assertEqual( i, Vector.Random(i).dimensions() )
+ }
+ }})
+
+ test("modulus", function() { with(this) {
+ assertEqual( Math.sqrt(50), $V([0,3,4,5]).modulus() )
+ assertEqual( 1, Vector.i.modulus() )
+ }})
+
+ test("dimensions", function() { with(this) {
+ assertEqual( 4, $V([0,3,4,5]).dimensions() )
+ }})
+
+ test("eql", function() { with(this) {
+ var V = Vector.Random(6)
+ assert( V.eql(V) )
+ assert( Vector.Zero(3).eql([0,0,0]) )
+ assert( $V([3,6,9]).eql([3.0,6.0,9.0]) )
+ assert( ! $V([3.01,6,9]).eql([3.0,6.0,9.0]) )
+ assert( ! $V([3,6,9]).eql([3,6,10]) )
+ assert( ! $V([3,6,9]).eql([3,7,9]) )
+ assert( ! $V([3,6,9]).eql([4,6,9]) )
+ }})
+
+ test("single element", function() { with(this) {
+ var V = $V([4])
+ assertEqual( '[4]', V.inspect() )
+ assertEqual( 4, V.modulus() )
+ }})
+
+ test("dup", function() { with(this) {
+ var V = $V([3,4,5])
+ var dup = V.dup()
+ assert( V.eql(dup) )
+ dup.elements[0] = 24
+ assert( V.eql([3,4,5]) )
+ assert( dup.eql([24,4,5]) )
+ }})
+
+ test("map", function() { with(this) {
+ var V = $V([1,6,3,9])
+ assert( V.map(function(x) { return x*x }).eql([1,36,9,81]) )
+ }})
+
+ test("normalize", function() { with(this) {
+ var V = $V([8,2,9,4])
+ assertEqual( 1, V.toUnitVector().modulus() )
+ assert( V.toUnitVector().x(Math.sqrt(165)).eql(V) )
+ assert( V.toUnitVector().isParallelTo(V) )
+ }})
+
+ test("angleFrom", function() { with(this) {
+ var k = Sylvester.precision
+ assertEqual( Math.PI/2, Vector.i.angleFrom(Vector.j) )
+ assertEqual(
+ Math.round((Math.PI/4)*k)/k,
+ Math.round(($V([1,0]).angleFrom($V([1,1])))*k)/k
+ )
+ assertNull( Vector.i.angleFrom([1,6,3,5]) )
+ }})
+
+ test("angle types", function() { with(this) {
+ assert( Vector.i.isParallelTo(Vector.i.x(235457)) )
+ assertNull( Vector.i.isParallelTo([8,9]) )
+ assert( Vector.i.isAntiparallelTo(Vector.i.x(-235457)) )
+ assertNull( Vector.i.isAntiparallelTo([8,9]) )
+ assert( Vector.i.isPerpendicularTo(Vector.k) )
+ assertNull( Vector.i.isPerpendicularTo([8,9,0,3]) )
+ }})
+
+ test("arithmetic", function() { with(this) {
+ var V1 = $V([2,9,4])
+ var V2 = $V([5,13,7])
+ assert( V1.add(V2).eql([7,22,11]) )
+ assert( V1.subtract(V2).eql([-3,-4,-3]) )
+ assertNull( V1.add([2,8]) )
+ assertNull( V1.subtract([9,3,6,1,7]) )
+ assert( V1.x(4).eql([8,36,16]) )
+ }})
+
+ test("products", function() { with(this) {
+ var V1 = $V([2,9,4])
+ var V2 = $V([5,13,7])
+ assertEqual( 2* 5 + 9*13 + 4*7, V1.dot(V2) )
+ assert( V1.cross(V2).eql([9*7-4*13, 4*5-2*7, 2*13-9*5]) )
+ assertNull( V1.dot([7,9]) )
+ assertNull( V2.cross([9,1,4,3]) )
+ }})
+
+ test("max", function() { with(this) {
+ var V = $V([2,8,5,9,3,7,12])
+ assertEqual( 12, V.max() )
+ V = $V([-17,8,5,9,3,7,12])
+ assertEqual( -17, V.max() )
+ }})
+
+ test("indexOf", function() { with(this) {
+ var V = $V([2,6,0,3])
+ assertEqual( 1, V.indexOf(2) )
+ assertEqual( 4, V.indexOf(3) )
+ assertEqual( 2, V.indexOf(V.max()) )
+ assertNull( V.indexOf(7) )
+ }})
+
+ test("toDiagonalMatrix", function() { with(this) {
+ assert(
+ $V([2,6,4,3]).toDiagonalMatrix().eql([
+ [2,0,0,0],
+ [0,6,0,0],
+ [0,0,4,0],
+ [0,0,0,3]
+ ])
+ )
+ }})
+
+ test("round", function() { with(this) {
+ assert( $V([2.56, 3.5, 3.49]).round().eql([3,4,3]) )
+ }})
+
+ test("distanceFrom", function() { with(this) {
+ assertEqual( $V([1,9,0,13]).modulus(), $V([3,9,4,6]).distanceFrom([2,0,4,-7]) )
+ assertEqual( Math.sqrt(64 + 49), $V([2,8,7]).distanceFrom(Sylvester.Line.X) )
+ assertEqual( 78, $V([28,-43,78]).distanceFrom(Sylvester.Plane.XY) )
+ assertEqual( 5, $V([7,4,0]).distanceFrom(Sylvester.Line.Segment.create([0,0,0], [4,0,0])) )
+ }})
+
+ test("liesIn", function() { with(this) {
+ assert( $V([12,0,0]).liesOn(Sylvester.Line.X) )
+ assert( ! $V([12,1,0]).liesOn(Sylvester.Line.X) )
+ assert( ! $V([12,0,3]).liesOn(Sylvester.Line.X) )
+ assert( $V([9,16,4]).liesOn(Sylvester.Line.Segment.create([2,9,4], [14,21,4])) )
+ assert( ! $V([9,17,4]).liesOn(Sylvester.Line.Segment.create([2,9,4], [14,21,4])) )
+ assert( $V([0,-3,6]).liesIn(Sylvester.Plane.YZ) )
+ assert( ! $V([4,-3,6]).liesIn(Sylvester.Plane.YZ) )
+ }})
+
+ test("reflectionIn", function() { with(this) {
+ assert( $V([3,0,0]).reflectionIn([0,3,0]).eql([-3,6,0]) )
+ assert( $V([3,0,0]).reflectionIn(Sylvester.Line.create([0,0,0], [1,0,1])).eql([0,0,3]) )
+ var V1 = $V([25,-48,77])
+ var V2 = $V([25,-48,-77])
+ assert( V1.reflectionIn(Sylvester.Plane.XY).eql(V2) )
+ assert( V2.reflectionIn(Sylvester.Plane.YX).eql(V1) )
+ }})
+
+ test("rotate", function() { with(this) {
+ assert( $V([12,1]).rotate(Math.PI/2, [5,1]).eql([5,8]) )
+ assert( Vector.i.rotate(-Math.PI/2, Sylvester.Line.create([10, 0, 100], Vector.k)).eql([10,9,0]) )
+ }})
}})
View
200 test/vector.html
@@ -1,200 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
- <title>Vector tests</title>
- <script src="lib/prototype.js" type="text/javascript" charset="utf-8"></script>
- <script src="lib/unittest.js" type="text/javascript" charset="utf-8"></script>
- <script src="../lib/sylvester.js" type="text/javascript" charset="utf-8"></script>
- <link rel="stylesheet" href="lib/test.css" type="text/css" />
-</head>
-<body>
-
-<h1 id="vector_test_suite">Vector test suite</h1>
-
-<div id="testlog">
-
-</div>
-
-<script type="text/javascript" charset="utf-8">
-
-new Test.Unit.Runner({
- testCreation: function() { with(this) {
- assertIdentical('[0, 1, 7, 5]', $V([0, 1, 7, 5]).inspect());
- assertIdentical('[0, 1.4, 7.034, 5.28638]', $V([0, 1.4, 7.034, 5.28638]).inspect());
- }},
-
- testElementRetrieval: function() { with(this) {
- var V = $V([0,3,4,5]);
- assertIdentical(0, V.e(1));
- assertIdentical(5, V.e(4));
- assertIdentical(null, V.e(5));
- }},
-
- testZeroVector: function() { with(this) {
- assertIdentical('[0, 0, 0, 0]', Vector.Zero(4).inspect());
- for (var i = 1; i < 8; i++) {
- assertIdentical(0, Vector.Zero(i).modulus());
- assertIdentical(i, Vector.Zero(i).dimensions());
- }
- }},
-
- testRandomVector: function() { with(this) {
- for (var i = 1; i < 8; i++) {
- assertIdentical(i, Vector.Random(i).dimensions());
- }
- }},
-
- testModulus: function() { with(this) {
- assertIdentical(Math.sqrt(50), $V([0,3,4,5]).modulus());
- assertIdentical(1, Vector.i.modulus());
- }},
-
- testDimensions: function() { with(this) {
- assertIdentical(4, $V([0,3,4,5]).dimensions());
- }},
-
- testEquality: function() { with(this) {
- var V = Vector.Random(6);
- assert(V.eql(V));
- assert(Vector.Zero(3).eql([0,0,0]));
- assert($V([3,6,9]).eql([3.0,6.0,9.0]));
- assert(!$V([3.01,6,9]).eql([3.0,6.0,9.0]));
- assert(!$V([3,6,9]).eql([3,6,10]));
- assert(!$V([3,6,9]).eql([3,7,9]));
- assert(!$V([3,6,9]).eql([4,6,9]));
- }},
-
- testSingleElement: function() { with(this) {
- var V = $V([4]);
- assertIdentical('[4]', V.inspect());
- assertIdentical(4, V.modulus());
- }},
-
- testDuplication: function() { with(this) {
- var V = $V([3,4,5]);
- var dup = V.dup();
- assert(V.eql(dup));
- dup.elements[0] = 24;
- assert(V.eql([3,4,5]));
- assert(dup.eql([24,4,5]));
- }},
-
- testMapping: function() { with(this) {
- var V = $V([1,6,3,9]);
- assert(V.map(function(x) { return x*x; }).eql([1,36,9,81]));
- }},
-
- testNormalization: function() { with(this) {
- var V = $V([8,2,9,4]);
- assertIdentical(1, V.toUnitVector().modulus());
- assert(V.toUnitVector().x(Math.sqrt(165)).eql(V));
- assert(V.toUnitVector().isParallelTo(V));
- }},
-
- testAngleFrom: function() { with(this) {
- var k = Sylvester.precision;
- assertIdentical(Math.PI/2, Vector.i.angleFrom(Vector.j));
- assertIdentical(
- Math.round((Math.PI/4)*k)/k,
- Math.round(($V([1,0]).angleFrom($V([1,1])))*k)/k
- );
- assertNull(Vector.i.angleFrom([1,6,3,5]));
- }},
-
- testAngleTypes: function() { with(this) {
- assert(Vector.i.isParallelTo(Vector.i.x(235457)));
- assertNull(Vector.i.isParallelTo([8,9]));
- assert(Vector.i.isAntiparallelTo(Vector.i.x(-235457)));
- assertNull(Vector.i.isAntiparallelTo([8,9]));
- assert(Vector.i.isPerpendicularTo(Vector.k));
- assertNull(Vector.i.isPerpendicularTo([8,9,0,3]));
- }},
-
- testArithmetic: function() { with(this) {
- var V1 = $V([2,9,4]);
- var V2 = $V([5,13,7]);
- assert(V1.add(V2).eql([7,22,11]));
- assert(V1.subtract(V2).eql([-3,-4,-3]));
- assertNull(V1.add([2,8]));
- assertNull(V1.subtract([9,3,6,1,7]));
- assert(V1.x(4).eql([8,36,16]));
- }},
-
- testProducts: function() { with(this) {
- var V1 = $V([2,9,4]);
- var V2 = $V([5,13,7]);
- assertIdentical(2* 5 + 9*13 + 4*7, V1.dot(V2));
- assert(V1.cross(V2).eql([9*7-4*13, 4*5-2*7, 2*13-9*5]));
- assertNull(V1.dot([7,9]));
- assertNull(V2.cross([9,1,4,3]));
- }},
-
- testMaximum: function() { with(this) {
- var V = $V([2,8,5,9,3,7,12]);
- assertIdentical(12, V.max());
- V = $V([-17,8,5,9,3,7,12]);
- assertIdentical(-17, V.max());
- }},
-
- testIndexOf: function() { with(this) {
- var V = $V([2,6,0,3]);
- assertIdentical(1, V.indexOf(2));
- assertIdentical(4, V.indexOf(3));
- assertIdentical(2, V.indexOf(V.max()));
- assertNull(V.indexOf(7));
- }},
-
- testDiagonalMatrix: function() { with(this) {
- assert(
- $V([2,6,4,3]).toDiagonalMatrix().eql([
- [2,0,0,0],
- [0,6,0,0],
- [0,0,4,0],
- [0,0,0,3]
- ])
- );
- }},
-
- testRounding: function() { with(this) {
- assert($V([2.56, 3.5, 3.49]).round().eql([3,4,3]));
- }},
-
- testDistances: function() { with(this) {
- assertIdentical($V([1,9,0,13]).modulus(), $V([3,9,4,6]).distanceFrom([2,0,4,-7]));
- assertIdentical(Math.sqrt(64 + 49), $V([2,8,7]).distanceFrom(Line.X));
- assertIdentical(78, $V([28,-43,78]).distanceFrom(Plane.XY));
- assertIdentical(5, $V([7,4,0]).distanceFrom(Line.Segment.create([0,0,0], [4,0,0])));
- }},
-
- testContainedByLineOrPlane: function() { with(this) {
- assert($V([12,0,0]).liesOn(Line.X));
- assert(!$V([12,1,0]).liesOn(Line.X));
- assert(!$V([12,0,3]).liesOn(Line.X));
- assert($V([9,16,4]).liesOn(Line.Segment.create([2,9,4], [14,21,4])));
- assert(!$V([9,17,4]).liesOn(Line.Segment.create([2,9,4], [14,21,4])));
- assert($V([0,-3,6]).liesIn(Plane.YZ));
- assert(!$V([4,-3,6]).liesIn(Plane.YZ));
- }},
-
- testReflection: function() { with(this) {
- assert($V([3,0,0]).reflectionIn([0,3,0]).eql([-3,6,0]));
- assert($V([3,0,0]).reflectionIn($L([0,0,0], [1,0,1])).eql([0,0,3]));
- var V1 = $V([25,-48,77]);
- var V2 = $V([25,-48,-77]);
- assert(V1.reflectionIn(Plane.XY).eql(V2));
- assert(V2.reflectionIn(Plane.YX).eql(V1));
- }},
-
- testRotation: function() { with(this) {
- assert($V([12,1]).rotate(Math.PI/2, [5,1]).eql([5,8]));
- assert(Vector.i.rotate(-Math.PI/2, $L([10, 0, 100], Vector.k)).eql([10,9,0]));
- }}
-});
-
-</script>
-
-</body>
-</html>
Please sign in to comment.
Something went wrong with that request. Please try again.