import math
def calculate_corner_expansion(v1, v2, v3, radius):
"""
Expand polygon corners to prepare for rounded corner generation.
Args:
v1, v2, v3: tuples representing the (x, y) coordinates of the polygon vertices.
radius: float, the desired radius for the rounded corners.
Returns:
A tuple of new points (v2_new, v3_new) after corner expansion.
"""
def distance(p1, p2):
return math.hypot(p2[0] - p1[0], p2[1] - p1[1])
def angle_between(p1, p2, p3):
a = distance(p2, p3)
b = distance(p1, p3)
c = distance(p1, p2)
return math.acos((a**2 + b**2 - c**2) / (2 * a * b))
def expand_outward(p, angle, radius):
dx, dy = p[0] - v1[0], p[1] - v1[1]
length = math.hypot(dx, dy)
unit_dx, unit_dy = dx / length, dy / length
angle = math.radians(angle)
new_dx, new_dy = unit_dx * math.cos(angle) - unit_dy * math.sin(angle), unit_dx * math.sin(angle) + unit_dy * math.cos(angle)
return (v1[0] + new_dx * radius, v1[1] + new_dy * radius)
def expand_inward(p, angle, radius):
dx, dy = p[0] - v1[0], p[1] - v1[1]
length = math.hypot(dx, dy)
unit_dx, unit_dy = dx / length, dy / length
angle = math.radians(angle)
new_dx, new_dy = unit_dx * math.cos(angle) - unit_dy * math.sin(angle), unit_dx * math.sin(angle) + unit_dy * math.cos(angle)
return (v1[0] - new_dx * radius, v1[1] - new_dy * radius)
angle_v1_v2 = angle_between(v1, v2, v3)
angle_v2_v1 = angle_between(v2, v1, v3)
if angle_v1_v2 < math.pi / 2:
# Convex corner
v2_new = expand_outward(v2, angle_v2_v1, radius)
elif angle_v1_v2 > math.pi / 2:
# Concave corner
v2_new = expand_inward(v2, angle_v2_v1, radius)
else:
# Straight corner
# Handle this case as required, e.g., by setting v2_new to v2
v2_new = v2
# Concave corner
if angle_v1_v2 < math.pi / 2:
v3_new = expand_outward(v3, angle_v1_v2, radius)
elif angle_v1_v2 > math.pi / 2:
v3_new = expand_inward(v3, angle_v1_v2, radius)
else:
# Handle this case as required, e.g., by setting v3_new to v3
v3_new = v3
return v2_new, v3_new
# Example usage:
v1 = (0, 0)
v2 = (1, 1)
v3 = (2, 0)
radius = 0.1
v2_new, v3_new = calculate_corner_expansion(v1, v2, v3, radius)
print(f'New points after expansion: {v2_new}, {v3_new}')
This Python code snippet defines a function calculate_corner_expansion which calculates the new points for corner expansion based on the given vertices and radius. It first calculates the angles at each vertex and then decides whether to expand the corner outward or inward. The function expand_outward and expand_inward are used to calculate the new points based on the corner type.