-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Open
dragoncoder047
wants to merge
8
commits into
kaplayjs:master
Choose a base branch
from
dragoncoder047:fix-triangulate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: fix triangulate #1103
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1682c6b
fix: fix triangulate
dragoncoder047 3d7f05b
Merge branch 'master' into fix-triangulate
dragoncoder047 d563dc9
add doc for changed triangulate method
dragoncoder047 6e63bba
var
dragoncoder047 17c473c
var
dragoncoder047 b5324a1
breaking change
dragoncoder047 7f9ecc9
remove iterator
dragoncoder047 8bb4237
Merge branch 'master' into fix-triangulate
dragoncoder047 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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 }, | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you explain why it doesnt return Vec2 anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see drawPolygon.ts