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

mNo edit summary
Line 634:
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</lang>
 
=={{header|Phix}}==
Output similar to [[Bitmap/Bézier_curves/Cubic#Mathematica|Mathematica]]
Requires new_image() from [[Bitmap#Phix|Bitmap]], bresLine() from [[Bitmap/Bresenham's_line_algorithm#Phix|Bresenham's_line_algorithm]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]].
Included as demo\rosetta\Bitmap_BezierCubic.exw, results may be verified with demo\rosetta\viewppm.exw
<lang Phix>function cubic_bezier(sequence img, atom x1, atom y1, atom x2, atom y2, atom x3, atom y3, atom x4, atom y4, integer colour, integer segments)
atom t, t1, a, b, c, d
sequence pts = repeat(0,segments*2)
 
for i=0 to segments*2-1 by 2 do
t = i/segments
t1 = 1-t
a = power(t1,3)
b = 3*t*power(t1,2)
c = 3*power(t,2)*t1
d = power(t,3)
pts[i+1] = floor(a*x1+b*x2+c*x3+d*x4)
pts[i+2] = floor(a*y1+b*y2+c*y3+d*y4)
end for
for i=1 to segments*2-2 by 2 do
img = bresLine(img, pts[i], pts[i+1], pts[i+2], pts[i+3], colour)
end for
return img
end function
 
sequence img = new_image(300,200,black)
img = cubic_bezier(img, 0,100, 100,0, 200,200, 300,100, white, 40)
img = bresLine(img,0,100,100,0,green)
img = bresLine(img,100,0,200,200,green)
img = bresLine(img,200,200,300,100,green)
img[1][100] = red
img[100][1] = red
img[200][200] = red
img[300][100] = red
write_ppm("Bézier.ppm",img)</lang>
 
=={{header|PHP}}==
7,806

edits