Skip to content

Commit c343632

Browse files
committed
reworked Vector classes, no change in API, reordered methods, divide-by-zero prevention, removed use of .set() in methods for better performance
1 parent 95bb462 commit c343632

File tree

3 files changed

+219
-248
lines changed

3 files changed

+219
-248
lines changed

src/core/Vector2.js

Lines changed: 56 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @author mr.doob / http://mrdoob.com/
33
* @author philogb / http://blog.thejit.org/
4+
* @author egraether / http://egraether.com/
45
*/
56

67
THREE.Vector2 = function ( x, y ) {
@@ -27,100 +28,98 @@ THREE.Vector2.prototype = {
2728

2829
copy : function ( v ) {
2930

30-
this.set(
31-
32-
v.x,
33-
v.y
34-
35-
);
31+
this.x = v.x;
32+
this.y = v.y;
3633

3734
return this;
3835

3936
},
4037

41-
addSelf : function ( v ) {
38+
clone : function () {
4239

43-
this.set(
40+
return new THREE.Vector2( this.x, this.y );
4441

45-
this.x + v.x,
46-
this.y + v.y
42+
},
4743

48-
);
44+
45+
add : function ( v1, v2 ) {
46+
47+
this.x = v1.x + v2.x;
48+
this.y = v1.y + v2.y;
4949

5050
return this;
5151

5252
},
5353

54-
add : function ( v1, v2 ) {
54+
addSelf : function ( v ) {
5555

56-
this.set(
56+
this.x += v.x;
57+
this.y += v.y;
5758

58-
v1.x + v2.x,
59-
v1.y + v2.y
59+
return this;
60+
61+
},
6062

61-
);
63+
sub : function ( v1, v2 ) {
64+
65+
this.x = v1.x - v2.x;
66+
this.y = v1.y - v2.y;
6267

6368
return this;
6469

6570
},
6671

6772
subSelf : function ( v ) {
6873

69-
this.set(
70-
71-
this.x - v.x,
72-
this.y - v.y
73-
74-
);
74+
this.x -= v.x;
75+
this.y -= v.y;
7576

7677
return this;
7778

7879
},
7980

80-
sub : function ( v1, v2 ) {
81-
82-
this.set(
83-
84-
v1.x - v2.x,
85-
v1.y - v2.y
81+
multiplyScalar : function ( s ) {
8682

87-
);
83+
this.x *= s;
84+
this.y *= s;
8885

8986
return this;
9087

9188
},
9289

93-
multiplyScalar : function ( s ) {
90+
divideScalar : function ( s ) {
91+
92+
if ( s ) {
93+
94+
this.x /= s;
95+
this.y /= s;
9496

95-
this.set(
97+
} else {
9698

97-
this.x * s,
98-
this.y * s
99+
this.set( 0, 0 );
99100

100-
);
101+
}
101102

102103
return this;
103104

104105
},
105106

107+
106108
negate : function() {
107109

108-
this.set(
110+
return this.multiplyScalar( -1 );
109111

110-
- this.x,
111-
- this.y
112+
},
112113

113-
);
114+
dot : function ( v ) {
114115

115-
return this;
116+
return this.x * v.x + this.y * v.y;
116117

117118
},
118119

119-
unit : function () {
120-
121-
this.multiplyScalar( 1 / this.length() );
120+
lengthSq : function () {
122121

123-
return this;
122+
return this.x * this.x + this.y * this.y;
124123

125124
},
126125

@@ -130,15 +129,25 @@ THREE.Vector2.prototype = {
130129

131130
},
132131

133-
lengthSq : function () {
132+
normalize : function () {
134133

135-
return this.x * this.x + this.y * this.y;
134+
return this.divideScalar( this.length() );
136135

137136
},
138137

139-
clone : function () {
138+
setLength : function ( l ) {
140139

141-
return new THREE.Vector2( this.x, this.y );
140+
return this.normalize().multiplyScalar( l );
141+
142+
},
143+
144+
// deprecated: same as normalize
145+
unit : function () {
146+
147+
return this.normalize();
148+
149+
// this.multiplyScalar( 1 / this.length() );
150+
// return this;
142151

143152
}
144153

0 commit comments

Comments
 (0)