p5-python 0.5.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- p5/__init__.py +49 -0
- p5/__main__.py +50 -0
- p5/color.py +80 -0
- p5/constants.py +133 -0
- p5/corefunctions.py +525 -0
- p5/drawcurve.py +146 -0
- p5/mathfunctions.py +149 -0
- p5/p5-python.png +0 -0
- p5/renderer.py +221 -0
- p5/settings.py +58 -0
- p5/timedata.py +30 -0
- p5/transform.py +78 -0
- p5_python-0.5.4.dist-info/METADATA +15 -0
- p5_python-0.5.4.dist-info/RECORD +16 -0
- p5_python-0.5.4.dist-info/WHEEL +5 -0
- p5_python-0.5.4.dist-info/top_level.txt +1 -0
p5/drawcurve.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Splines/Curves from PAdLib - Ian Mallett - 2013
|
|
2
|
+
|
|
3
|
+
from math import *
|
|
4
|
+
import pygame
|
|
5
|
+
from p5.renderer import *
|
|
6
|
+
|
|
7
|
+
# math helper
|
|
8
|
+
def rndint(num): return int(num+0.5)
|
|
9
|
+
def clamp(num, low,high):
|
|
10
|
+
if num < low: return low
|
|
11
|
+
if num > high: return high
|
|
12
|
+
return num
|
|
13
|
+
|
|
14
|
+
def vec_add(v1,v2):
|
|
15
|
+
return [v1[i]+v2[i] for i in range(len(v1))]
|
|
16
|
+
def vec_sub(v1,v2):
|
|
17
|
+
return [v1[i]-v2[i] for i in range(len(v1))]
|
|
18
|
+
def vec_dot(v1,v2):
|
|
19
|
+
return sum([v1[i]*v2[i] for i in range(len(v1))])
|
|
20
|
+
def vec_scale(sc,v):
|
|
21
|
+
return [sc*v[i] for i in range(len(v))]
|
|
22
|
+
def vec_negate(v):
|
|
23
|
+
return [-v[i] for i in range(len(v))]
|
|
24
|
+
|
|
25
|
+
def vec_length_sq(vec):
|
|
26
|
+
return vec_dot(vec,vec)
|
|
27
|
+
def vec_length(vec):
|
|
28
|
+
return vec_dot(vec,vec) ** 0.5
|
|
29
|
+
def vec_norm(vec):
|
|
30
|
+
return vec_scale(1.0/vec_length(vec),vec)
|
|
31
|
+
|
|
32
|
+
def vec_reflect(vec, norm):
|
|
33
|
+
v_dot_n = vec_dot(vec,norm)
|
|
34
|
+
sc = 2 * v_dot_n
|
|
35
|
+
return [sc*norm[i]-vec[i] for i in range(len(vec))]
|
|
36
|
+
|
|
37
|
+
def point_project_line(p, l1,l2):
|
|
38
|
+
#Adapted from http://www.gamedev.net/topic/444154-closest-point-on-a-line/
|
|
39
|
+
p_l1 = vec_sub(p,l1)
|
|
40
|
+
l2_l1 = vec_sub(l2,l1)
|
|
41
|
+
|
|
42
|
+
t = vec_dot(p_l1,l2_l1) / vec_dot(l2_l1,l2_l1)
|
|
43
|
+
|
|
44
|
+
return vec_add(l1,vec_scale(t,l2_l1))
|
|
45
|
+
|
|
46
|
+
# draw bezier curve
|
|
47
|
+
def _draw_bezier(surface, color, controlpointslist, steps, aa, width, blend):
|
|
48
|
+
#Algorithm derived from geometric animations on
|
|
49
|
+
#http://en.wikipedia.org/wiki/B[e]zier_curve
|
|
50
|
+
#The "[e]" is actually an e with an inflection
|
|
51
|
+
#over it; removed for this file's portability.
|
|
52
|
+
|
|
53
|
+
points = []
|
|
54
|
+
|
|
55
|
+
def draw_curve(controlpointslist):
|
|
56
|
+
l = len(controlpointslist)
|
|
57
|
+
|
|
58
|
+
def lerp(p1,p2):
|
|
59
|
+
return vec_add(
|
|
60
|
+
vec_scale(1.0-t,p1),
|
|
61
|
+
vec_scale( t,p2)
|
|
62
|
+
)
|
|
63
|
+
if l == 2:
|
|
64
|
+
points.append(lerp(*controlpointslist))
|
|
65
|
+
else:
|
|
66
|
+
controlpointslist2 = []
|
|
67
|
+
for i in range(l-1):
|
|
68
|
+
controlpointslist2.append(lerp(
|
|
69
|
+
controlpointslist[i],
|
|
70
|
+
controlpointslist[i+1]
|
|
71
|
+
))
|
|
72
|
+
draw_curve(controlpointslist2)
|
|
73
|
+
|
|
74
|
+
for i in range(steps):
|
|
75
|
+
t = float(i) / float(steps-1)
|
|
76
|
+
draw_curve(controlpointslist)
|
|
77
|
+
|
|
78
|
+
points = [list(map(rndint,p)) for p in points]
|
|
79
|
+
draw_lines(surface,points)
|
|
80
|
+
|
|
81
|
+
#draw spline curve
|
|
82
|
+
def _draw_spline(surface, color, closed, pointslist, steps, t,b,c, aa, width, blend):
|
|
83
|
+
#Kochanek-Bartels spline implementation, written long ago and updated.
|
|
84
|
+
t_inc = 1.0/float(steps)
|
|
85
|
+
|
|
86
|
+
#This allows us to draw through all visible control points (normal Kochanek-Bartels
|
|
87
|
+
#splines do not draw through their last endpoints).
|
|
88
|
+
if closed:
|
|
89
|
+
pointslist = [pointslist[-2],pointslist[-1]] + pointslist + [pointslist[0],pointslist[1]]
|
|
90
|
+
else:
|
|
91
|
+
pointslist = [pointslist[0]] + pointslist + [pointslist[-1]]
|
|
92
|
+
|
|
93
|
+
cona = (1-t)*(1+b)*(1-c)*0.5
|
|
94
|
+
conb = (1-t)*(1-b)*(1+c)*0.5
|
|
95
|
+
conc = (1-t)*(1+b)*(1+c)*0.5
|
|
96
|
+
cond = (1-t)*(1-b)*(1-c)*0.5
|
|
97
|
+
|
|
98
|
+
tans = []
|
|
99
|
+
tand = []
|
|
100
|
+
for x in range(len(pointslist)-2):
|
|
101
|
+
tans.append([])
|
|
102
|
+
tand.append([])
|
|
103
|
+
i = 1
|
|
104
|
+
while i < len(pointslist)-1:
|
|
105
|
+
pa = pointslist[i-1]
|
|
106
|
+
pb = pointslist[i ]
|
|
107
|
+
pc = pointslist[i+1]
|
|
108
|
+
x1 = pb[0] - pa[0]
|
|
109
|
+
y1 = pb[1] - pa[1]
|
|
110
|
+
x2 = pc[0] - pb[0]
|
|
111
|
+
y2 = pc[1] - pb[1]
|
|
112
|
+
tans[i-1] = (cona*x1+conb*x2, cona*y1+conb*y2)
|
|
113
|
+
tand[i-1] = (conc*x1+cond*x2, conc*y1+cond*y2)
|
|
114
|
+
i += 1
|
|
115
|
+
|
|
116
|
+
for i in range(1,len(pointslist)-2,1):
|
|
117
|
+
p0 = pointslist[i ]
|
|
118
|
+
p1 = pointslist[i+1]
|
|
119
|
+
m0 = tand[i-1]
|
|
120
|
+
m1 = tans[i ]
|
|
121
|
+
|
|
122
|
+
#draw curve from p0 to p1
|
|
123
|
+
points = [(p0[0],p0[1])]
|
|
124
|
+
t_iter = t_inc
|
|
125
|
+
while t_iter < 1.0:
|
|
126
|
+
h00 = ( 2*(t_iter*t_iter*t_iter)) - ( 3*(t_iter*t_iter)) + 1
|
|
127
|
+
h10 = ( 1*(t_iter*t_iter*t_iter)) - ( 2*(t_iter*t_iter)) + t_iter
|
|
128
|
+
h01 = (-2*(t_iter*t_iter*t_iter)) + ( 3*(t_iter*t_iter))
|
|
129
|
+
h11 = ( 1*(t_iter*t_iter*t_iter)) - ( 1*(t_iter*t_iter))
|
|
130
|
+
px = h00*p0[0] + h10*m0[0] + h01*p1[0] + h11*m1[0]
|
|
131
|
+
py = h00*p0[1] + h10*m0[1] + h01*p1[1] + h11*m1[1]
|
|
132
|
+
points.append((px,py))
|
|
133
|
+
t_iter += t_inc
|
|
134
|
+
points.append((p1[0],p1[1]))
|
|
135
|
+
|
|
136
|
+
points = [list(map(rndint,p)) for p in points]
|
|
137
|
+
draw_lines(surface,points)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def bezier(surface, color, controlpointslist, steps, width=1):
|
|
141
|
+
_draw_bezier(surface, color, controlpointslist, steps, False, width, False)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def spline(surface, color, closed, pointslist, steps, t=0.0,b=0.0,c=0.0, width=1):
|
|
145
|
+
_draw_spline(surface, color, closed, pointslist, steps, t,b,c, False, width, False)
|
|
146
|
+
|
p5/mathfunctions.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import builtins
|
|
3
|
+
from p5.settings import *
|
|
4
|
+
import random as randompy
|
|
5
|
+
from random import randint, triangular, seed, gauss
|
|
6
|
+
|
|
7
|
+
def randomSeed(var):
|
|
8
|
+
builtins.RANDOMSEED = var
|
|
9
|
+
randompy.seed(var)
|
|
10
|
+
|
|
11
|
+
def random(*args):
|
|
12
|
+
if len(args)==0 :
|
|
13
|
+
return randompy.random()
|
|
14
|
+
if len(args)==1 :
|
|
15
|
+
if type(args[0]) == 'list' or type(args[0]) == 'set' or type(args[0]) == 'tuple':
|
|
16
|
+
return randompy.choice(args[0])
|
|
17
|
+
else:
|
|
18
|
+
return randompy.uniform(0,args[0])
|
|
19
|
+
if len(args)==2 :
|
|
20
|
+
return randompy.uniform(args[0],args[1])
|
|
21
|
+
|
|
22
|
+
def constrain(num, low, high):
|
|
23
|
+
if num <= high and num >= low:
|
|
24
|
+
return num
|
|
25
|
+
elif num < low:
|
|
26
|
+
return low
|
|
27
|
+
elif num > high:
|
|
28
|
+
return high
|
|
29
|
+
return -1
|
|
30
|
+
|
|
31
|
+
def pow(x,exp):
|
|
32
|
+
return x**exp
|
|
33
|
+
|
|
34
|
+
def floor(x):
|
|
35
|
+
return math.floor(x)
|
|
36
|
+
|
|
37
|
+
def ceil(x):
|
|
38
|
+
return math.ceil(x)
|
|
39
|
+
|
|
40
|
+
def dist(x, y):
|
|
41
|
+
return math.dist(x, y)
|
|
42
|
+
|
|
43
|
+
def dist(x1, y1, x2, y2):
|
|
44
|
+
return math.dist((x1, y1), (x2, y2))
|
|
45
|
+
|
|
46
|
+
def exp(x):
|
|
47
|
+
return math.exp(x)
|
|
48
|
+
|
|
49
|
+
def lerp(a,b,t):
|
|
50
|
+
return (1 - t) * 1 + t * b
|
|
51
|
+
|
|
52
|
+
def log(x):
|
|
53
|
+
return math.log(x)
|
|
54
|
+
|
|
55
|
+
def log10(x):
|
|
56
|
+
return math.log(x, 10)
|
|
57
|
+
|
|
58
|
+
def log2(x):
|
|
59
|
+
return math.log(x, 2)
|
|
60
|
+
|
|
61
|
+
def logb(x, b):
|
|
62
|
+
return math.log(x, b)
|
|
63
|
+
|
|
64
|
+
def mag(x, y):
|
|
65
|
+
return math.dist((0, 0), (x, y))
|
|
66
|
+
|
|
67
|
+
def rerange(value, start1, stop1, start2, stop2, clamp=False):
|
|
68
|
+
n = (stop2 - start2)*value / (stop1 - start1) + start2
|
|
69
|
+
if clamp:
|
|
70
|
+
return constrain(n, start2, stop2)
|
|
71
|
+
else:
|
|
72
|
+
return n
|
|
73
|
+
|
|
74
|
+
def map(val,srclow,srcup,destlow,destup):
|
|
75
|
+
return (val-srclow)/(srcup-srclow) * (destup-destlow) + destlow
|
|
76
|
+
|
|
77
|
+
def norm(value, start, stop):
|
|
78
|
+
return rererange(value, start, stop, 0, 1)
|
|
79
|
+
|
|
80
|
+
def sq(n):
|
|
81
|
+
return n*n
|
|
82
|
+
|
|
83
|
+
def sqrt(x):
|
|
84
|
+
return math.sqrt(x)
|
|
85
|
+
|
|
86
|
+
def frac(x):
|
|
87
|
+
return math.modf(x)[1]
|
|
88
|
+
|
|
89
|
+
def sin(x):
|
|
90
|
+
return math.sin(x)
|
|
91
|
+
|
|
92
|
+
def cos(x):
|
|
93
|
+
return math.cos(x)
|
|
94
|
+
|
|
95
|
+
def tan(x):
|
|
96
|
+
return math.tan(x)
|
|
97
|
+
|
|
98
|
+
def asin(x):
|
|
99
|
+
return math.asin(x)
|
|
100
|
+
|
|
101
|
+
def acos(x):
|
|
102
|
+
return math.acos(x)
|
|
103
|
+
|
|
104
|
+
def atan2(x):
|
|
105
|
+
return math.atan2(x)
|
|
106
|
+
|
|
107
|
+
def atan(x):
|
|
108
|
+
return math.atan(x)
|
|
109
|
+
|
|
110
|
+
def degrees(x):
|
|
111
|
+
return math.degrees(x)
|
|
112
|
+
|
|
113
|
+
def radians(x):
|
|
114
|
+
return math.radians(x)
|
|
115
|
+
|
|
116
|
+
def randomgaussian(mean,dev):
|
|
117
|
+
return gauss(mean,dev)
|
|
118
|
+
|
|
119
|
+
def integral(f, a, b, n=100, type="midpoint"):
|
|
120
|
+
if type == "midpoint":
|
|
121
|
+
return _midpoint(f, a, b, n)
|
|
122
|
+
elif type == "trapezoid":
|
|
123
|
+
return _trapezoid(f, a, b, n)
|
|
124
|
+
elif type == "simpsons":
|
|
125
|
+
return _simpsons(f, a, b, n)
|
|
126
|
+
else:
|
|
127
|
+
return -1
|
|
128
|
+
pass
|
|
129
|
+
|
|
130
|
+
def _midpoint(f, a, b, n):
|
|
131
|
+
dx = (b-a)/n
|
|
132
|
+
result = 0
|
|
133
|
+
|
|
134
|
+
xi = linspace(a + 0.5*dx, b - 0.5*dx, n)
|
|
135
|
+
result = dx*sum(f(xi))
|
|
136
|
+
return result
|
|
137
|
+
|
|
138
|
+
def _trapezoid(f, a, b, n):
|
|
139
|
+
dx = (b-a)/n
|
|
140
|
+
result = 0
|
|
141
|
+
|
|
142
|
+
xi = linspace(a, b, n+1)
|
|
143
|
+
result = sum(f(xi)) - 0.5*f(a) - 0.5*f(b)
|
|
144
|
+
return result*dx
|
|
145
|
+
|
|
146
|
+
def _simpsons(f, a, b, n):
|
|
147
|
+
if n % 2:
|
|
148
|
+
return -1
|
|
149
|
+
return (b-a)/6.0 * (f(a) + 4*f((a+b)/2) + f(b))
|
p5/p5-python.png
ADDED
|
Binary file
|
p5/renderer.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import cairo
|
|
3
|
+
from p5.settings import *
|
|
4
|
+
|
|
5
|
+
def bgra(color): #convert color RGBA 0-255 to BGRA 0-1
|
|
6
|
+
b_g_r_a = [0,0,0,0]
|
|
7
|
+
b_g_r_a[0] = color[2]/255
|
|
8
|
+
b_g_r_a[1] = color[1]/255
|
|
9
|
+
b_g_r_a[2] = color[0]/255
|
|
10
|
+
|
|
11
|
+
if len(color)<4:
|
|
12
|
+
b_g_r_a[3] = 1
|
|
13
|
+
else:
|
|
14
|
+
b_g_r_a[3] = color[3]/255
|
|
15
|
+
return b_g_r_a
|
|
16
|
+
|
|
17
|
+
def a_radians(a):
|
|
18
|
+
if builtins.ANGLEMODE == DEGREES:
|
|
19
|
+
return a * math.pi/180
|
|
20
|
+
else:
|
|
21
|
+
return a
|
|
22
|
+
def clear_background(surface, width, height, color):
|
|
23
|
+
|
|
24
|
+
ctx = cairo.Context(surface)
|
|
25
|
+
ctx.set_source_rgba(*bgra(color))
|
|
26
|
+
ctx.rectangle(0, 0, width, height)
|
|
27
|
+
ctx.fill()
|
|
28
|
+
|
|
29
|
+
def draw_line(surface, x1, y1, x2, y2):
|
|
30
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
31
|
+
scale_xy = settings["scale_amnt"]
|
|
32
|
+
origin_x = settings["real_ox"]
|
|
33
|
+
origin_y = settings["real_oy"]
|
|
34
|
+
stroke_color = settings["stroke_color"]
|
|
35
|
+
thickness = settings["stroke_weight"]
|
|
36
|
+
fill_color = settings["fill_color"]
|
|
37
|
+
ctx = cairo.Context(surface)
|
|
38
|
+
ctx.translate(origin_x, origin_y)
|
|
39
|
+
ctx.rotate(rotate_angle)
|
|
40
|
+
ctx.set_line_width(thickness)
|
|
41
|
+
ctx.move_to(x1, y1)
|
|
42
|
+
ctx.line_to(x2, y2)
|
|
43
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
44
|
+
ctx.stroke()
|
|
45
|
+
|
|
46
|
+
def draw_rect(surface, x, y, width, height):
|
|
47
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
48
|
+
scale_xy = settings["scale_amnt"]
|
|
49
|
+
origin_x = settings["real_ox"]
|
|
50
|
+
origin_y = settings["real_oy"]
|
|
51
|
+
stroke_color = settings["stroke_color"]
|
|
52
|
+
thickness = settings["stroke_weight"]
|
|
53
|
+
fill_color = settings["fill_color"]
|
|
54
|
+
ctx = cairo.Context(surface)
|
|
55
|
+
ctx.translate(origin_x, origin_y)
|
|
56
|
+
ctx.rotate(rotate_angle)
|
|
57
|
+
ctx.rectangle(x, y, width, height)
|
|
58
|
+
ctx.set_line_width(thickness)
|
|
59
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
60
|
+
ctx.fill_preserve()
|
|
61
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
62
|
+
ctx.stroke()
|
|
63
|
+
|
|
64
|
+
def draw_polygon(surface, vertices):
|
|
65
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
66
|
+
scale_xy = settings["scale_amnt"]
|
|
67
|
+
origin_x = settings["real_ox"]
|
|
68
|
+
origin_y = settings["real_oy"]
|
|
69
|
+
stroke_color = settings["stroke_color"]
|
|
70
|
+
thickness = settings["stroke_weight"]
|
|
71
|
+
fill_color = settings["fill_color"]
|
|
72
|
+
ctx = cairo.Context(surface)
|
|
73
|
+
ctx.translate(origin_x, origin_y)
|
|
74
|
+
ctx.rotate(rotate_angle)
|
|
75
|
+
ctx.move_to(*vertices[0])
|
|
76
|
+
for n in range(1,len(vertices)):
|
|
77
|
+
ctx.line_to(*vertices[n])
|
|
78
|
+
ctx.close_path()
|
|
79
|
+
ctx.set_line_width(thickness)
|
|
80
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
81
|
+
ctx.fill_preserve()
|
|
82
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
83
|
+
ctx.stroke()
|
|
84
|
+
|
|
85
|
+
def draw_lines(surface, vertices):
|
|
86
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
87
|
+
scale_xy = settings["scale_amnt"]
|
|
88
|
+
origin_x = settings["real_ox"]
|
|
89
|
+
origin_y = settings["real_oy"]
|
|
90
|
+
stroke_color = settings["stroke_color"]
|
|
91
|
+
thickness = settings["stroke_weight"]
|
|
92
|
+
ctx = cairo.Context(surface)
|
|
93
|
+
ctx.translate(origin_x, origin_y)
|
|
94
|
+
ctx.rotate(rotate_angle)
|
|
95
|
+
ctx.move_to(*vertices[0])
|
|
96
|
+
for n in range(1,len(vertices)):
|
|
97
|
+
ctx.line_to(*vertices[n])
|
|
98
|
+
ctx.set_line_width(thickness)
|
|
99
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
100
|
+
ctx.stroke()
|
|
101
|
+
|
|
102
|
+
def draw_circle(surface, x, y, radius):
|
|
103
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
104
|
+
scale_xy = settings["scale_amnt"]
|
|
105
|
+
origin_x = settings["real_ox"]
|
|
106
|
+
origin_y = settings["real_oy"]
|
|
107
|
+
stroke_color = settings["stroke_color"]
|
|
108
|
+
thickness = settings["stroke_weight"]
|
|
109
|
+
fill_color = settings["fill_color"]
|
|
110
|
+
ctx = cairo.Context(surface)
|
|
111
|
+
ctx.translate(origin_x, origin_y)
|
|
112
|
+
ctx.rotate(rotate_angle)
|
|
113
|
+
ctx.arc(x, y, radius, 0, 2.0 * math.pi)
|
|
114
|
+
ctx.set_line_width(thickness)
|
|
115
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
116
|
+
ctx.fill_preserve()
|
|
117
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
118
|
+
ctx.stroke()
|
|
119
|
+
|
|
120
|
+
def draw_ellipse(surface, x, y, width, height):
|
|
121
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
122
|
+
scale_xy = settings["scale_amnt"]
|
|
123
|
+
origin_x = settings["real_ox"]
|
|
124
|
+
origin_y = settings["real_oy"]
|
|
125
|
+
stroke_color = settings["stroke_color"]
|
|
126
|
+
thickness = settings["stroke_weight"]
|
|
127
|
+
fill_color = settings["fill_color"]
|
|
128
|
+
ctx = cairo.Context(surface)
|
|
129
|
+
ctx.translate(origin_x, origin_y)
|
|
130
|
+
ctx.rotate(rotate_angle)
|
|
131
|
+
radius = width/2
|
|
132
|
+
scale_x = 1.0
|
|
133
|
+
scale_y = height / width
|
|
134
|
+
ctx.scale(scale_x, scale_y)
|
|
135
|
+
ctx.arc(x, y/scale_y, radius, 0, 2.0 * math.pi)
|
|
136
|
+
ctx.set_line_width(thickness)
|
|
137
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
138
|
+
ctx.fill_preserve()
|
|
139
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
140
|
+
ctx.stroke()
|
|
141
|
+
|
|
142
|
+
def draw_arc(surface, x, y, width, height, angle1, angle2, mode):
|
|
143
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
144
|
+
scale_xy = settings["scale_amnt"]
|
|
145
|
+
origin_x = settings["real_ox"]
|
|
146
|
+
origin_y = settings["real_oy"]
|
|
147
|
+
stroke_color = settings["stroke_color"]
|
|
148
|
+
thickness = settings["stroke_weight"]
|
|
149
|
+
fill_color = settings["fill_color"]
|
|
150
|
+
ctx = cairo.Context(surface)
|
|
151
|
+
ctx.translate(origin_x, origin_y)
|
|
152
|
+
ctx.rotate(rotate_angle)
|
|
153
|
+
if mode == "pie":
|
|
154
|
+
ctx.move_to(x, y)
|
|
155
|
+
radius = width/2
|
|
156
|
+
scale_x = 1.0
|
|
157
|
+
scale_y = height / width
|
|
158
|
+
ctx.scale(scale_x, scale_y)
|
|
159
|
+
ctx.arc(x, y/scale_y, radius, angle1, angle2)
|
|
160
|
+
if (mode == "chord") or (mode == "pie"):
|
|
161
|
+
ctx.close_path()
|
|
162
|
+
if (mode == "chord") or (mode == "pie") or (mode == "open"):
|
|
163
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
164
|
+
ctx.fill_preserve()
|
|
165
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
166
|
+
ctx.stroke()
|
|
167
|
+
|
|
168
|
+
def draw_text(surface, txt, x, y, style):
|
|
169
|
+
rotate_angle = a_radians(settings["rotate_amnt"])
|
|
170
|
+
scale_xy = settings["scale_amnt"]
|
|
171
|
+
origin_x = settings["real_ox"]
|
|
172
|
+
origin_y = settings["real_oy"]
|
|
173
|
+
stroke_color = settings["text_outline"]
|
|
174
|
+
thickness = settings["stroke_weight"]
|
|
175
|
+
fill_color = settings["text_color"]
|
|
176
|
+
font_name = settings["text_font"]
|
|
177
|
+
font_size = settings["text_size"]
|
|
178
|
+
ctx = cairo.Context(surface)
|
|
179
|
+
ctx.translate(origin_x, origin_y)
|
|
180
|
+
ctx.rotate(rotate_angle)
|
|
181
|
+
font_options = cairo.FontOptions()
|
|
182
|
+
font_options.set_antialias(cairo.ANTIALIAS_DEFAULT)
|
|
183
|
+
ctx.set_font_options(font_options)
|
|
184
|
+
cairo_slant = cairo.FONT_SLANT_NORMAL
|
|
185
|
+
cairo_weight = cairo.FONT_WEIGHT_NORMAL
|
|
186
|
+
if style == "italic" or style == "bolditalic":
|
|
187
|
+
cairo_slant = cairo.FONT_SLANT_ITALIC
|
|
188
|
+
if style == "bold" or style == "bolditalic":
|
|
189
|
+
cairo_weight = cairo.FONT_WEIGHT_BOLD
|
|
190
|
+
ctx = cairo.Context(surface)
|
|
191
|
+
ctx.select_font_face(font_name, cairo_slant,cairo_weight)
|
|
192
|
+
ctx.set_font_size(font_size)
|
|
193
|
+
xbearing, ybearing, width, height, dx, dy = ctx.text_extents(txt)
|
|
194
|
+
if settings["text_align"] == RIGHT:
|
|
195
|
+
x = x - width
|
|
196
|
+
if settings["text_align"] == CENTER:
|
|
197
|
+
x = x - width/2
|
|
198
|
+
# Render outline
|
|
199
|
+
ctx.set_source_rgba(*bgra(stroke_color))
|
|
200
|
+
for i in range(-thickness, thickness+1):
|
|
201
|
+
for j in range(-thickness, thickness+1):
|
|
202
|
+
ctx.move_to(x + i, y + j)
|
|
203
|
+
ctx.show_text(txt)
|
|
204
|
+
|
|
205
|
+
ctx.set_source_rgba(*bgra(fill_color))
|
|
206
|
+
ctx.move_to(x, y)
|
|
207
|
+
ctx.show_text(txt)
|
|
208
|
+
ctx.stroke()
|
|
209
|
+
'''
|
|
210
|
+
def draw_curve(surface,color):
|
|
211
|
+
x, y = 100, 50
|
|
212
|
+
x1, y1 = 140, 50
|
|
213
|
+
x2, y2 = 60, 100
|
|
214
|
+
x3, y3 = 190, 150
|
|
215
|
+
ctx = cairo.Context(surface)
|
|
216
|
+
ctx.move_to(x, y)
|
|
217
|
+
ctx.curve_to(x1, y1, x2, y2, x3, y3)
|
|
218
|
+
ctx.set_source_rgba(*bgra(color))
|
|
219
|
+
ctx.stroke()
|
|
220
|
+
'''
|
|
221
|
+
|
p5/settings.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import builtins
|
|
2
|
+
from p5.constants import *
|
|
3
|
+
|
|
4
|
+
pg_Screen = None
|
|
5
|
+
clock = None
|
|
6
|
+
|
|
7
|
+
builtins.mouseX = 0
|
|
8
|
+
builtins.mouseY = 0
|
|
9
|
+
builtins.pmouseX = None
|
|
10
|
+
builtins.pmouseY = None
|
|
11
|
+
builtins.movedX = None
|
|
12
|
+
builtins.movedY = None
|
|
13
|
+
builtins.mouseButton = None
|
|
14
|
+
builtins.mouseIsPressed = False
|
|
15
|
+
builtins.keyIsPressed = False
|
|
16
|
+
builtins.key = None
|
|
17
|
+
builtins.keyCode = None
|
|
18
|
+
builtins.width = 360
|
|
19
|
+
builtins.height = 360
|
|
20
|
+
builtins.shape = []
|
|
21
|
+
builtins.framerate = 60
|
|
22
|
+
builtins.is_Looping = True
|
|
23
|
+
builtins.ANGLEMODE = DEGREES
|
|
24
|
+
builtins.RANDOMSEED = 0
|
|
25
|
+
builtins.MILLIS = 0
|
|
26
|
+
builtins.frameCount = 0
|
|
27
|
+
builtins.ellipse_mode = CENTER
|
|
28
|
+
builtins.rect_mode = CORNER
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# variables
|
|
32
|
+
stack = [] # store for settings
|
|
33
|
+
settings = {
|
|
34
|
+
"fill_color" : [255,255,255,255],
|
|
35
|
+
"no_fill" : False,
|
|
36
|
+
"no_stroke" : False,
|
|
37
|
+
"stroke_weight" : 1,
|
|
38
|
+
"stroke_color" : [0,0,0,255],
|
|
39
|
+
"rotate_amnt" : 0,
|
|
40
|
+
"scale_amnt" : 1,
|
|
41
|
+
"text_size" : 12,
|
|
42
|
+
"text_font" : "Arial",
|
|
43
|
+
"text_bold" : False,
|
|
44
|
+
"text_italic" : False,
|
|
45
|
+
"text_align" : LEFT,
|
|
46
|
+
"text_color" : [0,0,0,255],
|
|
47
|
+
"text_outline" : [0,0,0,0],
|
|
48
|
+
"origin_x" : 0,
|
|
49
|
+
"origin_y" : 0,
|
|
50
|
+
"real_ox" : 0,
|
|
51
|
+
"real_oy" : 0,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
vertices = []
|
|
56
|
+
curvevertices = []
|
|
57
|
+
shape = None
|
|
58
|
+
|
p5/timedata.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
import time
|
|
3
|
+
import builtins
|
|
4
|
+
from p5.settings import *
|
|
5
|
+
|
|
6
|
+
def year():
|
|
7
|
+
return datetime.now().year
|
|
8
|
+
|
|
9
|
+
def month():
|
|
10
|
+
return datetime.now().month
|
|
11
|
+
|
|
12
|
+
def day():
|
|
13
|
+
return datetime.now().day
|
|
14
|
+
|
|
15
|
+
def hour():
|
|
16
|
+
return datetime.now().hour
|
|
17
|
+
|
|
18
|
+
def minute():
|
|
19
|
+
return datetime.now().minute
|
|
20
|
+
|
|
21
|
+
def second():
|
|
22
|
+
return datetime.now().second
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def milli_sec():
|
|
26
|
+
return int(round(time.time() * 1000))
|
|
27
|
+
|
|
28
|
+
def millis():
|
|
29
|
+
return milli_sec() - builtins.MILLIS
|
|
30
|
+
pass
|
p5/transform.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# TRANSFORM
|
|
2
|
+
from p5.settings import *
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
def transform_canvas_reset(): # Transformations are reset at the beginning of the draw loop.
|
|
6
|
+
global settings
|
|
7
|
+
settings["rotate_amnt"] = 0
|
|
8
|
+
settings["scale_amnt"] = 1
|
|
9
|
+
settings["origin_x"] = 0
|
|
10
|
+
settings["origin_y"] = 0
|
|
11
|
+
settings["real_ox"] = 0
|
|
12
|
+
settings["real_oy"] = 0
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
def resetMatrix():
|
|
16
|
+
transform_canvas_reset()
|
|
17
|
+
|
|
18
|
+
def rotate(angle):
|
|
19
|
+
current_angle = settings["rotate_amnt"]
|
|
20
|
+
current_angle += angle
|
|
21
|
+
if current_angle < 0:
|
|
22
|
+
current_angle = 360 + current_angle
|
|
23
|
+
if current_angle > 359:
|
|
24
|
+
current_angle = current_angle % 360
|
|
25
|
+
settings["rotate_amnt"] = current_angle
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
def scale(amnt):
|
|
29
|
+
settings["scale_amnt"] = amnt
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
def real_oxy(xx,yy): # calulate real x,y coordinate
|
|
33
|
+
aa = settings["rotate_amnt"]
|
|
34
|
+
ox = settings["real_ox"]
|
|
35
|
+
oy = settings["real_oy"]
|
|
36
|
+
x = xx
|
|
37
|
+
y = yy
|
|
38
|
+
if builtins.ANGLEMODE == DEGREES:
|
|
39
|
+
aa = aa * math.pi/180
|
|
40
|
+
x = ox + int(xx * math.cos(aa)) - int(yy * math.sin(aa))
|
|
41
|
+
y = oy + int(yy * math.cos(aa)) + int(xx * math.sin(aa))
|
|
42
|
+
settings["real_ox"] = x
|
|
43
|
+
settings["real_oy"] = y
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def translate(x, y):
|
|
48
|
+
global settings
|
|
49
|
+
settings["origin_x"] = settings["origin_x"] + x
|
|
50
|
+
settings["origin_y"] = settings["origin_y"] + y
|
|
51
|
+
real_oxy(x, y) # calculate real origin x,y
|
|
52
|
+
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
def push():
|
|
56
|
+
global stack,settings
|
|
57
|
+
r = settings["rotate_amnt"]
|
|
58
|
+
s = settings["scale_amnt"]
|
|
59
|
+
ox = settings["origin_x"]
|
|
60
|
+
oy = settings["origin_y"]
|
|
61
|
+
rox= settings["real_ox"]
|
|
62
|
+
roy= settings["real_oy"]
|
|
63
|
+
stack.append(r)
|
|
64
|
+
stack.append(s)
|
|
65
|
+
stack.append(ox)
|
|
66
|
+
stack.append(oy)
|
|
67
|
+
stack.append(rox)
|
|
68
|
+
stack.append(roy)
|
|
69
|
+
|
|
70
|
+
def pop():
|
|
71
|
+
global stack,settings
|
|
72
|
+
settings["real_oy"] = stack.pop()
|
|
73
|
+
settings["real_ox"] = stack.pop()
|
|
74
|
+
settings["origin_y"] = stack.pop()
|
|
75
|
+
settings["origin_x"] = stack.pop()
|
|
76
|
+
settings["scale_amnt"] = stack.pop()
|
|
77
|
+
settings["rotate_amnt"] = stack.pop()
|
|
78
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: p5-python
|
|
3
|
+
Version: 0.5.4
|
|
4
|
+
Summary: A package wrapping pygame functionality in p5.js calling conventions.
|
|
5
|
+
Home-page: https://github.com/gromko/p5-python
|
|
6
|
+
Author: Grygoriy Gromko
|
|
7
|
+
Author-email: gr.gromko@gmail.com
|
|
8
|
+
Maintainer: Grygoriy Gromko
|
|
9
|
+
Maintainer-email: gr.gromko@gmail.com
|
|
10
|
+
Requires-Python: >=3.8.0
|
|
11
|
+
Requires-Dist: multipledispatch (<0.7.0,>=0.6.0)
|
|
12
|
+
Requires-Dist: numpy (<2.0.0,>=1.22.2)
|
|
13
|
+
Requires-Dist: pygame (<3.0.0,>=2.1.2)
|
|
14
|
+
|
|
15
|
+
#p5 is a friendly tool for learning to code and make art. It is a free and open-source Python library built by an inclusive, nurturing community. p5 is a Python library that provides high level drawing functionality to help you quickly create simulations and interactive art using Python.
|