1- import { Vector } from "@/core/math/geometry/Vector " ;
1+ import { Vector2D } from "@/core/math/geometry/Vector2D " ;
22import { Rectangle } from "@/core/math/geometry/Rectangle" ;
33import { Line } from "@/core/math/geometry/Line" ;
44
@@ -8,12 +8,12 @@ import { Line } from "@/core/math/geometry/Line";
88 * animations, and UI transitions in games.
99 */
1010export class BezierCurve {
11- private readonly controlPoints : Vector [ ] ;
11+ private readonly controlPoints : Vector2D [ ] ;
1212 private readonly degree : number ;
1313 private cachedLength ?: number ;
14- private cachedLookupTable ?: Vector [ ] ;
14+ private cachedLookupTable ?: Vector2D [ ] ;
1515
16- constructor ( controlPoints : Vector [ ] ) {
16+ constructor ( controlPoints : Vector2D [ ] ) {
1717 if ( controlPoints . length < 2 ) {
1818 throw new Error ( "BezierCurve requires at least 2 control points" ) ;
1919 }
@@ -28,21 +28,21 @@ export class BezierCurve {
2828 /**
2929 * Creates a quadratic Bezier curve (3 control points)
3030 */
31- public static quadratic ( start : Vector , control : Vector , end : Vector ) : BezierCurve {
31+ public static quadratic ( start : Vector2D , control : Vector2D , end : Vector2D ) : BezierCurve {
3232 return new BezierCurve ( [ start , control , end ] ) ;
3333 }
3434
3535 /**
3636 * Creates a cubic Bezier curve (4 control points)
3737 */
38- public static cubic ( start : Vector , control1 : Vector , control2 : Vector , end : Vector ) : BezierCurve {
38+ public static cubic ( start : Vector2D , control1 : Vector2D , control2 : Vector2D , end : Vector2D ) : BezierCurve {
3939 return new BezierCurve ( [ start , control1 , control2 , end ] ) ;
4040 }
4141
4242 /**
4343 * Creates a linear Bezier curve (straight line between 2 points)
4444 */
45- public static linear ( start : Vector , end : Vector ) : BezierCurve {
45+ public static linear ( start : Vector2D , end : Vector2D ) : BezierCurve {
4646 return new BezierCurve ( [ start , end ] ) ;
4747 }
4848
@@ -51,7 +51,7 @@ export class BezierCurve {
5151 * @param t - Parameter value between 0 and 1
5252 * @returns Point on the curve at t
5353 */
54- public getPoint ( t : number ) : Vector {
54+ public getPoint ( t : number ) : Vector2D {
5555 t = Math . max ( 0 , Math . min ( 1 , t ) ) ; // Clamp to [0, 1]
5656
5757 if ( this . degree === 1 ) {
@@ -69,7 +69,7 @@ export class BezierCurve {
6969 /**
7070 * Gets the tangent (direction) vector at parameter t
7171 */
72- public getTangent ( t : number ) : Vector {
72+ public getTangent ( t : number ) : Vector2D {
7373 t = Math . max ( 0 , Math . min ( 1 , t ) ) ;
7474 const epsilon = 0.0001 ;
7575
@@ -82,15 +82,15 @@ export class BezierCurve {
8282 /**
8383 * Gets the normal (perpendicular) vector at parameter t
8484 */
85- public getNormal ( t : number ) : Vector {
85+ public getNormal ( t : number ) : Vector2D {
8686 const tangent = this . getTangent ( t ) ;
87- return new Vector ( - tangent . y , tangent . x ) ; // 2D perpendicular
87+ return new Vector2D ( - tangent . y , tangent . x ) ; // 2D perpendicular
8888 }
8989
9090 /**
9191 * Gets the derivative (velocity) at parameter t
9292 */
93- public getDerivative ( t : number ) : Vector {
93+ public getDerivative ( t : number ) : Vector2D {
9494 t = Math . max ( 0 , Math . min ( 1 , t ) ) ;
9595
9696 if ( this . degree === 1 ) {
@@ -149,7 +149,7 @@ export class BezierCurve {
149149 * @param distance - Distance from start of curve
150150 * @returns Point at that distance, or null if distance exceeds curve length
151151 */
152- public getPointAtDistance ( distance : number ) : Vector | null {
152+ public getPointAtDistance ( distance : number ) : Vector2D | null {
153153 const totalLength = this . getLength ( ) ;
154154 if ( distance < 0 || distance > totalLength ) {
155155 return null ;
@@ -173,14 +173,14 @@ export class BezierCurve {
173173 } ;
174174 }
175175
176- const leftPoints : Vector [ ] = [ ] ;
177- const rightPoints : Vector [ ] = [ ] ;
176+ const leftPoints : Vector2D [ ] = [ ] ;
177+ const rightPoints : Vector2D [ ] = [ ] ;
178178
179179 let points = [ ...this . controlPoints ] ;
180180 leftPoints . push ( points [ 0 ] ) ;
181181
182182 for ( let i = 0 ; i < this . degree ; i ++ ) {
183- const newPoints : Vector [ ] = [ ] ;
183+ const newPoints : Vector2D [ ] = [ ] ;
184184 for ( let j = 0 ; j < points . length - 1 ; j ++ ) {
185185 newPoints . push ( points [ j ] . interpolate ( points [ j + 1 ] , t ) ) ;
186186 }
@@ -192,7 +192,7 @@ export class BezierCurve {
192192 rightPoints . unshift ( points [ points . length - 1 ] ) ;
193193
194194 for ( let i = 0 ; i < this . degree ; i ++ ) {
195- const newPoints : Vector [ ] = [ ] ;
195+ const newPoints : Vector2D [ ] = [ ] ;
196196 for ( let j = 0 ; j < points . length - 1 ; j ++ ) {
197197 newPoints . push ( points [ j ] . interpolate ( points [ j + 1 ] , t ) ) ;
198198 }
@@ -209,8 +209,8 @@ export class BezierCurve {
209209 /**
210210 * Gets evenly spaced points along the curve
211211 */
212- public getPoints ( count : number ) : Vector [ ] {
213- const points : Vector [ ] = [ ] ;
212+ public getPoints ( count : number ) : Vector2D [ ] {
213+ const points : Vector2D [ ] = [ ] ;
214214 for ( let i = 0 ; i <= count ; i ++ ) {
215215 const t = i / count ;
216216 points . push ( this . getPoint ( t ) ) ;
@@ -245,7 +245,7 @@ export class BezierCurve {
245245 /**
246246 * Finds the closest point on the curve to a given point
247247 */
248- public getClosestPoint ( point : Vector , samples : number = 100 ) : { point : Vector ; t : number ; distance : number } {
248+ public getClosestPoint ( point : Vector2D , samples : number = 100 ) : { point : Vector2D ; t : number ; distance : number } {
249249 let closestPoint = this . getPoint ( 0 ) ;
250250 let closestT = 0 ;
251251 let closestDistance = point . distanceBetween ( closestPoint ) ;
@@ -268,29 +268,29 @@ export class BezierCurve {
268268 /**
269269 * Checks if a point is near the curve within a tolerance
270270 */
271- public containsPoint ( point : Vector , tolerance : number = 1 ) : boolean {
271+ public containsPoint ( point : Vector2D , tolerance : number = 1 ) : boolean {
272272 const closest = this . getClosestPoint ( point ) ;
273273 return closest . distance <= tolerance ;
274274 }
275275
276276 /**
277277 * Gets all control points
278278 */
279- public getControlPoints ( ) : Vector [ ] {
279+ public getControlPoints ( ) : Vector2D [ ] {
280280 return [ ...this . controlPoints ] ;
281281 }
282282
283283 /**
284284 * Gets the start point of the curve
285285 */
286- public getStart ( ) : Vector {
286+ public getStart ( ) : Vector2D {
287287 return this . controlPoints [ 0 ] ;
288288 }
289289
290290 /**
291291 * Gets the end point of the curve
292292 */
293- public getEnd ( ) : Vector {
293+ public getEnd ( ) : Vector2D {
294294 return this . controlPoints [ this . controlPoints . length - 1 ] ;
295295 }
296296
@@ -320,12 +320,12 @@ export class BezierCurve {
320320
321321 // Private helper methods
322322
323- private evaluateLinear ( t : number ) : Vector {
323+ private evaluateLinear ( t : number ) : Vector2D {
324324 const [ p0 , p1 ] = this . controlPoints ;
325325 return p0 . interpolate ( p1 , t ) ;
326326 }
327327
328- private evaluateQuadratic ( t : number ) : Vector {
328+ private evaluateQuadratic ( t : number ) : Vector2D {
329329 const [ p0 , p1 , p2 ] = this . controlPoints ;
330330 const oneMinusT = 1 - t ;
331331 const oneMinusTSquared = oneMinusT * oneMinusT ;
@@ -335,10 +335,10 @@ export class BezierCurve {
335335 const y = oneMinusTSquared * p0 . y + 2 * oneMinusT * t * p1 . y + tSquared * p2 . y ;
336336 const z = oneMinusTSquared * p0 . z + 2 * oneMinusT * t * p1 . z + tSquared * p2 . z ;
337337
338- return new Vector ( x , y , z ) ;
338+ return new Vector2D ( x , y , z ) ;
339339 }
340340
341- private evaluateCubic ( t : number ) : Vector {
341+ private evaluateCubic ( t : number ) : Vector2D {
342342 const [ p0 , p1 , p2 , p3 ] = this . controlPoints ;
343343 const oneMinusT = 1 - t ;
344344 const oneMinusTCubed = oneMinusT * oneMinusT * oneMinusT ;
@@ -350,15 +350,15 @@ export class BezierCurve {
350350 const y = oneMinusTCubed * p0 . y + 3 * oneMinusTSquared * t * p1 . y + 3 * oneMinusT * tSquared * p2 . y + tCubed * p3 . y ;
351351 const z = oneMinusTCubed * p0 . z + 3 * oneMinusTSquared * t * p1 . z + 3 * oneMinusT * tSquared * p2 . z + tCubed * p3 . z ;
352352
353- return new Vector ( x , y , z ) ;
353+ return new Vector2D ( x , y , z ) ;
354354 }
355355
356- private deCasteljau ( points : Vector [ ] , t : number ) : Vector {
356+ private deCasteljau ( points : Vector2D [ ] , t : number ) : Vector2D {
357357 if ( points . length === 1 ) {
358358 return points [ 0 ] ;
359359 }
360360
361- const newPoints : Vector [ ] = [ ] ;
361+ const newPoints : Vector2D [ ] = [ ] ;
362362 for ( let i = 0 ; i < points . length - 1 ; i ++ ) {
363363 newPoints . push ( points [ i ] . interpolate ( points [ i + 1 ] , t ) ) ;
364364 }
0 commit comments