home *** CD-ROM | disk | FTP | other *** search
Text File | 2001-02-20 | 4.3 KB | 240 lines | [AMAS/AMAP] |
- // -* TSMath.js *-
- //
- // Description: Basic maths
- // Author:
- // Version: $Id: TSMath.js,v 1.5 2000/12/08 18:07:31 consumer Exp $
- //
-
- //
- // Conversions
- //
-
- function degToRad(angleDeg)
- {
- return angleDeg * Math.PI / 180;
- }
-
- function radToDeg(angleRad)
- {
- return angleRad / Math.PI * 180;
- }
-
- //
- // Point
- //
-
- function Point(x, y, z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
-
- function makePoint(x, y, z)
- {
- return new Point(x, y, z);
- }
-
- function makePointFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- var strSplit = str.split(" ");
- return new Point(strSplit[0], strSplit[1], strSplit[2]);
- }
-
- function makeStringFromPoint(pt)
- {
- return (pt.x + ' ' + pt.y + ' ' + pt.z);
- }
-
- //
- // Point operations
- //
-
- function pointRotateX(pt, angle)
- {
- new_x = pt.x
- new_y = (pt.y * Math.cos(angle)) + (pt.z * Math.sin(angle))
- new_z = (pt.z * Math.cos(angle)) - (pt.y * Math.sin(angle))
-
- return new Point(new_x, new_y, new_z)
- }
-
- function pointRotateY(pt, angle)
- {
- new_x = (pt.x * Math.cos(angle)) - (pt.z * Math.sin(angle))
- new_y = pt.y
- new_z = (pt.x * Math.sin(angle)) + (pt.z * Math.cos(angle))
-
- return new Point(new_x, new_y, new_z)
- }
-
- function pointRotateZ(pt, angle)
- {
- new_x = (pt.x * Math.cos(angle)) + (pt.y * Math.sin(angle))
- new_y = (pt.y * Math.cos(angle)) - (pt.x * Math.sin(angle))
- new_z = pt.z
-
- return new Point(new_x, new_y, new_z)
- }
-
- function pointSphericalRotate(theta, phi)
- {
- fphi = parseFloat(phi);
- ftheta = parseFloat(theta);
-
- new_x = Math.cos(fphi) * Math.sin(ftheta);
- new_y = Math.sin(fphi);
- new_z = Math.cos(fphi) * Math.cos(ftheta);
-
- return makePoint(new_x, new_y, new_z);
- }
-
- function pointNegate(pt)
- {
- return makePoint(-parseFloat(pt.x),
- -parseFloat(pt.y),
- -parseFloat(pt.z));
- }
-
- function pointTranslate(pt, x, y, z)
- {
- return makePoint(parseFloat(pt.x) + parseFloat(x),
- parseFloat(pt.y) + parseFloat(y),
- parseFloat(pt.z) + parseFloat(z));
- }
-
- function pointScale(pt, x, y, z)
- {
- return makePoint(parseFloat(pt.x) * parseFloat(x),
- parseFloat(pt.y) * parseFloat(y),
- parseFloat(pt.z) * parseFloat(z));
- }
-
- function pointMultiply(pt, c)
- {
- return makePoint(parseFloat(pt.x) * parseFloat(c),
- parseFloat(pt.y) * parseFloat(c),
- parseFloat(pt.z) * parseFloat(c));
- }
-
- function pointDistance(pt1, pt2)
- {
- return vectorLength(makeVector(pt1, pt2));
- }
-
- function pointInterpolate(pt1, pt2, alpha)
- {
- return makePoint(parseFloat(pt1.x) * (1.0 - alpha) + parseFloat(pt2.x) * alpha,
- parseFloat(pt1.y) * (1.0 - alpha) + parseFloat(pt2.y) * alpha,
- parseFloat(pt1.z) * (1.0 - alpha) + parseFloat(pt2.z) * alpha);
- }
-
- //
- // Vector operations
- // (The vector structure is a point)
- //
-
- function makeVector(pt1, pt2)
- {
- return makePoint(pt2.x - pt1.x, pt2.y - pt1.y, pt2.z - pt1.z);
- }
-
- function vectorLength(v)
- {
- return Math.sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
- }
-
- function vectorNormalize(v)
- {
- return pointMultiply(v, 1 / vectorLength(v));
- }
-
- //
- // Bounding box
- //
-
- function BoundingBox(pointMin, pointMax)
- {
- this.min = pointMin;
- this.max = pointMax;
- }
-
- function makeBoundingBox(pointMin, pointMax)
- {
- var bbox = new BoundingBox(pointMin, pointMax);
-
- // [HACK] Le constructeur n'a pas l'air de fonctionner!! :-(
- bbox.min = pointMin;
- bbox.max = pointMax;
-
- return bbox;
- }
-
- function makeBoundingBoxFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- var strSplit = str.split(" ");
- var min = new Point(strSplit[0], strSplit[1], strSplit[2]);
- var max = new Point(strSplit[3], strSplit[4], strSplit[5]);
-
- return makeBoundingBox(min, max);
- }
-
- function boundingBoxGetLengthX(bbox)
- {
- return (bbox.max.x - bbox.min.x);
- }
-
- function boundingBoxGetLengthY(bbox)
- {
- return (bbox.max.y - bbox.min.y);
- }
-
- function boundingBoxGetLengthZ(bbox)
- {
- return (bbox.max.z - bbox.min.z);
- }
-
- //
- // Color
- //
-
- function Color(r, g, b)
- {
- this.r = r;
- this.g = g;
- this.b = b;
- }
-
- function makeColor(r, g, b)
- {
- return new Color(r, g, b);
- }
-
- function makeColorFromString(str)
- {
- // Resolves bug on NS
- str = str + '\0';
- strSplit = str.split(" ");
- return new Color(strSplit[0], strSplit[1], strSplit[2]);
- }
-
- function makeStringFromColor(col)
- {
- return (col.r + ' ' + col.g + ' ' + col.b);
- }
-
- //
- // Color operations
- //
-
- function colorMultiply(col, c)
- {
- return makeColor(parseFloat(col.r) * c,
- parseFloat(col.g) * c,
- parseFloat(col.b) * c);
- }
-