Last active
June 7, 2022 22:58
-
-
Save aarqon/7ead48fe530deddfe64845fdecd63d94 to your computer and use it in GitHub Desktop.
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
extends Spatial | |
export (int) var num_points = 3 | |
export (Vector2) var pane_scale = Vector2.ONE | |
var points = [] | |
var polygons = [] | |
var intersection = null | |
var default_verts = [ | |
Vector2(0,0), | |
Vector2(0,1), | |
Vector2(1,1), | |
Vector2(1,0) | |
] | |
var m_p | |
func _ready(): | |
reset() | |
$Debug.points = points | |
$Debug.polys = polygons | |
m_p = make_polygons() | |
func _process(_delta: float): | |
if Input.is_action_just_pressed("ui_accept"): | |
print(m_p) | |
if m_p == null or m_p.is_valid(): | |
m_p = m_p.resume() | |
else: | |
print("Function already resumed") | |
func make_polygons(): | |
for point in points: | |
$Debug.current_point = point | |
var verts = default_verts.duplicate() | |
polygons.append(verts) | |
var sorted_points = points.duplicate() | |
sorted_points.erase(point) | |
for i in range(len(sorted_points)): | |
sorted_points[i] = sorted_points[i] - point | |
sorted_points.sort_custom(PointSorter, "sort_angular_ascending") | |
for i in range(len(sorted_points)): | |
sorted_points[i] = sorted_points[i] + point | |
for test_point in sorted_points: | |
if point == test_point: | |
continue | |
$Debug.target_point = test_point | |
# Get midpoint and perp. bisector as ray | |
var midpoint = (test_point + point) / 2 | |
var bisect_dir1 = midpoint + Vector2(-(test_point.y - point.y), test_point.x - point.x) * 10 | |
var bisect_dir2 = midpoint - Vector2(-(test_point.y - point.y), test_point.x - point.x) * 10 | |
$Debug.bisector = [bisect_dir1, bisect_dir2] | |
# Check edges for intersections (both directions) | |
for i in range(len(verts)): | |
# Get origin and direction of edge | |
var origin = verts[i] | |
var next = (i + 1) % len(verts) | |
var direction = verts[next] - origin | |
print("Testing edge %s -> %s" % [verts[i], verts[next]]) | |
# First must make sure lower determinant is not 0 (parallel) | |
var lower_det = bisect_dir1.cross(direction) | |
var beta | |
if lower_det == 0: | |
# Try next edge | |
continue | |
else: | |
beta = (origin - midpoint).cross(bisect_dir1) / lower_det | |
# Range is (0,1) | |
if beta > 0 and beta < 1: | |
intersection = lerp(origin, direction, beta) | |
$Debug.intersection = intersection | |
yield() | |
# If the origin is in our half-plane, replace the direction | |
if (midpoint - origin).dot(midpoint - point) > 0: | |
verts.remove(next) | |
verts.insert(next, intersection) | |
# If the direction is in our half-plane, replace the origin | |
elif (midpoint - direction).dot(midpoint - point) > 0: | |
verts.remove(i) | |
verts.insert(i, intersection) | |
yield() | |
func reset(): | |
randomize() | |
points = [] | |
for _i in range(num_points): | |
points.append(Vector2(randf()*pane_scale.x, randf()*pane_scale.y)) | |
polygons = [] | |
class PointSorter: | |
static func sort_angular_ascending(a, b): | |
var a_angle = Vector2.UP.dot(a) | |
var b_angle = Vector2.UP.dot(b) | |
if a_angle < 0: | |
a_angle += 360 # To make range [0-360) | |
if b_angle < 0: | |
b_angle += 360 | |
return a_angle < b_angle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment