Xiaolin Wu's line algorithm: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: with caveats comment) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<
REAL one
Line 104:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Xiaolin_Wu's_line_algorithm.png Screenshot from Atari 8-bit computer]
Line 110:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 756:
FBVARSCinfo_fin:
</syntaxhighlight>
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<
#NoEnv
SetBatchLines, -1
Line 864:
DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
return, S
}</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
END
Line 921:
GCOL 1
LINE X%*2, Y%*2, X%*2, Y%*2
ENDPROC</
=={{header|C}}==
Line 927:
This implementation follows straightforwardly the pseudocode given on Wikipedia. (Further analysis of the code could give suggestions for improvements).
<
image img,
unsigned int x0, unsigned int y0,
Line 933:
color_component r,
color_component g,
color_component b );</
<
rgb_color_p to, float br)
{
Line 1,038:
#undef round_
#undef rfpart_
</syntaxhighlight>
=={{header|C++}}==
<
#include <functional>
#include <algorithm>
Line 1,116:
}
}
</syntaxhighlight>
=={{header|C#}}==
<syntaxhighlight lang="c">
public class Line
{
Line 1,228:
}
}
</syntaxhighlight>
=={{header|D}}==
{{trans|Go}}
This performs the mixing of the colors, both in grey scale and RGB.
<
/// Plots anti-aliased line by Xiaolin Wu's line algorithm.
Line 1,341:
im2.aaLine(177.4, 12.3, 127, 222.5, red);
im2.savePPM6("xiaolin_lines2.ppm");
}</
=={{header|FreeBASIC}}==
Line 1,347:
Only changed xend=round() in xend=ipart() to make it more in line with FreeBASIC's own line drawing routine. Rfpart give me some trouble so I changed if somewhat.
The small functions where all converted into macro's
<
' compile with: fbc -s console or fbc -s gui
' Xiaolin Wu’s line-drawing algorithm
Line 1,473:
While Inkey <> "" : Wend
Sleep
End</
=={{header|Go}}==
<
import "math"
Line 1,554:
intery = intery + gradient
}
}</
Demonstration program:
<
// Files required to build supporting package raster are found in:
Line 1,571:
g.AaLine(177.4, 12.3, 127, 222.5)
g.Bitmap().WritePpmFile("wu.ppm")
}</
=={{header|Haskell}}==
Line 1,577:
Example makes use of [http://hackage.haskell.org/package/JuicyPixels <tt>JuicyPixels</tt>] for serialization to PNG format and and [http://hackage.haskell.org/package/primitive <tt>primitive</tt>] to abstract away memory-related operations. This is a fairly close translation of the algorithm as described on [https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm Wikipedia]:
<
module Main (main) where
Line 1,687:
-- Write it out to a file on disc
writePng "xiaolin-wu-algorithm.png" img</
Building and running this program will generate an output PNG file named <code>xiaolin-wu-algorithm.png</code> showing a white antialiased diagonal line.
Line 1,693:
=={{header|J}}==
'''Solution:'''
<
coinsert'jgl2'
Line 1,717:
weights=. ((2&}.,~ xgap*2&{.)&.(_1&|.) (,.~-.) 1|ylist)
weights (drawpt r)"1 2 (,:+&0 1)"1 xlist,.<.ylist
)</
'''Example use:'''
<
wd'pc win closeok; minwh 600 400;cc g isidraw flush; pshow;' NB. J802 or later
glpaint glclear ''
glpaint drawLine 10 10 590 390</
=={{header|Java}}==
[[File:xiaolinwu_java.png|200px|thumb|right]]
{{works with|Java|8}}
<
import static java.lang.Math.*;
import javax.swing.*;
Line 1,836:
});
}
}</
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
fpart(x) = mod(x, one(x))
Line 1,915:
img = fill(Gray(1.0N0f8), 250, 250);
drawline!(img, 8, 8, 192, 154)</
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.awt.*
Line 2,019:
f.isVisible = true
}
}</
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
NoMainWin
WindowWidth = 270
Line 2,178:
Loop
End Function
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ReverseFractionalPart[x_] := 1 - FractionalPart[x]
ReplacePixelWithAlpha[img_Image, pos_ -> colvals : {_, _, _},
Line 2,230:
]
image = ConstantImage[Black, {100, 100}];
Fold[DrawLine[#1, {20, 10}, #2] &, image, AngleVector[{20, 10}, {75, #}] & /@ Subdivide[0, Pi/2, 10]]</
=={{header|Nim}}==
Simple translation of the Wikipedia algorithm.
<
import imageman
Line 2,314:
for x1 in countup(100, 700, 60):
img.drawLine(400, 700, x1.toFloat, 100)
img.savePNG("xiaoling_wu.png", compression = 9)</
=={{header|Pascal}}==
Line 2,321:
Based on Wikipwdia pseudocode with some optimizations and alpha handling.
<
program wu;
uses
Line 2,444:
until false;
end.
</syntaxhighlight>
=={{header|Perl}}==
This is mostly a translation of the pseudo-code on Wikipedia, except that the <code>$plot</code> trick was inspired by the Raku example.
<
use strict;
use warnings;
Line 2,526:
}
__END__
</syntaxhighlight>
{{out}}
<pre>plot 0 1 0.5
Line 2,553:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/wuline.htm here], with caveats as below. Resize the window to show lines at any angle
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\XiaolinWuLine.exw
Line 2,823:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
=={{header|PicoLisp}}==
<
(de plot (Img X Y C)
Line 2,882:
(prinl 120 " " 90)
(prinl 100)
(for Y Img (apply printsp Y)) ) )</
=={{header|PureBasic}}==
<
Plot(x, y, RGB(Red(Color) * (b), Green(Color) * (b), Blue(Color) * (b)))
EndMacro
Line 2,956:
Repeat
event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow</
=={{header|Python}}==
<
algorithm
Line 3,035:
filename = sys.argv[1]
img.save(filename)
print 'image saved to', filename</
=={{header|Racket}}==
<
(require 2htdp/image)
Line 3,121:
img-2
(save-image img-1 "images/xiaolin-wu-racket-1.png")
(save-image img-2 "images/xiaolin-wu-racket-2.png")</
Output files:
Line 3,129:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
sub fpart(\x) { x - floor(x) }
Line 3,188:
}
draw-line [0,1], [10,2];</
{{out}}
<pre>plot 0 1 1
Line 3,223:
<br>Also, it takes in account (that can easily be overlooked) of the note after the description of the algorithm:
<br>'''Note''': If at the beginning of the routine abs(''dx'') < abs(''dy'') is true, then all plotting should be done with '''x''' and '''y''' reversed.
<
background= '·' /*background character: a middle-dot. */
image.= background /*fill the array with middle-dots. */
Line 3,282:
plotXY: parse arg xx,yy,bc,switchYX; if switchYX then parse arg yy,xx
image.xx.yy=bc; minX=min(minX, xx); maxX=max(maxX,xx)
minY=min(minY, yy); maxY=max(maxY,yy); return</
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,307:
=={{header|Ruby}}==
{{trans|Tcl}}
<
def fpart(n); n - ipart(n); end
def rfpart(n); 1.0 - fpart(n); end
Line 3,373:
bitmap.draw_line_antialised(Pixel[10, 10], Pixel[a,490], RGBColour::YELLOW)
end
bitmap.draw_line_antialised(Pixel[10, 10], Pixel[490,490], RGBColour::YELLOW)</
=={{header|Scala}}==
Uses [[Bitmap#Scala]].
<
import math.{floor => ipart, round, abs}
Line 3,445:
intery = intery + gradient
}
}</
'''Example:'''
Test line drawing in various directions including vertical, horizontal, 45° and oblique (such lines are drawn multiple times to test swapped parameters).
<
val img = new RgbBitmap(r*2+1, r*2+1)
val line = drawLine(plotter(img, Color.GRAY)_)_
Line 3,458:
line(a, b)
}
javax.imageio.ImageIO.write(img.image, "png", new java.io.File("XiaolinWuLineAlgorithm.png"))</
{{out}}
View the PNG, available at the following URL because RosettaCode image uploads were disabled:
Line 3,465:
=={{header|Sidef}}==
{{trans|Perl}}
<
c && printf("plot %d %d %.1f\n", x, y, c);
}
Line 3,522:
}
drawLine(0, 1, 10, 2);</
{{out}}
<pre>
Line 3,548:
=={{header|Swift}}==
<
// apply pixel of color at x,y with an OVER blend to the bitmap
public func pixel(color: Color, x: Int, y: Int) {
Line 3,641:
}
}
</syntaxhighlight>
=={{header|Tcl}}==
{{libheader|Tk}}
Uses code from [[Basic bitmap storage#Tcl]]
<
package require Tk
Line 3,733:
toplevel .wu
label .wu.l -image $img
pack .wu.l</
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<
import "dome" for Window
import "math" for Math
Line 3,828:
}
var Game = XiaolinWu.new(640, 640)</
=={{header|Yabasic}}==
{{trans|Phix}}
<
rB = 255 : gB = 255 : bB = 224
Line 3,915:
draw_line(w, 0, 200, 200)
draw_line(0, h, 200, 200)
draw_line(w, h, 200, 200)</
[[Category:Geometry]]
|