-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Spherical and Polar to math.polar.ts
- Loading branch information
Showing
2 changed files
with
118 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import type { DeepImmutable, Nullable, float } from "../types"; | ||
import {Vector2, Vector3} from "./math.vector"; | ||
|
||
/** | ||
* Class used to store (r, theta) vector representation | ||
*/ | ||
export class Polar { | ||
public radius: number; | ||
public theta: number; | ||
|
||
/** | ||
* Creates a new Polar object | ||
* @param radius the radius of the vector | ||
* @param theta the angle of the vector | ||
*/ | ||
constructor(radius: number, theta: number){ | ||
this.radius = radius; | ||
this.theta = theta; | ||
} | ||
|
||
/** | ||
* Gets the rectangular coordinates of the current Polar | ||
* @param the reference to assign the result | ||
* @returns the updated reference | ||
*/ | ||
public toVector2ToRef(ref: Vector2): Vector2{ | ||
Vector2.FromPolarToRef(this, ref); | ||
return ref; | ||
} | ||
|
||
/** | ||
* Gets the rectangular coordinates of the current Polar | ||
* @returns the rectangular coordinates | ||
*/ | ||
public toVector2(): Vector2{ | ||
return Vector2.FromPolar(this); | ||
} | ||
|
||
/** | ||
* Converts a given Vector2 to its polar coordinates | ||
* @param v the Vector2 to convert | ||
* @param ref the reference to assign the result | ||
* @returns the updated reference | ||
*/ | ||
public static FromVector2ToRef(v: Vector2, ref: Polar): Polar{ | ||
const theta = Math.sign(v.y) * Math.acos(v.x / v.length()); | ||
ref.radius = v.length(); | ||
ref.theta = theta; | ||
return ref | ||
} | ||
|
||
/** | ||
* Converts a given Vector2 to its polar coordinates | ||
* @param v the Vector2 to convert | ||
* @returns a Polar | ||
*/ | ||
public static FromVector2(v: Vector2): Polar{ | ||
let polar = new Polar(); | ||
Polar.FromVector2ToRef(v, polar); | ||
return polar; | ||
} | ||
} | ||
|
||
export class Spherical { | ||
public length: number; | ||
public theta: number; | ||
public phi: number; | ||
|
||
constructor(length, theta, phi){ | ||
this.length = length; | ||
this.theta = theta; | ||
this.phi = phi; | ||
} | ||
|
||
/** | ||
* Assigns the rectangular coordinates of the current Spherical to a Vector3 | ||
* @param ref the Vector3 to update | ||
* @returns the updated Vector3 | ||
*/ | ||
public toVector3ToRef(ref: DeepImmutable<Vector3>): Vector3{ | ||
return Vector3.FromSphericalToRef(ref); | ||
} | ||
|
||
/** | ||
* Gets a Vector3 from the current spherical coordinates | ||
* @returns the Vector3 | ||
*/ | ||
public toVector3(): Vector3{ | ||
return Vector3.FromSpherical(this); | ||
} | ||
|
||
/** | ||
* Assigns the spherical coordinates from a Vector3 | ||
* @param vector the vector to convert | ||
* @param ref the Spherical to update | ||
* @returns the updated ref | ||
*/ | ||
public static FromVector3ToRef(vector: DeepImmutable<Vector3>, ref: Spherical): Spherical{ | ||
let rotation = Vector3.PitchYawRollForDirectionChange(Vector3.Zero(), vector); | ||
ref.radius = vector.length(); | ||
ref.theta = rotation.x; | ||
ref.phi = rotation.y; | ||
return ref; | ||
} | ||
|
||
/** | ||
* Gets a Spherical from a Vector3 | ||
* @param vector defines the vector in (x, y, z) coordinate space | ||
* @returns a new Spherical | ||
*/ | ||
public static FromVector3(vector: DeepImmutable<Vector3>): Spherical{ | ||
let spherical = new Spherical(); | ||
Spherical.FromVector3ToRef(vector, spherical); | ||
return spherical; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters