Bitmap/Bresenham's line algorithm: Difference between revisions
Content deleted Content added
m →{{header|Java}}: remove dead code |
Devstopfix (talk | contribs) Missing divide by 2 on init of error term |
||
Line 298: | Line 298: | ||
<lang clojure> |
<lang clojure> |
||
(defn draw-line |
(defn draw-line |
||
"Draw a line from x1,y1 to x2,y2 using Bresenham's, to a java BufferedImage in the colour of pixel." |
"Draw a line from x1,y1 to x2,y2 using Bresenham's, to a java BufferedImage in the colour of pixel." |
||
[buffer x1 y1 x2 y2 pixel] |
[buffer x1 y1 x2 y2 pixel] |
||
(let [dist-x (abs (- x1 x2)) |
(let [dist-x (Math/abs (- x1 x2)) |
||
dist-y (abs (- y1 y2)) |
dist-y (Math/abs (- y1 y2)) |
||
steep (> dist-y dist-x)] |
steep (> dist-y dist-x)] |
||
(let [[x1 y1 x2 y2] (if steep [y1 x1 y2 x2] [x1 y1 x2 y2])] |
(let [[x1 y1 x2 y2] (if steep [y1 x1 y2 x2] [x1 y1 x2 y2])] |
||
(let [[x1 y1 x2 y2] (if (> x1 x2) [x2 y2 x1 y1] [x1 y1 x2 y2])] |
(let [[x1 y1 x2 y2] (if (> x1 x2) [x2 y2 x1 y1] [x1 y1 x2 y2])] |
||
(let [delta-x (- x2 x1) |
(let [delta-x (- x2 x1) |
||
delta-y (abs (- y1 y2)) |
delta-y (Math/abs (- y1 y2)) |
||
y-step (if (< y1 y2) 1 -1)] |
y-step (if (< y1 y2) 1 -1)] |
||
Line 314: | Line 315: | ||
#(.setRGB buffer (int %2) (int %1) pixel))] |
#(.setRGB buffer (int %2) (int %1) pixel))] |
||
(loop [x x1 y y1 error (floor delta-x 2) ] |
(loop [x x1 y y1 error (Math/floor (/ delta-x 2)) ] |
||
(plot x y) |
(plot x y) |
||
(if (< x x2) |
(if (< x x2) |