Xiaolin Wu's line algorithm: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: with caveats comment)
m (syntax highlighting fixup automation)
Line 16:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
REAL one
Line 104:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 756:
FBVARSCinfo_fin:
 
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
{{libheader|GDIP}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#SingleInstance, Force
#NoEnv
SetBatchLines, -1
Line 864:
DllCall("msvcrt\_i64to" U, "Int64",n, "Str",S, "Int",Base)
return, S
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> PROCdrawAntiAliasedLine(100, 100, 600, 400, 0, 0, 0)
END
Line 921:
GCOL 1
LINE X%*2, Y%*2, X%*2, Y%*2
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
Line 927:
This implementation follows straightforwardly the pseudocode given on Wikipedia. (Further analysis of the code could give suggestions for improvements).
 
<langsyntaxhighlight lang="c">void draw_line_antialias(
image img,
unsigned int x0, unsigned int y0,
Line 933:
color_component r,
color_component g,
color_component b );</langsyntaxhighlight>
 
<langsyntaxhighlight lang="c">inline void _dla_changebrightness(rgb_color_p from,
rgb_color_p to, float br)
{
Line 1,038:
#undef round_
#undef rfpart_
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
 
<langsyntaxhighlight lang="c++">
#include <functional>
#include <algorithm>
Line 1,116:
}
}
</syntaxhighlight>
</lang>
 
=={{header|C#}}==
<syntaxhighlight lang="c">
<lang c>
public class Line
{
Line 1,228:
}
}
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|Go}}
This performs the mixing of the colors, both in grey scale and RGB.
<langsyntaxhighlight lang="d">import std.math, std.algorithm, grayscale_image;
 
/// 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");
}</langsyntaxhighlight>
 
=={{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
<langsyntaxhighlight FreeBASIClang="freebasic">' version 21-06-2015
' compile with: fbc -s console or fbc -s gui
' Xiaolin Wu’s line-drawing algorithm
Line 1,473:
While Inkey <> "" : Wend
Sleep
End</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package raster
 
import "math"
Line 1,554:
intery = intery + gradient
}
}</langsyntaxhighlight>
Demonstration program:
<langsyntaxhighlight lang="go">package main
 
// 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")
}</langsyntaxhighlight>
 
=={{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]:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE ScopedTypeVariables #-}
 
module Main (main) where
Line 1,687:
 
-- Write it out to a file on disc
writePng "xiaolin-wu-algorithm.png" img</langsyntaxhighlight>
 
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:'''
<langsyntaxhighlight lang="j">load'gl2'
coinsert'jgl2'
 
Line 1,717:
weights=. ((2&}.,~ xgap*2&{.)&.(_1&|.) (,.~-.) 1|ylist)
weights (drawpt r)"1 2 (,:+&0 1)"1 xlist,.<.ylist
)</langsyntaxhighlight>
 
'''Example use:'''
<langsyntaxhighlight lang="j"> wd'pc win closeok; xywh 0 0 300 200;cc g isigraph; pas 0 0; pshow;' NB. J6 or earlier
wd'pc win closeok; minwh 600 400;cc g isidraw flush; pshow;' NB. J802 or later
glpaint glclear ''
glpaint drawLine 10 10 590 390</langsyntaxhighlight>
 
=={{header|Java}}==
[[File:xiaolinwu_java.png|200px|thumb|right]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.awt.*;
import static java.lang.Math.*;
import javax.swing.*;
Line 1,836:
});
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using Images
 
fpart(x) = mod(x, one(x))
Line 1,915:
 
img = fill(Gray(1.0N0f8), 250, 250);
drawline!(img, 8, 8, 192, 154)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.awt.*
Line 2,019:
f.isVisible = true
}
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
NoMainWin
WindowWidth = 270
Line 2,178:
Loop
End Function
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[ReverseFractionalPart, ReplacePixelWithAlpha, DrawEndPoint, DrawLine]
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]]</langsyntaxhighlight>
 
=={{header|Nim}}==
Simple translation of the Wikipedia algorithm.
<langsyntaxhighlight Nimlang="nim">import math
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)</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,321:
Based on Wikipwdia pseudocode with some optimizations and alpha handling.
 
<langsyntaxhighlight lang="pascal">
program wu;
uses
Line 2,444:
until false;
end.
</syntaxhighlight>
</lang>
 
=={{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.
<langsyntaxhighlight lang="perl">#!perl
use strict;
use warnings;
Line 2,526:
}
__END__
</syntaxhighlight>
</lang>
{{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
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(scl 2)
 
(de plot (Img X Y C)
Line 2,882:
(prinl 120 " " 90)
(prinl 100)
(for Y Img (apply printsp Y)) ) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Macro PlotB(x, y, Color, b)
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</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">"""Script demonstrating drawing of anti-aliased lines using Xiaolin Wu's line
algorithm
 
Line 3,035:
filename = sys.argv[1]
img.save(filename)
print 'image saved to', filename</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang 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")</langsyntaxhighlight>
 
Output files:
Line 3,129:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub plot(\x, \y, \c) { say "plot {x} {y} {c}" }
sub fpart(\x) { x - floor(x) }
Line 3,188:
}
 
draw-line [0,1], [10,2];</langsyntaxhighlight>
{{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''': &nbsp; If at the beginning of the routine &nbsp; abs(''dx'') < abs(''dy'') &nbsp; is true, then all plotting should be done with &nbsp; '''x''' &nbsp; and &nbsp; '''y''' &nbsp; reversed.
<langsyntaxhighlight lang="rexx">/*REXX program plots/draws (ASCII) a line using the Xiaolin Wu line algorithm. */
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,307:
=={{header|Ruby}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="ruby">def ipart(n); n.truncate; end
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)</langsyntaxhighlight>
 
=={{header|Scala}}==
Uses [[Bitmap#Scala]].
<langsyntaxhighlight Scalalang="scala">import java.awt.Color
import math.{floor => ipart, round, abs}
 
Line 3,445:
intery = intery + gradient
}
}</langsyntaxhighlight>
'''Example:'''
 
Test line drawing in various directions including vertical, horizontal, 45° and oblique (such lines are drawn multiple times to test swapped parameters).
<langsyntaxhighlight Scalalang="scala">val r = 120
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"))</langsyntaxhighlight>
{{out}}
View the PNG, available at the following URL because RosettaCode image uploads were disabled:
Line 3,465:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func plot(x, y, c) {
c && printf("plot %d %d %.1f\n", x, y, c);
}
Line 3,522:
}
 
drawLine(0, 1, 10, 2);</langsyntaxhighlight>
{{out}}
<pre>
Line 3,548:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Darwin
// 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>
</lang>
 
=={{header|Tcl}}==
{{libheader|Tk}}
Uses code from [[Basic bitmap storage#Tcl]]
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 3,733:
toplevel .wu
label .wu.l -image $img
pack .wu.l</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 3,828:
}
 
var Game = XiaolinWu.new(640, 640)</langsyntaxhighlight>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">bresline = false // space toggles, for comparison
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)</langsyntaxhighlight>
 
[[Category:Geometry]]
10,339

edits