Last active
January 15, 2023 16:58
-
-
Save TheMatt2/5a25a613645e98523c4dba5abcc5ed10 to your computer and use it in GitHub Desktop.
This file contains 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
# https://en.wikipedia.org/wiki/Reuleaux_triangle | |
import math | |
import cairo | |
def pos_on_circle(cx, cy, radius, angle): | |
# Convert polar to cartesian, Author: Matthew Schweiss | |
x = math.cos(angle) * radius + cx | |
y = math.sin(angle) * radius + cy | |
return x, y | |
filename = "reuleaux_triangle.svg" | |
center_radius = 100 | |
arc_radius = center_radius * math.sin(math.pi / 3) / math.sin(math.pi / 6) | |
with cairo.SVGSurface(filename, arc_radius, center_radius * 2) as surface: | |
# Draw a Reuleaux triangle | |
ctx = cairo.Context(surface) | |
cx, cy = arc_radius / 2, center_radius | |
x1, y1 = pos_on_circle(cx, cy, center_radius, -math.pi / 2) | |
x2, y2 = pos_on_circle(cx, cy, center_radius, math.pi / 6) | |
x3, y3 = pos_on_circle(cx, cy, center_radius, 5 * math.pi / 6) | |
ctx.arc(x1, y1, arc_radius, math.pi / 3, 2 * math.pi / 3) | |
ctx.arc(x2, y2, arc_radius, math.pi, 4 * math.pi / 3) | |
ctx.arc(x3, y3, arc_radius, 5 * math.pi / 3, 0) | |
ctx.fill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment