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

Content deleted Content added
Ada solution added
Vedit macro language added
Line 158:
by_pair pts (fun p0 p1 -> line ~p0 ~p1);
;;</ocaml>
 
=={{header|Vedit macro language}}==
This implementation uses de Casteljau's algorithm to recursively split the Bezier curve into two smaller segments until the segment is short enough to be approximated with a straight line.
The advantage of this method is that only integer calculations are needed, and the most complex operations are addition and shift right. (I have used multiplication and division here for clarity.)
 
Constant recursion depth is used here. Recursion depth of 5 seems to give accurate enough result in most situations. In real world implementations, some adaptive method is often used to decide when to stop recursion.
 
<pre>
// Daw a Cubic bezier curve
// #20, #30 = Start point
// #21, #31 = Control point 1
// #22, #32 = Control point 2
// #23, #33 = end point
// #40 = depth of recursion
 
:CUBIC_BEZIER:
if (#40 > 0) {
#24 = (#20+#21)/2; #34 = (#30+#31)/2
#26 = (#22+#23)/2; #36 = (#32+#33)/2
#27 = (#20+#21*2+#22)/4; #37 = (#30+#31*2+#32)/4
#28 = (#21+#22*2+#23)/4; #38 = (#31+#32*2+#33)/4
#29 = (#20+#21*3+#22*3+#23)/8; #39 = (#30+#31*3+#32*3+#33)/8
Num_Push(20,40)
#21 = #24; #31 = #34 // control 1
#22 = #27; #32 = #37 // control 2
#23 = #29; #33 = #39 // end point
#40--
Call("CUBIC_BEZIER") // Draw "left" part
Num_Pop(20,40)
Num_Push(20,40)
#20 = #29; #30 = #39 // start point
#21 = #28; #31 = #38 // control 1
#22 = #26; #32 = #36 // control 2
#40--
Call("CUBIC_BEZIER") // Draw "right" part
Num_Pop(20,40)
} else {
#1=#20; #2=#30; #3=#23; #4=#33
Call("DRAW_LINE")
}
return
</pre>