Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

bresenham elisp

Name: Anonymous 2014-10-04 19:54

(defun octant0or3 (x0 y0 dx dy xdirection)
"draws a line in octant 0 or 3 (|dx|>=dy).
xdirection: 1 left to right, -1 right to left."
(let* ((dy*2 (* dy 2))
(dy*2-dx*2 (- dy*2 (round (* dx 2))))
(err (- dy*2 (round dx))))
(dot x0 y0)
(while (>= (setq dx (1- dx)) 0)
(if (>= err 0)
(setq y0 (1+ y0)
err (+ err dy*2-dx*2))
(setq err (+ err dy*2)))
(setq x0 (+ x0 xdirection))
(dot x0 y0))))

(defun octant1or2 (x0 y0 dx dy xdirection) ; |dx|<dy
(let* ((dx*2 (* dx 2))
(dx*2-dy*2 (- dx*2 (round (* dy 2))))
(err (- dx*2 (round dy))))
(dot x0 y0)
(while (>= (setq dy (1- dy)) 0)
(if (>= err 0)
(setq x0 (+ x0 xdirection)
err (+ err dx*2-dy*2))
(setq err (+ err dx*2)))
(setq y0 (1+ y0))
(dot x0 y0))))

(defun line (x0 y0 x1 y1)
(let (dx dy temp)
(when (> y0 y1)
(setq temp y0
y0 y1
y1 temp
temp x0
x0 x1
x1 temp))
(setq dx (- x1 x0)
dy (- y1 y0))
(if (> dx 0)
(if (> dx dy)
(octant0or3 x0 y0 dx dy 1)
(octant1or2 x0 y0 dx dy 1))
(setq dx (- dx))
(if (> dx dy)
(octant0or3 x0 y0 dx dy -1)
(octant1or2 x0 y0 dx dy -1)))))

(defun dot (x y)
(goto-line y)
(forward-char x)
(delete-char 1)
(insert ?\*))

(defun drawing-buffer ()
(with-current-buffer (get-buffer-create "*drawing-buffer*")
(erase-buffer)
(dotimes (i 30)
(insert (make-string 70 ?\.) ?\n))
(display-buffer "*drawing-buffer*")))

(drawing-buffer)
(with-current-buffer "*drawing-buffer*"
(line 13 21 32 5)
(line 13 21 56 21)
(line 32 5 56 21)
(line 9 10 33 26)
(line 9 10 58 10)
(line 33 26 58 10))

Name: etaoin !S/UJugsOl. 2014-10-04 22:35

any comments?

Name: Anonymous 2014-10-04 23:57

>>2
yer trip iz jugs

Name: Anonymous 2014-10-05 1:18

Pretty cool, do you use it for something or was it just for fun?

setq
Oy vey!

Name: Anonymous 2014-10-05 1:32

>>4
i just wanted to learn bresenham, and test it.
what is the problem with setq?

Name: Anonymous 2014-10-05 1:41

>>5
i just wanted to learn bresenham
Neat, I didn't know there was anyone on /prog/ who actually wanted to learn things.

setq
Just poking fun at non-Functional lisp.

Name: Anonymous 2014-10-05 3:00

>>6
fuck off, r-tard, serious programming (as in low-level) is all imperative.

Name: Anonymous 2014-10-05 8:19

>>7
Yes, yes, I'm sure many toilet scrubbers think that all serious work is done with broom and plunger.

Name: Anonymous 2014-10-05 9:50

Can you contribute a simpler triangle drawing routine? For now I've wrote
https://github.com/saniv/symta/blob/master/bindings/gfx/src/gfx.c

typedef struct lerp {double x, i} lerp;

static void lerp_init(lerp *l, int sx, int ex, int first_step, int steps) {
l->i = (double)(ex - sx) / steps;
l->x = (double)sx + first_step*l->i;
}

static void lerp_advance(lerp *l) {
l->x += l->i;
}

#define SWAP(x,y) do {int t_ = x; x = y; y = t_;} while(0)

#define TRIANGLE_ROW(a,b) do { \
int x1 = (int)a.x; \
int x2 = (int)b.x; \
if (x1 < x2) gfx_hline(gfx, color, x1, y, x2-x1); \
else gfx_hline(gfx, color, x2, y, x1-x2); \
} while (0)

void gfx_triangle(gfx_t *gfx, uint32_t color, int ax, int ay, int bx, int by, int cx, int cy) {
int beg_y, cen_y, end_y;
int y, e;
lerp l, r;

if(ax < 0 && bx < 0 && cx < 0) return;
if(ax >= gfx->w && bx >= gfx->w && cx >= gfx->w) return;
if(ax == bx && ax == cx) return;

if (ay > by) {
SWAP(ax,bx);
SWAP(ay,by);
}

if (ay > cy) {
SWAP(ax,cx);
SWAP(ay,cy);
}

if(by > cy) {
SWAP(bx,cx);
SWAP(by,cy);
}

beg_y = ay;
cen_y = by;
end_y = cy;

if(end_y == beg_y || end_y < 0 || beg_y >= gfx->h) return;

if (beg_y < 0) {
lerp_init(&r, ax, cx, -beg_y, end_y-beg_y);
y = 0;
} else {
lerp_init(&r, ax, cx, 0, end_y-beg_y);
y = beg_y;
}

if (y < cen_y) {
if (beg_y < 0) lerp_init(&l, ax, bx,-beg_y, cen_y-beg_y);
else lerp_init(&l, ax, bx, 0, cen_y-beg_y);

if (cen_y > gfx->h) e = gfx->h;
else e = cen_y;

for (; y < e; ++y) {
TRIANGLE_ROW(l,r);
lerp_advance(&l);
lerp_advance(&r);
}
}

if(cen_y < end_y) {
lerp_init(&l, bx, cx, y-cen_y, end_y-cen_y);
if (end_y > gfx->h) end_y = gfx->h;

for (; y < end_y; ++y) {
TRIANGLE_ROW(l,r);
lerp_advance(&l);
lerp_advance(&r);
}
}
}

Name: Anonymous 2014-10-05 9:57

>>6

didn't know there was anyone on /prog/ who actually wanted to learn things.
most progriders already know everything and just want to shitpost.

Name: Alexander Dubček 2014-10-05 15:41

Check out the parallel lines I drew in my post number.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List