-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(geom): add interfaces, split out common.ts into /func folder
- Loading branch information
1 parent
34312d8
commit 9fb2091
Showing
12 changed files
with
202 additions
and
58 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
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,61 @@ | ||
import { ICopy } from "@thi.ng/api/api"; | ||
import { IDistance, IMix } from "@thi.ng/vectors/api"; | ||
|
||
export type SampleableVector<T> = ICopy<T> & IDistance<T> & IMix<T>; | ||
|
||
export interface IArea { | ||
/** | ||
* Computes and returns area. For types where orientation is taken | ||
* into account, setting `signed` to false will always return the | ||
* absolute area (default is `true`). | ||
* | ||
* @param unsigned | ||
*/ | ||
area(signed?: boolean): number; | ||
} | ||
|
||
export interface IArcLength { | ||
/** | ||
* Returns arc length, or for closed shapes, circumference. | ||
*/ | ||
arcLength(): number; | ||
} | ||
|
||
export interface IBounds<T> { | ||
bounds(): T; | ||
/** | ||
* Dimension along x-axis. | ||
*/ | ||
width(): number; | ||
/** | ||
* Dimension along y-axis. | ||
*/ | ||
height(): number; | ||
/** | ||
* Dimension along z-axis. | ||
*/ | ||
depth(): number; | ||
} | ||
|
||
export interface ICentroid<T> { | ||
/** | ||
* Computes & returns centroid. If `c` is given it MUST be a zero | ||
* vector and will be used to store result. | ||
* | ||
* @param c | ||
*/ | ||
centroid(c?: T): T; | ||
} | ||
|
||
export interface IEdges<T> { | ||
edges(): Iterable<T>; | ||
} | ||
|
||
export interface IToPolygon2 { | ||
// FIXME return type should be interface | ||
toPolygon2(opts?: any): any; | ||
} | ||
|
||
export interface IVertices<T> { | ||
vertices(opts?: any): Iterable<T>; | ||
} |
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
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
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
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,14 @@ | ||
import { IDistance } from "@thi.ng/vectors/api"; | ||
|
||
export const arcLength = <T extends IDistance<T>>(pts: Readonly<T>[], closed = false) => { | ||
const num = pts.length; | ||
if (num < 2) return 0; | ||
let i = pts[0]; | ||
let j = pts[1]; | ||
let res = 0; | ||
for (let k = 1; k < num; k++ , i = j, j = pts[k]) { | ||
res += i.dist(j); | ||
} | ||
closed && (res += i.dist(pts[0])); | ||
return res; | ||
}; |
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,10 @@ | ||
import { IMinMax } from "@thi.ng/vectors/api"; | ||
|
||
export const bounds = <T extends IMinMax<T>>(pts: T[], vmin: T, vmax: T) => { | ||
for (let i = pts.length; --i >= 0;) { | ||
const p = pts[i]; | ||
vmin.min(p); | ||
vmax.max(p); | ||
} | ||
return [vmin, vmax]; | ||
}; |
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,41 @@ | ||
import { Vec2 } from "@thi.ng/vectors/vec2"; | ||
|
||
export const circumCenter = (a: Readonly<Vec2>, b: Readonly<Vec2>, c: Readonly<Vec2>) => { | ||
let m1, m2, mx1, mx2, my1, my2, xc, yc; | ||
const deltaAB = Math.abs(a.y - b.y), | ||
deltaBC = Math.abs(b.y - c.y); | ||
|
||
if (deltaAB < 1e-6 && deltaBC < 1e-6) { | ||
return null; | ||
} | ||
const ax = a.x, ay = a.y, | ||
bx = b.x, by = b.y, | ||
cx = c.x, cy = c.y; | ||
if (deltaAB < 1e-6) { | ||
m2 = - (cx - bx) / (cy - by); | ||
mx2 = (bx + cx) / 2; | ||
my2 = (by + cy) / 2; | ||
xc = (bx + ax) / 2; | ||
yc = m2 * (xc - mx2) + my2; | ||
} else if (deltaBC < 1e-6) { | ||
m1 = - (bx - ax) / (by - ay); | ||
mx1 = (ax + bx) / 2; | ||
my1 = (ay + by) / 2; | ||
xc = (cx + bx) / 2; | ||
yc = m1 * (xc - mx1) + my1; | ||
} else { | ||
m1 = - (bx - ax) / (by - ay); | ||
m2 = - (cx - bx) / (cy - by); | ||
mx1 = (ax + bx) / 2; | ||
my1 = (ay + by) / 2; | ||
mx2 = (bx + cx) / 2; | ||
my2 = (by + cy) / 2; | ||
xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2); | ||
if (deltaAB > deltaBC) { | ||
yc = m1 * (xc - mx1) + my1; | ||
} else { | ||
yc = m2 * (xc - mx2) + my2; | ||
} | ||
} | ||
return new Vec2(xc, yc); | ||
} |
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,6 @@ | ||
import { wrap } from "@thi.ng/transducers/iter/wrap"; | ||
import { partition } from "@thi.ng/transducers/xform/partition"; | ||
|
||
export const edges = <T>(vertices: T[], closed = false) => { | ||
return partition(2, 1, closed ? wrap(vertices, 1, false, true) : vertices); | ||
}; |
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
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
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