prog8/examples/lines-circles.p8

90 lines
2.6 KiB
Plaintext
Raw Normal View History

2020-03-14 16:11:10 +00:00
%import c64lib
%import c64utils
%zeropage basicsafe
main {
sub start() {
c64scr.print("mid-point\ncircle\n and\nbresenham\nline\nalgorithms.\n")
const ubyte xcenter = 20
const ubyte ycenter = 12
ubyte r
for r in 3 to 12 step 3 {
circle(r)
}
2020-03-15 00:14:44 +00:00
;line(5,3, 30,3)
;line(5,3, 5, 24)
;line(30,3,30,24)
;line(5,24,30,24)
2020-03-14 16:11:10 +00:00
line(1, 10, 38, 24)
line(1, 20, 38, 2)
line(20, 4, 10, 24)
line(39, 16, 12, 0)
sub line(ubyte x1, ubyte y1, ubyte x2, ubyte y2) {
byte d = 0
ubyte dx = abs(x2 - x1)
ubyte dy = abs(y2 - y1)
ubyte dx2 = 2 * dx
ubyte dy2 = 2 * dy
byte ix = sgn(x2 as byte - x1 as byte)
byte iy = sgn(y2 as byte - y1 as byte)
2020-03-14 16:11:10 +00:00
ubyte x = x1
ubyte y = y1
if dx >= dy {
2020-03-14 17:11:24 +00:00
forever {
2020-03-14 16:11:10 +00:00
c64scr.setcc(x, y, 42, 5)
if x==x2
return
2020-03-15 00:14:44 +00:00
x += ix as ubyte
2020-03-14 16:11:10 +00:00
d += dy2
if d > dx {
2020-03-15 00:14:44 +00:00
y += iy as ubyte
2020-03-14 16:11:10 +00:00
d -= dx2
}
}
} else {
2020-03-14 17:11:24 +00:00
forever {
2020-03-14 16:11:10 +00:00
c64scr.setcc(x, y, 42, 5)
if y == y2
return
2020-03-15 00:14:44 +00:00
y += iy as ubyte
2020-03-14 16:11:10 +00:00
d += dx2
if d > dy {
2020-03-15 00:14:44 +00:00
x += ix as ubyte
2020-03-14 16:11:10 +00:00
d -= dy2
}
}
}
}
sub circle(ubyte radius) {
byte x = radius as byte
byte y = 0
byte decisionOver2 = 1-x
while x>=y {
c64scr.setcc(xcenter + x as ubyte, ycenter + y as ubyte, 81, 1)
c64scr.setcc(xcenter - x as ubyte, ycenter + y as ubyte, 81, 1)
c64scr.setcc(xcenter + x as ubyte, ycenter - y as ubyte, 81, 1)
c64scr.setcc(xcenter - x as ubyte, ycenter - y as ubyte, 81, 1)
c64scr.setcc(xcenter + y as ubyte, ycenter + x as ubyte, 81, 1)
c64scr.setcc(xcenter - y as ubyte, ycenter + x as ubyte, 81, 1)
c64scr.setcc(xcenter + y as ubyte, ycenter - x as ubyte, 81, 1)
c64scr.setcc(xcenter - y as ubyte, ycenter - x as ubyte, 81, 1)
y++
if decisionOver2<=0
decisionOver2 += 2*y+1
else {
x--
decisionOver2 += 2*(y-x)+1
}
}
}
}
}