-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(grid-iterators): update clipped shape iters
- better pre-clipping for circle/line - intern intersection/clipping helpers
- Loading branch information
1 parent
1b80de4
commit dc065ed
Showing
6 changed files
with
121 additions
and
33 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
This file was deleted.
Oops, something went wrong.
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,101 @@ | ||
import type { FnN3, FnU2, FnU7, FnU8, Tuple } from "@thi.ng/api"; | ||
|
||
/** | ||
* Filters points from `src` iterable to remove any falling outside the rect | ||
* defined by `left,top`..`right,bottom`. | ||
* | ||
* @param src | ||
* @param left | ||
* @param top | ||
* @param right | ||
* @param bottom | ||
*/ | ||
export function* clipped( | ||
src: Iterable<number[]>, | ||
left: number, | ||
top: number, | ||
right: number, | ||
bottom: number | ||
) { | ||
for (let p of src) { | ||
if (p[0] >= left && p[0] < right && p[1] >= top && p[1] < bottom) | ||
yield p; | ||
} | ||
} | ||
|
||
/** @internal */ | ||
const axis: FnN3 = (a, b, c) => | ||
(a < b ? a - b : a > b + c ? a - b - c : 0) ** 2; | ||
|
||
/** | ||
* Based on {@link @thi.ng/geom-isec}, but inlined to avoid dependency. | ||
* | ||
* @param x | ||
* @param y | ||
* @param w | ||
* @param h | ||
* @param cx | ||
* @param cy | ||
* @param r | ||
* | ||
* @internal | ||
*/ | ||
export const intersectRectCircle: FnU7<number, boolean> = ( | ||
x, | ||
y, | ||
w, | ||
h, | ||
cx, | ||
cy, | ||
r | ||
) => axis(cx, x, w) + axis(cy, y, h) <= r * r; | ||
|
||
/** | ||
* Based on {@link @thi.ng/geom-clip-line#liangBarsky2Raw}, but with diff return type. | ||
* | ||
* @param ax | ||
* @param ay | ||
* @param bx | ||
* @param by | ||
* @param minx | ||
* @param miny | ||
* @param maxx | ||
* @param maxy | ||
* | ||
* @internal | ||
*/ | ||
export const liangBarsky: FnU8<number, Tuple<number, 4> | undefined> = ( | ||
ax, | ||
ay, | ||
bx, | ||
by, | ||
minx, | ||
miny, | ||
maxx, | ||
maxy | ||
) => { | ||
const dx = bx - ax; | ||
const dy = by - ay; | ||
let alpha = 0; | ||
let beta = 1; | ||
const clip: FnU2<number, boolean> = (p, q) => { | ||
if (p < 0) { | ||
const r = q / p; | ||
if (r > beta) return false; | ||
r > alpha && (alpha = r); | ||
} else if (p > 0) { | ||
const r = q / p; | ||
if (r < alpha) return false; | ||
r < beta && (beta = r); | ||
} else if (q < 0) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
return clip(-dx, ax - minx) && | ||
clip(dx, maxx - ax) && | ||
clip(-dy, ay - miny) && | ||
clip(dy, maxy - ay) | ||
? [alpha * dx + ax, alpha * dy + ay, beta * dx + ax, beta * dy + ay] | ||
: undefined; | ||
}; |
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