circle.z80 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. circle:
  2. ;Input:
  3. ; (B,C) is the center (x,y)
  4. ; E is the radius, unsigned, less than 128 (0 or greater than 128 just quits).
  5. ; IX points to a `plot` routine that takes (B,C)=(x,y) as input.
  6. ld a,e
  7. add a,a
  8. ret c
  9. ret z
  10. ld l,e
  11. dec a
  12. ld e,a
  13. dec a ;if the pixel is only 1 wide, just plot the point
  14. jp z,call_ix ;Jump to the plot routine
  15. xor a
  16. ld h,-1
  17. ld d,1
  18. scf ;skip the first plot
  19. circleloop:
  20. call nc,plot4
  21. inc h
  22. sub d
  23. inc d
  24. inc d
  25. jr nc,circleloop
  26. _:
  27. dec l
  28. call plot4
  29. add a,e
  30. dec e
  31. ret z
  32. dec e
  33. jr nc,-_
  34. jp circleloop
  35. plot4:
  36. ;BC is center
  37. ;HL is x,y
  38. push de
  39. push af
  40. ld a,(circle_pattern)
  41. rlca
  42. ld (circle_pattern),a
  43. jr c,plot4_end
  44. push hl
  45. push bc
  46. ;If H is 0, or L is 0, we need to draw only half
  47. push hl
  48. ld a,b
  49. sub h
  50. ld b,a
  51. add a,h
  52. add a,h
  53. ld h,a
  54. ld a,c
  55. sub l
  56. ld c,a
  57. add a,l
  58. add a,l
  59. ld l,a
  60. ;B is x0-x
  61. ;C is y0-y
  62. ;H is x0+x
  63. ;L is y0+y
  64. ;plot(x0-x,y0-y)
  65. ;plot(x0+x,y0+y)
  66. push bc
  67. push hl
  68. call call_ix ;call the plot routine
  69. pop bc
  70. push bc
  71. call call_ix ;call the plot routine
  72. ;now swap the y coords
  73. pop hl
  74. pop bc
  75. ld a,l
  76. ld l,c
  77. ld c,a
  78. pop de
  79. xor a
  80. cp d
  81. jr z,+_
  82. cp e
  83. jr z,+_
  84. ;plot(x0-x,y0+y)
  85. ;plot(x0+x,y0-y)
  86. push hl
  87. call call_ix ;call the plot routine
  88. pop bc
  89. call call_ix ;call the plot routine
  90. _:
  91. pop bc
  92. pop hl
  93. plot4_end:
  94. pop af
  95. pop de
  96. ret