-
-
Notifications
You must be signed in to change notification settings - Fork 112
fix: fix triangulate #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix: fix triangulate #1103
Changes from 3 commits
1682c6b
3d7f05b
d563dc9
6e63bba
17c473c
b5324a1
7f9ecc9
8bb4237
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6379,8 +6379,9 @@ export interface KAPLAYCtx { | |
| * @since v3001.0 | ||
| * @group Math | ||
| * @subgroup Advanced | ||
| * @returns flattened list of indices of each of the decomposed triangles, list will always have a multiple of 3 length | ||
| */ | ||
| triangulate(pts: Vec2[]): Vec2[][]; | ||
| triangulate(pts: Vec2[]): number[]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would you explain why it doesnt return Vec2 anymore?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the only place that triangulate was used really inside kaplay at least is to compute the indices for the polygon triangulation and making triangulate() return the format that the polygon needs results in less conversion overhead and less wasted memory
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see drawPolygon.ts |
||
| /** | ||
| * @returns the convex hull of the given polygon. | ||
| * @since v4000.0 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| kaplay({ | ||
| background: [0, 0, 0], | ||
| }); | ||
|
|
||
| add([ | ||
| text("Drag corners of the polygon"), | ||
| pos(20, 20), | ||
| ]); | ||
|
|
||
| // Make a weird shape | ||
| const poly = add([ | ||
| polygon([ | ||
| vec2(0, 0), | ||
| vec2(100, 0), | ||
| vec2(200, 100), | ||
| vec2(100, 200), | ||
| vec2(0, 100), | ||
| vec2(80, 100), | ||
| ], { | ||
| colors: [ | ||
| rgb(128, 255, 128), | ||
| rgb(255, 128, 128), | ||
| rgb(128, 128, 255), | ||
| rgb(255, 128, 128), | ||
| rgb(128, 128, 128), | ||
| rgb(128, 255, 128), | ||
| ], | ||
| triangulate: true, | ||
| }), | ||
| pos(150, 150), | ||
| area(), | ||
| color(), | ||
| ]); | ||
|
|
||
| let dragging = null; | ||
| let hovering = null; | ||
|
|
||
| poly.onDraw(() => { | ||
| const triangleIndices = triangulate(poly.pts); | ||
| for (var i = 0; i < triangleIndices.length; i += 3) { | ||
| drawTriangle({ | ||
| p1: poly.pts[triangleIndices[i]], | ||
| p2: poly.pts[triangleIndices[i + 1]], | ||
| p3: poly.pts[triangleIndices[i + 2]], | ||
| fill: false, | ||
| outline: { color: BLACK }, | ||
| }); | ||
| } | ||
| if (hovering !== null) { | ||
| drawCircle({ | ||
| pos: poly.pts[hovering], | ||
| radius: 16, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| onUpdate(() => { | ||
| if (isConvex(poly.pts)) { | ||
| poly.color = WHITE; | ||
| } | ||
| else { | ||
| poly.color = rgb(192, 192, 192); | ||
| } | ||
| }); | ||
|
|
||
| onMousePress(() => { | ||
| dragging = hovering; | ||
| }); | ||
|
|
||
| onMouseRelease(() => { | ||
| dragging = null; | ||
| }); | ||
|
|
||
| onMouseMove(() => { | ||
| hovering = null; | ||
| const mp = mousePos().sub(poly.pos); | ||
| for (let i = 0; i < poly.pts.length; i++) { | ||
| if (mp.dist(poly.pts[i]) < 16) { | ||
| hovering = i; | ||
| break; | ||
| } | ||
| } | ||
| if (dragging !== null) { | ||
| poly.pts[dragging] = mousePos().sub(poly.pos); | ||
| } | ||
| }); | ||
|
|
||
| poly.onHover(() => { | ||
| poly.color = rgb(200, 200, 255); | ||
| }); | ||
|
|
||
| poly.onHoverEnd(() => { | ||
| poly.color = rgb(255, 255, 255); | ||
| }); | ||
|
|
||
| poly.onDraw(() => { | ||
| drawPolygon({ | ||
| ...poly, | ||
| pos: vec2(), | ||
| fill: false, | ||
| outline: { color: RED, width: 5 }, | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.