Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active July 22, 2024 09:55
Show Gist options
  • Save Shilo/b2583d09475a7d0e38dd6eef89c066d0 to your computer and use it in GitHub Desktop.
Save Shilo/b2583d09475a7d0e38dd6eef89c066d0 to your computer and use it in GitHub Desktop.
Polygon circle example. (Godot 4)
@tool
extends Node2D
@export var radius = 50.0:
set(value):
radius = value
queue_redraw()
@export var start_angle_deg = 0.0:
set(value):
start_angle_deg = value
queue_redraw()
@export var end_angle_deg = 180:
set(value):
end_angle_deg = value
queue_redraw()
var start_angle: float:
get:
return deg_to_rad(start_angle_deg)
var end_angle: float:
get:
return deg_to_rad(end_angle_deg)
@export_range(3, 1000) var circle_segments: int = 64:
set(value):
circle_segments = value
queue_redraw()
var angle: float:
get:
return end_angle - start_angle
var segments: int:
get:
return max(ceili(circle_segments * (angle / TAU)), 1)
func _draw():
var angle_step = (end_angle - start_angle) / segments
var points = []
points.resize(segments + 2)
points[0] = Vector2.ZERO
for i in segments + 1:
var angle = start_angle + i * angle_step
points[i + 1] = Vector2(cos(angle) * radius, sin(angle) * radius)
draw_colored_polygon(points, Color.WHITE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment