File size: 2,513 Bytes
1e40c2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Javascript/Canvas Textured 3D Renderer v0.3
 * Copyright (c) 2008 Jacob Seidelin, [email protected]
 * This software is free to use for non-commercial purposes. For anything else, please contact the author.
 * This is a version modified by Stefano Gioffre'.
 */

Canvas3D.Vec3 = function(vx, vy, vz) {
	this.x = vx;
	this.y = vy;
	this.z = vz;
}

Canvas3D.Vec3.prototype.set = function(vx, vy, vz) {
	this.x = vx;
	this.y = vy;
	this.z = vz;
}

Canvas3D.Vec3.prototype.addVector = function(V) {
	this.x += V.x;
	this.y += V.y;
	this.z += V.z;
	return this;
}

Canvas3D.Vec3.prototype.multiply = function(fScalar) {
	this.x *= fScalar;
	this.y *= fScalar;
	this.z *= fScalar;
	return this;
}

Canvas3D.Vec3.prototype.subVector = function(V) {
	this.x -= V.x;
	this.y -= V.y;
	this.z -= V.z;
	return this;
}

Canvas3D.Vec3.prototype.returnAdd = function(V) {
	return new Canvas3D.Vec3(this.x + V.x, this.y + V.y, this.z + V.z);
}

Canvas3D.Vec3.prototype.returnSub = function(V) {
	return new Canvas3D.Vec3(this.x - V.x, this.y - V.y, this.z - V.z);
}

Canvas3D.Vec3.prototype.clone = function() {
	return new Canvas3D.Vec3(this.x, this.y, this.z);
}

Canvas3D.Vec3.prototype.dot = function(V) {
	return ((this.x * V.x) + (this.y * V.y) + (this.z * V.z));
}

Canvas3D.Vec3.prototype.cross = function(V) {
	var vx = V.x;
	var vy = V.y;
	var vz = V.z;
	return new Canvas3D.Vec3((this.y * vz) - (this.z * vy), (this.z * vx) - (this.x * vz), (this.x * vy) - (this.y * vx));
}

Canvas3D.Vec3.prototype.length = function() {
	return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
}

Canvas3D.Vec3.prototype.unit = function() {
	var l = 1/Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
	return new Canvas3D.Vec3(this.x * l, this.y * l, this.z * l);
}

Canvas3D.Vec3.prototype.rotateX = function(a) {
	var ry = this.y;
	var rz = this.z;
	var c = Math.cos(a);
	var s = Math.sin(a);
	this.y = c * ry - s * rz;
	this.z = s * ry + c * rz;
}

Canvas3D.Vec3.prototype.rotateY = function(a) {
	var rx = this.x;
	var rz = this.z;
	var c = Math.cos(a);
	var s = Math.sin(a);
	this.x = c * rx - s * rz;
	this.z = s * rx + c * rz;
}

Canvas3D.Vec3.prototype.rotateZ = function(a) {
	var rx = this.x;
	var ry = this.y;
	var c = Math.cos(a);
	var s = Math.sin(a);
	this.x = c * rx - s * ry;
	this.y = s * rx + c * ry;
}

Canvas3D.Vec3.prototype.dist = function(oVec) {
	var x = oVec.x - this.x;
	var y = oVec.y - this.y;
	var z = oVec.z - this.z;
	return Math.sqrt(x*x + y*y + z*z);
}