Add n-gons (#2)

This commit is contained in:
Lipis
2020-11-07 01:24:34 +02:00
committed by GitHub
parent 5039c5b776
commit 3341601f76
3 changed files with 85 additions and 3 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
.DS_store
__pycache__
*.excalidraw
*.excalidraw*
+49 -2
View File
@@ -55,18 +55,38 @@ STROKE = [
"#d9480f",
]
# source: https://stackoverflow.com/a/46336730/8418
def bounding_box(points):
x_coordinates, y_coordinates = zip(*points)
return [
(min(x_coordinates), min(y_coordinates)),
(max(x_coordinates), max(y_coordinates)),
]
def width_height(box):
return box[1][0] - box[0][0], box[1][1] - box[0][1]
class Excalidraw:
data = {
"version": 1,
"version": 2,
"type": "excalidraw",
"source": "py-excalidraw",
"elements": [],
"appState": {"viewBackgroundColor": CANVAS[0]},
}
lib = {"type": "excalidrawlib", "version": 1, "library": []}
def __new_element(
self, element_type, angle=0, roughness=1, strokeWidth=1, fillStyle="hachure"
self,
element_type,
angle=0,
roughness=1,
strokeWidth=1,
fillStyle="hachure",
strokeSharpness="round",
):
return {
"type": element_type,
@@ -76,6 +96,7 @@ class Excalidraw:
"fillStyle": fillStyle,
"strokeWidth": strokeWidth,
"roughness": roughness,
"strokeSharpness": strokeSharpness,
"angle": angle,
}
@@ -98,11 +119,37 @@ class Excalidraw:
rectangle["backgroundColor"] = backgroundColor
self.data["elements"].append(rectangle)
def add_line(
self,
x,
y,
points,
strokeColor=STROKE[0],
backgroundColor=BACKGROUND[0],
**kargs,
):
line = self.__new_element("line", **kargs)
line["x"] = x
line["y"] = y
line["points"] = points
line["strokeColor"] = strokeColor
line["backgroundColor"] = backgroundColor
line["width"], line["height"] = width_height(bounding_box(points))
self.data["elements"].append(line)
def save_as(self, filename):
filename = filename if filename.find(".") > -1 else f"{filename}.excalidraw"
with open(filename, "w") as excalidraw_file:
json.dump(self.data, excalidraw_file, indent=2)
print(f"File saved {filename}")
def save_as_lib(self, filename):
filename = filename if filename.find(".") > -1 else f"{filename}.excalidrawlib"
for element in self.data["elements"]:
self.lib["library"].append([element])
with open(filename, "w") as excalidraw_lib_file:
json.dump(self.lib, excalidraw_lib_file, indent=2)
print(f"File saved {filename}")
def get_data(self):
return self.data
+35
View File
@@ -0,0 +1,35 @@
# http://excalidraw.com/?addLibrary=https://gist.githubusercontent.com/lipis/c148134151ac57b7f5df62cba69a4ee4/raw/b4b9bfa27c7399ebb6e4390fdfae2162ff768b39/ngons.excalidrawlib
# https://excalidraw.com/#json=5636464553492480,cBE2J8KaCrIGGucL7UpGbg
import excalidraw
import math
scene = excalidraw.Excalidraw()
radius = 80
initial_angle = math.pi / 2 + math.pi
ngons = [3, 4, 5, 6, 7, 8, 10, 12]
for index, ngon in enumerate(ngons):
points = []
for point in range(ngon + 1):
# source: https://stackoverflow.com/a/7198179/8418
points.append(
[
radius * math.cos(2 * math.pi * point / ngon + initial_angle),
radius * math.sin(2 * math.pi * point / ngon + initial_angle),
]
)
scene.add_line(
(index % 4) * (radius * 2 + radius / 2),
(index // 4) * (radius * 2 + radius / 2),
points,
strokeSharpness="sharp",
fillStyle="solid",
backgroundColor=excalidraw.BACKGROUND[index + 3],
)
scene.save_as("ngons")
scene.save_as_lib("ngons")