Parcourir la source

Add directionTo() to Vector2 and Vector3

customisations
alemart il y a 10 mois
Parent
révision
f7b06c1d1b
4 fichiers modifiés avec 68 ajouts et 20 suppressions
  1. 14
    0
      docs/api/vector2.md
  2. 14
    0
      docs/api/vector3.md
  3. 20
    10
      src/geometry/vector2.ts
  4. 20
    10
      src/geometry/vector3.ts

+ 14
- 0
docs/api/vector2.md Voir le fichier

@@ -30,6 +30,20 @@ Compute the length of the vector: `sqrt(x^2 + y^2)`.
30 30
 
31 31
 The length of the vector.
32 32
 
33
+### directionTo
34
+
35
+`vector.directionTo(v: Vector2): Vector2`
36
+
37
+Compute a unit vector pointing to `v` from `this`.
38
+
39
+**Arguments**
40
+
41
+* `v: Vector2`. A vector.
42
+
43
+**Returns**
44
+
45
+A new unit vector pointing to `v` from `this`.
46
+
33 47
 ### equals
34 48
 
35 49
 `vector.equals(v: Vector2): boolean`

+ 14
- 0
docs/api/vector3.md Voir le fichier

@@ -36,6 +36,20 @@ Compute the length of the vector: `sqrt(x^2 + y^2 + z^2)`.
36 36
 
37 37
 The length of the vector.
38 38
 
39
+### directionTo
40
+
41
+`vector.directionTo(v: Vector3): Vector3`
42
+
43
+Compute a unit vector pointing to `v` from `this`.
44
+
45
+**Arguments**
46
+
47
+* `v: Vector3`. A vector.
48
+
49
+**Returns**
50
+
51
+A new unit vector pointing to `v` from `this`.
52
+
39 53
 ### equals
40 54
 
41 55
 `vector.equals(v: Vector3): boolean`

+ 20
- 10
src/geometry/vector2.ts Voir le fichier

@@ -72,6 +72,16 @@ export class Vector2
72 72
     }
73 73
 
74 74
     /**
75
+     * Compute the direction from this to v
76
+     * @param v a vector
77
+     * @returns a new unit vector pointing to v from this
78
+     */
79
+    directionTo(v: Vector2): Vector2
80
+    {
81
+        return v._clone()._subtract(this)._normalize();
82
+    }
83
+
84
+    /**
75 85
      * Check if this and v have the same coordinates
76 86
      * @param v a vector
77 87
      * @returns true if this and v have the same coordinates
@@ -94,6 +104,16 @@ export class Vector2
94 104
     }
95 105
 
96 106
     /**
107
+     * Clone this vector
108
+     * @returns a clone of this vector
109
+     * @internal
110
+     */
111
+    _clone(): Vector2
112
+    {
113
+        return new Vector2(this.x, this.y);
114
+    }
115
+
116
+    /**
97 117
      * Set the coordinates of this vector
98 118
      * @param x x-coordinate
99 119
      * @param y y-coordinate
@@ -123,16 +143,6 @@ export class Vector2
123 143
     }
124 144
 
125 145
     /**
126
-     * Clone this vector
127
-     * @returns a clone of this vector
128
-     * @internal
129
-     */
130
-    _clone(): Vector2
131
-    {
132
-        return new Vector2(this.x, this.y);
133
-    }
134
-
135
-    /**
136 146
      * Normalize this vector
137 147
      * @returns this vector, normalized
138 148
      * @internal

+ 20
- 10
src/geometry/vector3.ts Voir le fichier

@@ -76,6 +76,16 @@ export class Vector3
76 76
     }
77 77
 
78 78
     /**
79
+     * Compute the direction from this to v
80
+     * @param v a vector
81
+     * @returns a new unit vector pointing to v from this
82
+     */
83
+    directionTo(v: Vector3): Vector3
84
+    {
85
+        return v._clone()._subtract(this)._normalize();
86
+    }
87
+
88
+    /**
79 89
      * Check if this and v have the same coordinates
80 90
      * @param v a vector
81 91
      * @returns true if this and v have the same coordinates
@@ -99,6 +109,16 @@ export class Vector3
99 109
     }
100 110
 
101 111
     /**
112
+     * Clone this vector
113
+     * @returns a clone of this vector
114
+     * @internal
115
+     */
116
+    _clone(): Vector3
117
+    {
118
+        return new Vector3(this.x, this.y, this.z);
119
+    }
120
+
121
+    /**
102 122
      * Set the coordinates of this vector
103 123
      * @param x x-coordinate
104 124
      * @param y y-coordinate
@@ -131,16 +151,6 @@ export class Vector3
131 151
     }
132 152
 
133 153
     /**
134
-     * Clone this vector
135
-     * @returns a clone of this vector
136
-     * @internal
137
-     */
138
-    _clone(): Vector3
139
-    {
140
-        return new Vector3(this.x, this.y, this.z);
141
-    }
142
-
143
-    /**
144 154
      * Normalize this vector
145 155
      * @returns this vector, normalized
146 156
      * @internal

Chargement…
Annuler
Enregistrer