Curve that touches three points: Difference between revisions
Content added Content deleted
m (→{{header|Perl 6}}: added zkl header) |
(→{{header|zkl}}: added code) |
||
Line 157: | Line 157: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Go}} |
|||
⚫ | |||
Uses Image Magick and |
|||
<lang zkl></lang> |
|||
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
|||
<lang zkl>const X=0, Y=1; // p.X == p[X] |
|||
var p=L(L(10.0, 10.0), L(100.0, 200.0), L(200.0, 10.0)); // (x,y) |
|||
fcn lagrange(x){ // float-->float |
|||
(x - p[1][X])*(x - p[2][X])/(p[0][X] - p[1][X])/(p[0][X] - p[2][X])*p[0][Y] + |
|||
(x - p[0][X])*(x - p[2][X])/(p[1][X] - p[0][X])/(p[1][X] - p[2][X])*p[1][Y] + |
|||
(x - p[0][X])*(x - p[1][X])/(p[2][X] - p[0][X])/(p[2][X] - p[1][X])*p[2][Y] |
|||
} |
|||
fcn getPoints(n){ // int-->( (x,y) ..) |
|||
pts:=List.createLong(2*n+1); |
|||
dx,pt,cnt := (p[1][X] - p[0][X])/n.toFloat(), p[0][X], n; |
|||
do(2){ |
|||
foreach i in (cnt){ |
|||
x:=pt + dx*i; |
|||
pts.append(L(x,lagrange(x))); |
|||
} |
|||
dx,pt,cnt = (p[2][X] - p[1][X])/n, p[1][X], n+1; |
|||
} |
|||
pts |
|||
} |
|||
fcn main{ |
|||
var [const] n=50; // more than enough for this |
|||
img,color := PPM(210,210,0xffffff), 0; // white background, black curve |
|||
foreach x,y in (p){ img.cross(x.toInt(),y.toInt(), 0xff0000) } // mark 3 pts |
|||
a,b := p[0][X].toInt(), p[0][Y].toInt(); // curve starting point |
|||
foreach x,y in (getPoints(n)){ |
|||
x,y = x.toInt(),y.toInt(); |
|||
img.line(a,b, x,y, color); // can only deal with ints |
|||
a,b = x,y; |
|||
} |
|||
img.writeJPGFile("quadraticCurve.zkl.jpg"); |
|||
⚫ | |||
{{out}} |
{{out}} |
||
Image at [http://www.zenkinetic.com/Images/RosettaCode/quadraticCurve.zkl.jpg sunflower quadratic curve] |
|||
<pre> |
|||
</pre> |