fix c64 bresenham line inaccuracy

This commit is contained in:
Irmen de Jong 2021-03-09 20:44:06 +01:00
parent 62ec77e148
commit 6bd205c02a
2 changed files with 30 additions and 38 deletions

View File

@ -34,7 +34,6 @@ graphics {
sub line(uword @zp x1, ubyte @zp y1, uword @zp x2, ubyte @zp y2) { sub line(uword @zp x1, ubyte @zp y1, uword @zp x2, ubyte @zp y2) {
; Bresenham algorithm. ; Bresenham algorithm.
; This code special-cases various quadrant loops to allow simple ++ and -- operations. ; This code special-cases various quadrant loops to allow simple ++ and -- operations.
; TODO there are some slight errors at the first/last pixels in certain slopes...??
if y1>y2 { if y1>y2 {
; make sure dy is always positive to have only 4 instead of 8 special cases ; make sure dy is always positive to have only 4 instead of 8 special cases
swap(x1, x2) swap(x1, x2)
@ -62,8 +61,8 @@ graphics {
dx = -dx dx = -dx
positive_ix = false positive_ix = false
} }
dx *= 2 word @zp dx2 = dx*2
dy *= 2 word @zp dy2 = dy*2
internal_plotx = x1 internal_plotx = x1
if dx >= dy { if dx >= dy {
@ -73,10 +72,10 @@ graphics {
if internal_plotx==x2 if internal_plotx==x2
return return
internal_plotx++ internal_plotx++
d += dy d += dy2
if d > dx { if d > dx {
y1++ y1++
d -= dx d -= dx2
} }
} }
} else { } else {
@ -85,10 +84,10 @@ graphics {
if internal_plotx==x2 if internal_plotx==x2
return return
internal_plotx-- internal_plotx--
d += dy d += dy2
if d > dx { if d > dx {
y1++ y1++
d -= dx d -= dx2
} }
} }
} }
@ -100,10 +99,10 @@ graphics {
if y1 == y2 if y1 == y2
return return
y1++ y1++
d += dx d += dx2
if d > dy { if d > dy {
internal_plotx++ internal_plotx++
d -= dy d -= dy2
} }
} }
} else { } else {
@ -112,10 +111,10 @@ graphics {
if y1 == y2 if y1 == y2
return return
y1++ y1++
d += dx d += dx2
if d > dy { if d > dy {
internal_plotx-- internal_plotx--
d -= dy d -= dy2
} }
} }
} }

View File

@ -1,39 +1,32 @@
%import textio %import graphics
%import floats %import floats
%import test_stack
%zeropage dontuse
main { main {
sub start() { sub start() {
graphics.enable_bitmap_mode()
uword xx uword xx
float fl ubyte yy
float fltotal=0.0
ubyte ub = 22
ub = 22 graphics.line(150,50,150,50)
fl = ub as float
floats.print_f(fl)
txt.nl()
ub = 255
fl = ub as float
floats.print_f(fl)
txt.nl()
xx = 123
fl = xx as float
floats.print_f(fl)
txt.nl()
xx = 55555
fl = xx as float
floats.print_f(fl)
txt.nl()
fltotal=0.0 for yy in 0 to 199-60 step 16 {
for xx in 1 to 255 {
fl = xx as float for xx in 0 to 319-50 step 16 {
fltotal = fl * fl graphics.line(30+xx, 10+yy, 50+xx, 30+yy)
graphics.line(49+xx, 30+yy, 10+xx, 30+yy)
graphics.line(11+xx, 29+yy, 29+xx, 11+yy)
; triangle 2, counter-clockwise
graphics.line(30+xx, 40+yy, 10+xx, 60+yy)
graphics.line(11+xx, 60+yy, 50+xx, 60+yy)
graphics.line(49+xx, 59+yy, 31+xx,41+yy)
}
} }
test_stack.test() repeat {
}
} }
} }