Browse Source

Add directionTo() to Vector2 and Vector3

customisations
alemart 10 months ago
parent
commit
f7b06c1d1b
4 changed files with 68 additions and 20 deletions
  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 View File

30
 
30
 
31
 The length of the vector.
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
 ### equals
47
 ### equals
34
 
48
 
35
 `vector.equals(v: Vector2): boolean`
49
 `vector.equals(v: Vector2): boolean`

+ 14
- 0
docs/api/vector3.md View File

36
 
36
 
37
 The length of the vector.
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
 ### equals
53
 ### equals
40
 
54
 
41
 `vector.equals(v: Vector3): boolean`
55
 `vector.equals(v: Vector3): boolean`

+ 20
- 10
src/geometry/vector2.ts View File

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
      * Check if this and v have the same coordinates
85
      * Check if this and v have the same coordinates
76
      * @param v a vector
86
      * @param v a vector
77
      * @returns true if this and v have the same coordinates
87
      * @returns true if this and v have the same coordinates
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
      * Set the coordinates of this vector
117
      * Set the coordinates of this vector
98
      * @param x x-coordinate
118
      * @param x x-coordinate
99
      * @param y y-coordinate
119
      * @param y y-coordinate
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
      * Normalize this vector
146
      * Normalize this vector
137
      * @returns this vector, normalized
147
      * @returns this vector, normalized
138
      * @internal
148
      * @internal

+ 20
- 10
src/geometry/vector3.ts View File

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
      * Check if this and v have the same coordinates
89
      * Check if this and v have the same coordinates
80
      * @param v a vector
90
      * @param v a vector
81
      * @returns true if this and v have the same coordinates
91
      * @returns true if this and v have the same coordinates
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
      * Set the coordinates of this vector
122
      * Set the coordinates of this vector
103
      * @param x x-coordinate
123
      * @param x x-coordinate
104
      * @param y y-coordinate
124
      * @param y y-coordinate
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
      * Normalize this vector
154
      * Normalize this vector
145
      * @returns this vector, normalized
155
      * @returns this vector, normalized
146
      * @internal
156
      * @internal

Loading…
Cancel
Save