;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*- ;; Subroutine for drawing a filld triangle. .proc 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 left 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 BCC AB_overflow_end STA AB ;; A_Y > B_Y AB_overflow: EOR #$ff ; Fix byte underflow STA AB ;; 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 BCC BC_overflow_end STA BC ;; B_Y > C_Y BC_overflow: EOR #$ff ; Fix byte underflow STA BC ;; 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: .include "upper_triangle.s" draw_lower_triangle: ;;This has the same cases as upper triangle .include "lower_triangle.s" RTS .endproc