Bitmap/Bézier curves/Cubic: Difference between revisions

Line 309:
{{works with|Python|3.1}}
 
Extending the example given [[Bresenham%27s_line_algorithm's line algorithm#Python|here]] and using the algorithm from the C solution above:
<lang python>def cubicbezier(self, x0, y0, x1, y1, x2, y2, x3, y3, n=20):
pts = [[0,0] for i in range(n+1)]
for i in range(n+1):
t = i / n
a = pow((1. - t), **3.)
b = 3. * t * pow((1. - t), **2.0)
c = 3.0 * pow(t, **2.0) * (1.0 - t)
d = pow(t, **3.0)
x = int(a * x0 + b * x1 + c * x2 + d * x3)
y = int(a * y0 + b * y1 + c * y2 + d * y3)
pts[i][0] =.add( (x, y) )
pts[i][1] = y
for i in range(n):
j =self.line(pts[i][0], pts[i][1], pts[i+1][0], pts[i+1][1])
self.line(pts[i][0], pts[i][1], pts[j][0], pts[j][1])
Bitmap.cubicbezier = cubicbezier
 
Anonymous user