Skip to content
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

Implement polygon/polyline simplification and smoothing #76

Merged
merged 2 commits into from
May 7, 2021

Conversation

Xrayez
Copy link
Contributor

@Xrayez Xrayez commented May 6, 2021

Adds RDP method for polyline simplification:

simplification.mp4

This is useful in case you have a complex shape that you need to simplify for the purposes of increased rendering and/or physics performance.

Adds Chaikin's polyline and polygon smoothing:

smoothing.mp4

This can be useful for, say, procedurally generating collision objects from simpler outlines, for instance creating asteroids.

This is approximate by definition, but if someone would also need to implement interpolated version (just smooth_polyline/polygon), this would make sense to add added in #77.

@Xrayez Xrayez added this to the gd3 milestone May 6, 2021
@Xrayez
Copy link
Contributor Author

Xrayez commented May 6, 2021

Snippet in case tests need to be updated in the future.

extends Node2D

export var is_polygon = false
var points = PoolVector2Array()


func _input(event):
	if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_LEFT:
		var pos = get_global_mouse_position()
		pos.x = int(pos.x)
		pos.y = int(pos.y)
		points.push_back(pos)
		update()
	elif event.is_action_pressed("ui_accept"):
		if is_polygon:
			points = GoostGeometry2D.smooth_polygon_approx(points, 1)
		else:
			points = GoostGeometry2D.smooth_polyline_approx(points, 1)
		update()
	elif event.is_action_pressed("ui_cancel"):
		points = GoostGeometry2D.simplify_polyline(points, 100.0)
		update()

func _draw():
	if not is_polygon:
		if points.size() >= 2:
			draw_polyline(points, Color.orange, 2, true)
	else:
		if points.size() >= 3:
			draw_colored_polygon(points, Color.orange)

Adds RDP method for polyline simplification.
Adds Chaikin's polyline and polygon smoothing (approximate by definition).
@Xrayez Xrayez force-pushed the simplify-smooth-poly branch from 5ae0176 to df522ca Compare May 6, 2021 17:55
@Xrayez Xrayez force-pushed the simplify-smooth-poly branch from 38d2651 to 3acc519 Compare May 7, 2021 13:58
@Xrayez Xrayez merged commit 26adfcf into gd3 May 7, 2021
@Xrayez Xrayez deleted the simplify-smooth-poly branch May 7, 2021 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant