Browse Source

Introduce methods distanceTo() and dot() to Vector2 and Vector3

customisations
alemart 10 months ago
parent
commit
9cf3c62ddd
4 changed files with 103 additions and 0 deletions
  1. 28
    0
      docs/api/vector2.md
  2. 28
    0
      docs/api/vector3.md
  3. 23
    0
      src/geometry/vector2.ts
  4. 24
    0
      src/geometry/vector3.ts

+ 28
- 0
docs/api/vector2.md View File

30
 
30
 
31
 The length of the vector.
31
 The length of the vector.
32
 
32
 
33
+### dot
34
+
35
+`vector.dot(v: Vector2): number`
36
+
37
+Compute the dot product of `this` and `v`.
38
+
39
+**Arguments**
40
+
41
+* `v: Vector2`. A vector.
42
+
43
+**Returns**
44
+
45
+The dot product of the vectors.
46
+
47
+### distanceTo
48
+
49
+`vector.distanceTo(v: Vector2): number`
50
+
51
+Compute the distance between points `this` and `v`.
52
+
53
+**Arguments**
54
+
55
+* `v: Vector2`. A vector / point.
56
+
57
+**Returns**
58
+
59
+The distance between the points.
60
+
33
 ### directionTo
61
 ### directionTo
34
 
62
 
35
 `vector.directionTo(v: Vector2): Vector2`
63
 `vector.directionTo(v: Vector2): Vector2`

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

36
 
36
 
37
 The length of the vector.
37
 The length of the vector.
38
 
38
 
39
+### dot
40
+
41
+`vector.dot(v: Vector3): number`
42
+
43
+Compute the dot product of `this` and `v`.
44
+
45
+**Arguments**
46
+
47
+* `v: Vector3`. A vector.
48
+
49
+**Returns**
50
+
51
+The dot product of the vectors.
52
+
53
+### distanceTo
54
+
55
+`vector.distanceTo(v: Vector3): number`
56
+
57
+Compute the distance between points `this` and `v`.
58
+
59
+**Arguments**
60
+
61
+* `v: Vector3`. A vector / point.
62
+
63
+**Returns**
64
+
65
+The distance between the points.
66
+
39
 ### directionTo
67
 ### directionTo
40
 
68
 
41
 `vector.directionTo(v: Vector3): Vector3`
69
 `vector.directionTo(v: Vector3): Vector3`

+ 23
- 0
src/geometry/vector2.ts View File

72
     }
72
     }
73
 
73
 
74
     /**
74
     /**
75
+     * Compute the dot product of this and v
76
+     * @param v a vector
77
+     * @returns the dot product of the vectors
78
+     */
79
+    dot(v: Vector2): number
80
+    {
81
+        return this.x * v.x + this.y * v.y;
82
+    }
83
+
84
+    /**
85
+     * Compute the distance between points this and v
86
+     * @param v a vector / point
87
+     * @returns the distance between the points
88
+     */
89
+    distanceTo(v: Vector2): number
90
+    {
91
+        const dx = this.x - v.x;
92
+        const dy = this.y - v.y;
93
+
94
+        return Math.sqrt(dx*dx + dy*dy);
95
+    }
96
+
97
+    /**
75
      * Compute the direction from this to v
98
      * Compute the direction from this to v
76
      * @param v a vector
99
      * @param v a vector
77
      * @returns a new unit vector pointing to v from this
100
      * @returns a new unit vector pointing to v from this

+ 24
- 0
src/geometry/vector3.ts View File

76
     }
76
     }
77
 
77
 
78
     /**
78
     /**
79
+     * Compute the dot product of this and v
80
+     * @param v a vector
81
+     * @returns the dot product of the vectors
82
+     */
83
+    dot(v: Vector3): number
84
+    {
85
+        return this.x * v.x + this.y * v.y + this.z * v.z;
86
+    }
87
+
88
+    /**
89
+     * Compute the distance between points this and v
90
+     * @param v a vector / point
91
+     * @returns the distance between the points
92
+     */
93
+    distanceTo(v: Vector3): number
94
+    {
95
+        const dx = this.x - v.x;
96
+        const dy = this.y - v.y;
97
+        const dz = this.z - v.z;
98
+
99
+        return Math.sqrt(dx*dx + dy*dy + dz*dz);
100
+    }
101
+
102
+    /**
79
      * Compute the direction from this to v
103
      * Compute the direction from this to v
80
      * @param v a vector
104
      * @param v a vector
81
      * @returns a new unit vector pointing to v from this
105
      * @returns a new unit vector pointing to v from this

Loading…
Cancel
Save