Xiaolin Wu's line algorithm: Difference between revisions

m
Added the Sidef language
mNo edit summary
m (Added the Sidef language)
Line 1,706:
View the PNG, available at the following URL because RosettaCode image uploads were disabled:
https://lh5.googleusercontent.com/GxBAHV4nebuO1uiKboKc6nQmmtlJV47jPwVZnQHcbV7TKm0kjdKfKteclCfxmSdFJnSKvYYoB5I
 
=={{header|Sidef}}==
{{trans|Perl}}
<lang ruby>func plot(x, y, c) {
c && printf("plot %d %d %.1f\n", x, y, c);
}
 
func fpart(x) {
x - int(x);
}
 
func rfpart(x) {
1 - fpart(x);
}
 
func drawLine(x0, y0, x1, y1) {
 
var p = plot;
if (abs(y1 - y0) > abs(x1 - x0)) {
p = {|arg| plot(arg[1, 0, 2]...) };
[y0, x0, y1, x1] » (\x0, \y0, \x1, \y1);
}
 
if (x0 > x1) {
[x1, x0, y1, y0] » (\x0, \x1, \y0, \y1);
}
 
var dx = (x1 - x0);
var dy = (y1 - y0);
var gradient = (dy / dx);
 
var xends = [];
var intery;
 
# handle the endpoints
[[x0, y0], [x1, y1]].each { |xy|
var (x, y) = xy...;
var xend = int(x + 0.5);
var yend = (y + gradient*(xend-x));
var xgap = rfpart(x + 0.5);
 
var x_pixel = xend;
var y_pixel = yend.int;
xends << x_pixel;
 
p.call(x_pixel, y_pixel , rfpart(yend) * xgap);
p.call(x_pixel, y_pixel+1, fpart(yend) * xgap);
intery == nil || next;
 
# first y-intersection for the main loop
intery = (yend + gradient);
}
 
# main loop
range(xends[0]+1, xends[1]-1).each { |x|
p.call(x, intery.int, rfpart(intery));
p.call(x, intery.int+1, fpart(intery));
intery += gradient;
}
}
 
drawLine(0, 1, 10, 2);</lang>
{{out}}
<pre>
plot 0 1 0.5
plot 10 2 0.5
plot 1 1 0.9
plot 1 2 0.1
plot 2 1 0.8
plot 2 2 0.2
plot 3 1 0.7
plot 3 2 0.3
plot 4 1 0.6
plot 4 2 0.4
plot 5 1 0.5
plot 5 2 0.5
plot 6 1 0.4
plot 6 2 0.6
plot 7 1 0.3
plot 7 2 0.7
plot 8 1 0.2
plot 8 2 0.8
plot 9 1 0.1
plot 9 2 0.9
</pre>
 
=={{header|Tcl}}==
2,747

edits