CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
|
|
@ -0,0 +1,44 @@
|
|||
void Line( const float x1, const float y1, const float x2, const float y2, const Color& color )
|
||||
{
|
||||
// Bresenham's line algorithm
|
||||
const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
|
||||
if(steep)
|
||||
{
|
||||
std::swap(x1, y1);
|
||||
std::swap(x2, y2);
|
||||
}
|
||||
|
||||
if(x1 > x2)
|
||||
{
|
||||
std::swap(x1, x2);
|
||||
std::swap(y1, y2);
|
||||
}
|
||||
|
||||
const float dx = x2 - x1;
|
||||
const float dy = fabs(y2 - y1);
|
||||
|
||||
float error = dx / 2.0f;
|
||||
const int ystep = (y1 < y2) ? 1 : -1;
|
||||
int y = (int)y1;
|
||||
|
||||
const int maxX = (int)x2;
|
||||
|
||||
for(int x=(int)x1; x<maxX; x++)
|
||||
{
|
||||
if(steep)
|
||||
{
|
||||
SetPixel(y,x, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetPixel(x,y, color);
|
||||
}
|
||||
|
||||
error -= dy;
|
||||
if(error < 0)
|
||||
{
|
||||
y += ystep;
|
||||
error += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(defun draw-line (buffer x1 y1 x2 y2 pixel)
|
||||
(declare (type rgb-pixel-buffer buffer))
|
||||
(declare (type integer x1 y1 x2 y2))
|
||||
(declare (type rgb-pixel pixel))
|
||||
(let* ((dist-x (abs (- x1 x2)))
|
||||
(dist-y (abs (- y1 y2)))
|
||||
(steep (> dist-y dist-x)))
|
||||
(when steep
|
||||
(psetf x1 y1 y1 x1
|
||||
x2 y2 y2 x2))
|
||||
(when (> x1 x2)
|
||||
(psetf x1 x2 x2 x1
|
||||
y1 y2 y2 y1))
|
||||
(let* ((delta-x (- x2 x1))
|
||||
(delta-y (abs (- y1 y2)))
|
||||
(error (floor delta-x 2))
|
||||
(y-step (if (< y1 y2) 1 -1))
|
||||
(y y1))
|
||||
(loop
|
||||
:for x :upfrom x1 :to x2
|
||||
:do (progn (if steep
|
||||
(setf (rgb-pixel buffer x y) pixel)
|
||||
(setf (rgb-pixel buffer y x) pixel))
|
||||
(setf error (- error delta-y))
|
||||
(when (< error 0)
|
||||
(incf y y-step)
|
||||
(incf error delta-x)))))
|
||||
buffer))
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
module bitmap_bresenhams_line_algorithm;
|
||||
|
||||
import std.algorithm, std.math, bitmap;
|
||||
|
||||
void drawLine(Color)(Image!Color img,
|
||||
size_t x1, size_t y1,
|
||||
in size_t x2, in size_t y2,
|
||||
in Color color)
|
||||
pure nothrow {
|
||||
immutable int dx = x2 - x1;
|
||||
immutable int ix = (dx > 0) - (dx < 0);
|
||||
immutable size_t dx2 = abs(dx) * 2;
|
||||
int dy = y2 - y1;
|
||||
immutable int iy = (dy > 0) - (dy < 0);
|
||||
immutable size_t dy2 = abs(dy) * 2;
|
||||
img[x1, y1] = color;
|
||||
|
||||
if (dx2 >= dy2) {
|
||||
int error = dy2 - (dx2 / 2);
|
||||
while (x1 != x2) {
|
||||
if (error >= 0 && (error || (ix > 0))) {
|
||||
error -= dx2;
|
||||
y1 += iy;
|
||||
}
|
||||
|
||||
error += dy2;
|
||||
x1 += ix;
|
||||
img[x1, y1] = color;
|
||||
}
|
||||
} else {
|
||||
int error = dx2 - (dy2 / 2);
|
||||
while (y1 != y2) {
|
||||
if (error >= 0 && (error || (iy > 0))) {
|
||||
error -= dy2;
|
||||
x1 += ix;
|
||||
}
|
||||
|
||||
error += dx2;
|
||||
y1 += iy;
|
||||
img[x1, y1] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
version (bitmap_bresenhams_line_algorithm_main) {
|
||||
void main() {
|
||||
auto img = new Image!RGB(25, 22);
|
||||
img.drawLine(5, 5, 15, 20, RGB.white);
|
||||
img.drawLine(3, 20, 10, 12, RGB.white);
|
||||
img.textualShow();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
def swap(&left, &right) { # From [[Generic swap]]
|
||||
def t := left
|
||||
left := right
|
||||
right := t
|
||||
}
|
||||
|
||||
def drawLine(image, var x0, var y0, var x1, var y1, color) {
|
||||
def steep := (y1 - y0).abs() > (x1 - x0).abs()
|
||||
if (steep) {
|
||||
swap(&x0, &y0)
|
||||
swap(&x1, &y1)
|
||||
}
|
||||
if (x0 > x1) {
|
||||
swap(&x0, &x1)
|
||||
swap(&y0, &y1)
|
||||
}
|
||||
def deltax := x1 - x0
|
||||
def deltay := (y1 - y0).abs()
|
||||
def ystep := if (y0 < y1) { 1 } else { -1 }
|
||||
var error := deltax // 2
|
||||
var y := y0
|
||||
for x in x0..x1 {
|
||||
if (steep) { image[y, x] := color } else { image[x, y] := color }
|
||||
error -= deltay
|
||||
if (error < 0) {
|
||||
y += ystep
|
||||
error += deltax
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
def i := makeImage(5, 20)
|
||||
drawLine(i, 1, 1, 3, 18, makeColor.fromFloat(0,1,1))
|
||||
i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Bresenham.ppm>))
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
include std/console.e
|
||||
include std/graphics.e
|
||||
include std/math.e
|
||||
|
||||
-- the new_image function and related code in the 25 or so
|
||||
-- lines below are from http://rosettacode.org/wiki/Basic_bitmap_storage#Euphoria
|
||||
-- as of friday, march 2, 2012
|
||||
|
||||
-- Some color constants:
|
||||
constant
|
||||
black = #000000,
|
||||
white = #FFFFFF,
|
||||
red = #FF0000,
|
||||
green = #00FF00,
|
||||
blue = #0000FF
|
||||
|
||||
-- Create new image filled with some color
|
||||
function new_image(integer width, integer height, atom fill_color)
|
||||
return repeat(repeat(fill_color,height),width)
|
||||
end function
|
||||
|
||||
--grid used for drawing lines in this program
|
||||
sequence screenData = new_image(16,16,black)
|
||||
|
||||
--the line algorithm
|
||||
function bresLine(sequence screenData, integer x0, integer y0, integer x1, integer y1, integer color)
|
||||
|
||||
integer deltaX = abs(x1 - x0), deltaY = abs(y1 - y0)
|
||||
integer stepX, stepY, lineError, error2
|
||||
|
||||
if x0 < x1 then
|
||||
stepX = 1
|
||||
else
|
||||
stepX = -1
|
||||
end if
|
||||
|
||||
if y0 < y1 then
|
||||
stepY = 1
|
||||
else
|
||||
stepY = -1
|
||||
end if
|
||||
|
||||
if deltaX > deltaY then
|
||||
lineError = deltaX
|
||||
else
|
||||
lineError = -deltaY
|
||||
end if
|
||||
|
||||
lineError = round(lineError / 2, 1)
|
||||
|
||||
while 1 do
|
||||
|
||||
screenData[x0][y0] = color
|
||||
|
||||
if (x0 = x1 and y0 = y1) then
|
||||
exit
|
||||
end if
|
||||
|
||||
error2 = lineError
|
||||
|
||||
if error2 > -deltaX then
|
||||
lineError -= deltaY
|
||||
x0 += stepX
|
||||
end if
|
||||
if error2 < deltaY then
|
||||
lineError += deltaX
|
||||
y0 += stepY
|
||||
end if
|
||||
end while
|
||||
return screenData -- return modified version of the screenData sequence
|
||||
end function
|
||||
|
||||
--prevents console output wrapping to next line if it is too big for the screen
|
||||
wrap(0)
|
||||
--outer diamond
|
||||
screenData = bresLine(screenData,8,1,16,8,white)
|
||||
screenData = bresLine(screenData,16,8,8,16,white)
|
||||
screenData = bresLine(screenData,8,16,1,8,white)
|
||||
screenData = bresLine(screenData,1,8,8,1,white)
|
||||
--inner diamond
|
||||
screenData = bresLine(screenData,8,4,12,8,white)
|
||||
screenData = bresLine(screenData,12,8,8,12,white)
|
||||
screenData = bresLine(screenData,8,12,4,8,white)
|
||||
screenData = bresLine(screenData,4,8,8,4,white)
|
||||
-- center lines drawing from left to right, and the next from right to left.
|
||||
screenData = bresLine(screenData,7,7,9,7,white)
|
||||
screenData = bresLine(screenData,9,9,7,9,white)
|
||||
--center dot
|
||||
screenData = bresLine(screenData,8,8,8,8,white)
|
||||
|
||||
--print to the standard console output
|
||||
for i = 1 to 16 do
|
||||
puts(1,"\n")
|
||||
for j = 1 to 16 do
|
||||
if screenData[j][i] = black then
|
||||
printf(1, "%s", ".")
|
||||
else
|
||||
printf(1, "%s", "#")
|
||||
end if
|
||||
end for
|
||||
end for
|
||||
|
||||
puts(1,"\n\n")
|
||||
any_key()
|
||||
|
||||
--/*
|
||||
--output was edited to replace the color's hex digits for clearer output graphics.
|
||||
--to output all the hex digits, use printf(1,"%06x", screenData[j][i])
|
||||
--to output 'shortened' hex digits, use :
|
||||
--printf(1, "%x", ( abs( ( (screenData[j][i] / #FFFFF) - 1 ) ) - 1 ) )
|
||||
--and
|
||||
--printf(1,"%x", abs( ( (screenData[j][i] / #FFFFF) - 1 ) ) )
|
||||
--
|
||||
--,respectively in the last if check.
|
||||
--*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue