Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joelgraff/ce4593be5a81ce90ed071ca62aac0739 to your computer and use it in GitHub Desktop.
Save joelgraff/ce4593be5a81ce90ed071ca62aac0739 to your computer and use it in GitHub Desktop.
import numpy as np
from shapely.geometry import Polygon
from shapely.affinity import scale
def generate_elliptical_core(a=8, b=8):
return {'type': 'ellipse', 'a': a, 'b': b}
def generate_core_polygon(core_shape, scale_factor=1.0, min_points=87): # Reduced to match
"""Generate a Shapely polygon for the core with at least min_points."""
if core_shape['type'] != 'ellipse':
raise ValueError(f"Unsupported core type: {core_shape['type']}. Only elliptical cores are supported.")
a, b = core_shape['a'] * scale_factor, core_shape['b'] * scale_factor
if a < 1e-6 or b < 1e-6:
raise ValueError("Core ellipse dimensions too small.")
num_points = max(87, min_points)
t = np.linspace(0, 2 * np.pi, num_points, endpoint=False)
points = [(a * np.cos(theta), b * np.sin(theta)) for theta in t]
# Remove duplicates
coords = [points[0]]
for i in range(1, len(points)):
dist = np.sqrt((points[i][0] - coords[-1][0])**2 +
(points[i][1] - coords[-1][1])**2)
if dist > 1e-6:
coords.append(points[i])
poly = Polygon(coords)
if not poly.is_valid:
raise ValueError("Generated core polygon is invalid: ellipse")
return poly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment