[Mar 03] 3h - Gemotry Closest
[Mar 04] 4h - Gemotry Closest
[Mar 15] 6h - Geometry Resize / UI
[Mar 16] 4h - Geometry Overlaps
[Mar 17] 1h - Code Review
[Mar 18] 3h - Fixes & Upload
# closest(l,p)
func _line_closest_point(line_start: Vector2, line_end: Vector2, point: Vector2) -> Vector2:
var difference: Vector2 = line_end - line_start
var point_dot: float = (line_end - line_start).dot(point - line_start)
var length_squared: float = line_start.distance_squared_to(line_end)
var factor: float = clamp(point_dot / length_squared, 0.0, 1.0)
return line_start + (factor * difference)
# contains(r,p)
func _rectangle_contains_point(rect: Rect2, point: Vector2) -> bool:
return !(point.x < rect.position.x || point.y < rect.position.y \
|| point.x > rect.end.x || point.y > rect.end.y)
# contains(t,p)
func _triangle_contains_point(t0: Vector2, t1: Vector2, t2: Vector2, p: Vector2) -> bool:
var a: float = 0.5 * (-t1.y * t2.x + t0.y * (-t1.x + t2.x) + t0.x * (t1.y - t2.y) + t1.x * t2.y)
var sign: float = -1 if a < 0 else 1
var s: float = (t0.y * t2.x - t0.x * t2.y + (t2.y - t0.y) * p.x + (t0.x - t2.x) * p.y) * sign
var t: float = (t0.x * t1.y - t0.y * t1.x + (t0.y - t1.y) * p.x + (t1.x - t0.x) * p.y) * sign
return s > 0 && t >0 && (s + t) < 2 * a * sign
# overlaps(l,l)
func _line_overlaps_line(line_a_start: Vector2, line_a_end: Vector2, line_b_start: Vector2, line_b_end: Vector2) -> bool:
var _ud: float = (line_a_end - line_a_start).cross(line_b_end - line_b_start)
var _ua: float = (line_a_end - line_a_start).cross(line_a_start - line_b_start) / _ud
var _ub: float = (line_b_end - line_b_start).cross(line_a_start - line_b_start) / _ud
return _ua >= 0 && _ua <= 1 && _ub >= 0 && _ub <= 1
# overlaps(c,l)
func _circle_overlaps_line(circle_position: Vector2, circle_radius: float, line_start: Vector2, line_end: Vector2) -> bool:
var closest: Vector2 = _line_closest_point(line_start, line_end, circle_position)
var distance: float = circle_position.distance_squared_to(closest)
return distance < (circle_radius ** 2)
# overlaps(c,r)
func _circle_overlaps_rectangle(rect: Rect2, circle_position: Vector2, circle_radius: float) -> bool:
var nearest: Vector2 = circle_position.clamp(rect.position, rect.end)
var distance: float = circle_position.distance_squared_to(nearest)
var overlap: float = (circle_radius ** 2) - distance
return overlap > 0
# overlaps(r,l)
func _rectangle_overlaps_line(line_start: Vector2, line_end: Vector2) -> bool:
return _rectangle_contains_point(_rect, line_start) \
|| _line_overlaps_line(_rect_top_start, _rect_top_end, line_start, line_end) \
|| _line_overlaps_line(_rect_bottom_start, _rect_bottom_end, line_start, line_end) \
|| _line_overlaps_line(_rect_left_start, _rect_left_end, line_start, line_end) \
|| _line_overlaps_line(_rect_right_start, _rect_right_end, line_start, line_end)
# overlaps(t,l)
func _triangle_overlaps_line(t0: Vector2, t1: Vector2, t2: Vector2, line_start: Vector2, line_end: Vector2) -> bool:
return _triangle_contains_point(t0, t1, t2, line_start) \
|| _line_overlaps_line(t0, t1, line_start, line_end) \
|| _line_overlaps_line(t1, t2, line_start, line_end) \
|| _line_overlaps_line(t2, t0, line_start, line_end)
# overlaps(t,c)
func _triangle_overlaps_circle(t0: Vector2, t1: Vector2, t2: Vector2, circle_position: Vector2, circle_radius: float) -> bool:
var nearest = _triangle_closest_point(t0, t1, t2, circle_position)
return _triangle_contains_point(t0, t1, t2, circle_position) \
|| nearest.distance_squared_to(circle_position) <= circle_radius ** 2
# overlaps(t,r)
func _triangle_overlaps_rectangle(rect: Rect2, t0: Vector2, t1: Vector2, t2: Vector2) -> bool:
return _rectangle_contains_point(rect, t0) \
|| _triangle_overlaps_line(t0, t1, t2, _rect_top_start, _rect_top_end) \
|| _triangle_overlaps_line(t0, t1, t2, _rect_bottom_start, _rect_bottom_end) \
|| _triangle_overlaps_line(t0, t1, t2, _rect_left_start, _rect_left_end) \
|| _triangle_overlaps_line(t0, t1, t2, _rect_right_start, _rect_right_end)
Leave a comment
Log in with itch.io to leave a comment.