.proc triangle ;; Subroutine for drawing a filld triangle. ;; It takes in values from A_X, A_Y, B_X, B_Y C_X, C_Y, see triangle.inc .include "triangle.inc" ;; Algorithm ;; (i) For height n it steps through Bresenham's line algorithm to know where the triangle starts and ends at height n. ;; (ii) It draws all pixel in this given range. ;; (iii) It change if the algorithm has hit a corner and need to change line! ;; Implementation ;; In the implementation the triangle split into 2 souch that (iii) can be redused to: check if the triangle has ended. ;; Each side of the sub-triangle can have 4 posiboul states (look at how line is implemented) ;;Lets first sort the edges such as ;; (i) A is at the highest point ;; (ii) B is at the middle point ;; (iii) C is at the lowest point ;; Lets also calculate the height-differense for the new points: ;; * AB for differense betwean A and B ;; * BC for differense betwean B and C ;;AB SEC LDA B_Y SBC A_Y STA AB BCC AB_overflow_end ;; A_Y > B_Y AB_overflow: EOR #$ff ; Fix byte underflow ;; swop A and B LDX B_Y LDY A_Y STX A_Y STY B_Y LDX B_X LDY A_X STX A_X STY B_X AB_overflow_end: ;;BC SEC LDA C_Y SBC B_Y STA BC BCC BC_overflow_end ;; B_Y > C_Y BC_overflow: EOR #$ff ; Fix byte underflow ;; swop A and B LDX C_Y LDY B_Y STX B_Y STY C_Y LDX C_X LDY B_X STX B_X STY C_X ;; If this is the case A and B may be in the wrong order. Lets redo stuff from the start JMP triangle BC_overflow_end: draw_upper_triangle: # This triangle has 10 posibol cases, see table bellow # ---------------------------------------------------- # | name left_side right_side | # ---------------------------------------------------- # | RR right-sloaping-steep right-sloaping-steep| # | rR right-sloaping right-sloaping-steep| # | lR left-sloaping right-sloaping-steep| # | LR left-sloaping-steep right-sloaping-steep| # | rr right-sloaping right-sloaping | # | lr left-sloaping right-sloaping | # | Lr left-sloaping-steep right-sloaping | # | ll left-sloaping left-sloaping | # | Ll left-sloaping-sleep left-sloaping | # | LL left-sloaping-sleep left-sloaping-sleep | # ---------------------------------------------------- RTS .endproc