forked from dbt-ethz/mola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colab2D.py
127 lines (99 loc) · 3.33 KB
/
colab2D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['Benjamin Dillenburger','Demetris Shammas','Mathias Bernhard']
__copyright__ = 'Copyright 2019 / Digital Building Technologies DBT / ETH Zurich'
__license__ = 'MIT License'
__email__ = ['<[email protected]>']
p5code=""
def display_lines2D(lines):
global p5code
for line in lines:
p5code += "line(" + str(line.v1.x) + "," + str(line.v1.y) + "," + str(line.v2.x) + "," + str(line.v2.y) + ");"
def display_faces2D(faces):
global p5code
for face in faces:
if face.color != None:
p5code += "fill("+str(face.color[0])+","+str(face.color[1])+","+str(face.color[2])+");"
p5code += "beginShape();"
for v in face.vertices:
p5code += "vertex("+str(v.x)+","+str(v.y)+");"
p5code += "endShape();"
def save_canvas(fileName):
global p5code
p5code+="saveCanvas(canvas,'"+fileName+"');"
def save_image(fileName):
global p5code
p5code+="save("+fileName+");"
def _begin2D(width=1024,height=768):
return '''<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.js"></script><script>new p5();var canvas = createCanvas('''+str(width)+","+str(height)+''');'''
def _end2D():
return '''</script>'''
def text_size(size):
global p5code
p5code+="textSize("+str(size)+");"
def translate(x,y):
global p5code
p5code+="translate("+str(x)+","+str(y)+");"
def scale(x,y):
global p5code
p5code+="scale("+str(x)+","+str(y)+");"
def text(text,x,y):
global p5code
p5code+="text("+str(text)+","+str(x)+","+str(y)+");"
def no_stroke():
global p5code
p5code+="noStroke();"
def no_fill():
global p5code
p5code+="noFill();"
def stroke_weight(weight):
global p5code
p5code+="strokeWeight("+str(weight)+");"
def color_mode(mode,max):
global p5code
p5code+="colorMode("+str(mode)+","+str(max)+");"
def stroke(r,g,b):
global p5code
p5code+="stroke("+str(r)+","+str(g)+","+str(b)+");"
def fill(r,g=None,b=None):
global p5code
if isinstance(r,list) or isinstance(r,tuple):
p5code+="fill("+str(r[0])+","+str(r[1])+","+str(r[2])+");"
else:
p5code+="fill("+str(r)+","+str(g)+","+str(b)+");"
def line(x1,y1,x2,y2):
global p5code
p5code+="line("+str(x1)+","+str(y1)+","+str(x2)+","+str(y2)+");"
def ellipse(x,y,w,h):
global p5code
p5code+="ellipse("+str(x)+","+str(y)+","+str(w)+","+str(h)+");"
def circle(x,y,radius):
global p5code
p5code+="ellipse("+str(x)+","+str(y)+","+str(radius)+","+str(radius)+");"
def rect(x1,y1,x2,y2):
global p5code
p5code+="rect("+str(x1)+","+str(y1)+","+str(x2)+","+str(y2)+");"
def begin_shape():
global p5code
p5code+="beginShape();"
def end_shape():
global p5code
p5code+="endShape();"
def vertex(x,y):
global p5code
p5code+="vertex("+str(x)+","+str(y)+");"
def background(r,g,b):
global p5code
p5code+="background("+str(r)+","+str(g)+","+str(b)+");"
def begin_draw(width=1024,height=768):
global p5code
p5code=_begin2D(width,height)
p5code+="rectMode(CORNERS);"
def end_draw():
global p5code
p5code+=_end2D()
return p5code
# helper function to get a drawing canvas inside the notebook
def whiteboard():
from IPython.display import IFrame
return IFrame('https://dbt.arch.ethz.ch/temp/canvas', width=800, height=500)