Plane.js
Summary
This file defines the Plane class and its methods.
Class Summary
|
Plane |
The Plane class is a very useful definition of a plane. |
dependencies = ['Line'];
require(dependencies);
function Plane(base, v1, v2) {
if(base instanceof Plane) {
return new Plane(base.base, base.norm);
}
this.base = new point3D(0,0,0);
this.norm = new point3D(0,0,0);
if(!(base.dimension() && v1.dimension())) {
return null;
}
if(base.n > 3 || v1.n > 3) {
return null;
}
if(base.n == 2) {
this.base = new point3D(base.get(1), base.get(2), 0);
} else {
if(base instanceof point3D) {
this.base = new point3D(base);
} else if(base instanceof Vector) {
this.base = new point3D(base.get(1), base.get(2), base.get(3));
}
}
if(v2) {
this.norm = v1.cross(v2).normalize().toPoint();
} else {
this.norm = v1.normalize().toPoint();
}
}
Plane.prototype = {
equalTo: function(plane) {
return (this.contains(plane.base) && this.isParallel(plane));
},
translate: function(dx, dy, dz) {
var base = new point3D(this.base.x + dx, this.base.y + dy, this.base.z + dz);
return new Plane(base, this.norm);
},
isParallel: function(obj) {
if(obj instanceof Plane) {
var theta = this.norm.angleBetween(obj.norm);
return (Math.abs(theta) < Zero || Math.abs(theta - Math.PI) < Zero);
} else if(obj instanceof Line) {
return this.norm.isPerpendicular(obj.direction);
} else {
return null;
}
},
isPerpendicular: function(plane) {
return (Math.abs(this.norm.dot(plane.norm)) < Zero);
},
distanceFrom: function(obj) {
if(this.inersects(obj) || this.contains(obj)) {
return 0;
}
if(obj.base) {
return Math.abs(this.norm.dot(this.base.subtract(obj.base)));
} else {
return Math.abs(this.norm.dot(this.base.subtract(obj)));
}
},
contains: function(obj) {
if(obj.norm) {
return this.equalTo(obj);
}
if(obj.direction) {
return (this.contains(obj.base) && this.contains(obj.base.add(obj.direction)));
} else {
return (this.distanceFrom(obj) < Zero);
}
},
intersects: function(obj) {
if(typeof(obj.direction) == 'undefined' && typeof(obj.base) == 'undefined') {
return null;
}
return (!this.isParallel(obj));
},
intersection: function(obj) {
if(!this.intersects(obj)) {
return null;
}
if(obj instanceof Line) {
var mult = this.norm.dot(this.base.subtract(obj.base)) / this.norm.dot(obj.direction);
var vec = obj.base.add(obj.direction.multiplyBy(mult));
return new point3D(vec.get(1),vec.get(2),vec.get(3));
}
if(obj instanceof Plane) {
var direction = this.norm.cross(obj.norm).normalize();
var B = this.base, N = this.norm, O = obj.norm, C = obj.base;
var solver = Matrix.zero(2,2), i = 0;
while(solver.isSingular()) {
i++;
solver = new Matrix([
[ N.get((i%3)+1), N.get((i+1)%3 +1) ],
[ O.get((i%3)+1), O.get((i+1)%3 +1) ]
]);
}
var inverse = solver.invert();
var x = N.dot(B), y = O.dot(C);
var intersection = [
inverse.get(1,1)*x + inverse.get(1,2)*y,
inverse.get(2,1)*x + inverse.get(2,2)*y
];
var base = new Array();
for(var j = 1; j <=3; j++) {
base.push((i == j) ? 0 : intersection[(j + (5-i)%3)%3]);
}
return new Line(new Vector(base), direction);
}
},
pointClosestTo: function(point) {
if(!(point instanceof point3D || point instanceof Vector)) {
return null;
}
var P = point, B = this.base, N = this.norm;
var dot = B.subtract(P).dot(N);
var vec = P.add(N.multiplyBy(dot));
if(point instanceof point3D) {
return new point3D(vec.get(1), vec.get(2), vec.get(3));
} else if(point instanceof Vector) {
return new Vector(vec);
}
},
rotate: function(deg, line) {
var C = line.pointClosestTo(this.base), B = this.base, N = this.norm;
var P = B.subtract(C);
var newB = C.add(P.rotate(deg, line));
var newN = N.rotate(deg, line);
return new Plane(newB, newN);
},
toString: function() {
return "base: " + this.base + ", normal: " + this.norm;
}
}
Plane.XY = new Plane(point3D.zero, Vector.k);
Plane.YX = Plane.XY;
Plane.YZ = new Plane(point3D.zero, Vector.i);
Plane.ZY = Plane.YZ;
Plane.ZX = new Plane(point3D.zero, Vector.j);
Plane.XZ = Plane.ZX;
Documentation generated by
JSDoc on Mon Aug 11 13:58:31 2008