A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,37 @@
|
|||
USING: accessors arrays kernel locals math math.functions
|
||||
math.ranges math.vectors rosettacode.raster.display
|
||||
rosettacode.raster.storage sequences ui.gadgets ;
|
||||
IN: rosettacode.raster.line
|
||||
|
||||
:: line-points ( pt1 pt2 -- points )
|
||||
pt1 first2 :> y0! :> x0!
|
||||
pt2 first2 :> y1! :> x1!
|
||||
y1 y0 - abs x1 x0 - abs > :> steep
|
||||
steep [
|
||||
y0 x0 y0! x0!
|
||||
y1 x1 y1! x1!
|
||||
] when
|
||||
x0 x1 > [
|
||||
x0 x1 x0! x1!
|
||||
y0 y1 y0! y1!
|
||||
] when
|
||||
x1 x0 - :> deltax
|
||||
y1 y0 - abs :> deltay
|
||||
0 :> current-error!
|
||||
deltay deltax / abs :> deltaerr
|
||||
0 :> ystep!
|
||||
y0 :> y!
|
||||
y0 y1 < [ 1 ystep! ] [ -1 ystep! ] if
|
||||
x0 x1 1 <range> [
|
||||
y steep [ swap ] when 2array
|
||||
current-error deltaerr + current-error!
|
||||
current-error 0.5 >= [
|
||||
ystep y + y!
|
||||
current-error 1 - current-error!
|
||||
] when
|
||||
] { } map-as ;
|
||||
|
||||
! Needs rosettacode.raster.storage for the set-pixel function and to create the image
|
||||
: draw-line ( {R,G,B} pt1 pt2 image -- )
|
||||
[ line-points ] dip
|
||||
[ set-pixel ] curry with each ;
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
thru=: <./ + -~ i.@+ _1 ^ > NB. integers from x through y
|
||||
|
||||
NB.*getBresenhamLine v Returns points for a line given start and end points
|
||||
NB. y is: y0 x0 ,: y1 x1
|
||||
getBresenhamLine=: monad define
|
||||
steep=. ([: </ |@-~/) y
|
||||
points=. |."1^:steep y
|
||||
slope=. %~/ -~/ points
|
||||
ypts=. thru/ {."1 points
|
||||
xpts=. ({: + 0.5 <.@:+ slope * ypts - {.) {.points
|
||||
|."1^:steep ypts,.xpts
|
||||
)
|
||||
|
||||
NB.*drawLines v Draws lines (x) on image (y)
|
||||
NB. x is: 2-item list (start and end points) ; (color)
|
||||
drawLines=: (1&{:: ;~ [: ; [: <@getBresenhamLine"2 (0&{::))@[ setPixels ]
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
myimg=: 0 255 0 makeRGB 20 32 NB. 32 by 20 green image
|
||||
myimg=: ((1 1 ,: 5 11) ; 255 0 0 ) drawLines myimg NB. draw red line from xy point 1 1 to 11 5
|
||||
|
||||
NB. Works for lists of 2 by 2 arrays each defining a line's start and end point.
|
||||
Diamond=: _2]\ _2]\ 9 5 5 15 , 5 15 9 25 , 9 25 13 15 , 13 15 9 5
|
||||
Square =: _2]\ _2]\ 5 5 5 25 , 5 25 13 25 , 13 25 13 5 , 13 5 5 5
|
||||
viewRGB myimg=: (Diamond;255 0 0) drawLines myimg NB. draw 4 red lines to form a diamond
|
||||
viewRGB myimg=: (Square;0 0 255) drawLines myimg NB. draw 4 blue lines to form a square
|
||||
viewRGB (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
fn plot img coord steep col =
|
||||
(
|
||||
if steep then
|
||||
(
|
||||
swap coord[1] coord[2]
|
||||
)
|
||||
setPixels img coord col
|
||||
)
|
||||
|
||||
fn drawLine img start end col =
|
||||
(
|
||||
local steep = (abs (end.y - start.y)) > (abs (end.x - start.x))
|
||||
|
||||
if steep then
|
||||
(
|
||||
swap start.x start.y
|
||||
swap end.x end.y
|
||||
)
|
||||
|
||||
if start.x > end.x then
|
||||
(
|
||||
swap start.x end.x
|
||||
swap start.y end.y
|
||||
)
|
||||
|
||||
local deltaX = end.x - start.x
|
||||
local deltaY = abs (end.y - start.y)
|
||||
local error = deltaX / 2.0
|
||||
local yStep = -1
|
||||
local y = start.y
|
||||
|
||||
if start.y < end.y then
|
||||
(
|
||||
yStep = 1
|
||||
)
|
||||
|
||||
for x in start.x to end.x do
|
||||
(
|
||||
plot img [x, y] steep col
|
||||
error -= deltaY
|
||||
if error < 0 then
|
||||
(
|
||||
y += yStep
|
||||
error += deltaX
|
||||
)
|
||||
)
|
||||
img
|
||||
)
|
||||
|
||||
myBitmap = bitmap 512 512 color:(color 0 0 0)
|
||||
myBitmap = drawLine myBitmap [0, 511] [511, 0] #((color 255 255 255))
|
||||
display myBitmap
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
SegmentBresenham := proc (img, x0, y0, x1, y1)
|
||||
local deltax, deltay, x, y, ystep, steep, err, img2, x02, y02, x12, y12;
|
||||
x02, x12, y02, y12 := y0, y1, x0, x1;
|
||||
steep := abs(x12 - x02) < abs(y12 - y02);
|
||||
img2 := copy(img);
|
||||
if steep then
|
||||
x02, y02 := y02, x02;
|
||||
x12, y12 := y12, x12;
|
||||
end if;
|
||||
if x12 < x02 then
|
||||
x02, x12 := x12, x02;
|
||||
y02, y12 := y12, y02;
|
||||
end if;
|
||||
deltax := x12 - x02;
|
||||
deltay := abs(y12 - y02);
|
||||
err := deltax / 2;
|
||||
y := y02;
|
||||
if y02 < y12 then
|
||||
ystep := 1
|
||||
else
|
||||
ystep := -1
|
||||
end if;
|
||||
for x from x02 to x12 do
|
||||
if steep then
|
||||
img2[y, x] := 0
|
||||
else
|
||||
img2[x, y] := 0
|
||||
end if;
|
||||
err := err - deltay;
|
||||
if err < 0 then
|
||||
y := y + ystep;
|
||||
err := err + deltax
|
||||
end if;
|
||||
end do;
|
||||
return img2;
|
||||
end proc:
|
||||
|
|
@ -0,0 +1 @@
|
|||
Rasterize[ Style[Graphics[Line[{{0, 0}, {20, 10}}]], Antialiasing -> False]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue