mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
chore: Unify math types, utils and functions (#8389)
Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
This commit is contained in:
parent
e3d1dee9d0
commit
f4dd23fc31
98 changed files with 4291 additions and 3661 deletions
52
packages/math/line.ts
Normal file
52
packages/math/line.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { pointCenter, pointRotateRads } from "./point";
|
||||
import type { GlobalPoint, Line, LocalPoint, Radians } from "./types";
|
||||
|
||||
/**
|
||||
* Create a line from two points.
|
||||
*
|
||||
* @param points The two points lying on the line
|
||||
* @returns The line on which the points lie
|
||||
*/
|
||||
export function line<P extends GlobalPoint | LocalPoint>(a: P, b: P): Line<P> {
|
||||
return [a, b] as Line<P>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenient point creation from an array of two points.
|
||||
*
|
||||
* @param param0 The array with the two points to convert to a line
|
||||
* @returns The created line
|
||||
*/
|
||||
export function lineFromPointPair<P extends GlobalPoint | LocalPoint>([a, b]: [
|
||||
P,
|
||||
P,
|
||||
]): Line<P> {
|
||||
return line(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
* @param pointArray
|
||||
* @returns
|
||||
*/
|
||||
export function lineFromPointArray<P extends GlobalPoint | LocalPoint>(
|
||||
pointArray: P[],
|
||||
): Line<P> | undefined {
|
||||
return pointArray.length === 2
|
||||
? line<P>(pointArray[0], pointArray[1])
|
||||
: undefined;
|
||||
}
|
||||
|
||||
// return the coordinates resulting from rotating the given line about an origin by an angle in degrees
|
||||
// note that when the origin is not given, the midpoint of the given line is used as the origin
|
||||
export const lineRotate = <Point extends LocalPoint | GlobalPoint>(
|
||||
l: Line<Point>,
|
||||
angle: Radians,
|
||||
origin?: Point,
|
||||
): Line<Point> => {
|
||||
return line(
|
||||
pointRotateRads(l[0], origin || pointCenter(l[0], l[1]), angle),
|
||||
pointRotateRads(l[1], origin || pointCenter(l[0], l[1]), angle),
|
||||
);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue