import math
# voor Deense ellipse n = 2.5
# https://piethein.com/superellipse/
# a: lengte
# b: breedte
# n: factor
# res: aantal punten
def superellipse_points(a, b, n, res):
pts = []
for i in range(res):
t = 2 * math.pi * i / res
x = a * math.copysign(abs(math.cos(t)) ** (2.0 / n), math.cos(t))
y = b * math.copysign(abs(math.sin(t)) ** (2.0 / n), math.sin(t))
pts.append((x, y))
return pts
def save_svg(points, filename, padding=10):
xs = [p[0] for p in points]
ys = [p[1] for p in points]
min_x, max_x = min(xs), max(xs)
min_y, max_y = min(ys), max(ys)
width = max_x - min_x + 2 * padding
height = max_y - min_y + 2 * padding
# Translate points so they fit in the viewport
def tx(x): return x - min_x + padding
def ty(y): return y - min_y + padding
coords = " ".join(f"{tx(x):.6f},{ty(y):.6f}" for x, y in points)
svg = (
f'<svg xmlns="http://www.w3.org/2000/svg" '
f'width="{width:.4f}" height="{height:.4f}" '
f'viewBox="0 0 {width:.4f} {height:.4f}">\n'
f' <polygon points="{coords}" '
f'fill="none" stroke="black" stroke-width="1"/>\n'
f'</svg>\n'
)
with open(filename, "w") as f:
f.write(svg)
print(f"SVG saved to {filename}")
if __name__ == "__main__":
a = float(input("a: "))
b = float(input("b: "))
n = float(input("n: "))
res = int(input("res: "))
points = superellipse_points(a, b, n, res)
for x, y in points:
print(f"{x:.10f}, {y:.10f}")
save_svg(points, "superellipse.svg")
input('Press ENTER to exit')