Open RobLoach opened 2 years ago
From Chris on discord
package primitives
import "cart/w4"
func Triangle(x1, y1, x2, y2, x3, y3 int) {
colors := *w4.DRAW_COLORS
*w4.DRAW_COLORS = *w4.DRAW_COLORS
if y1 > y2 {
y1, y2 = y2, y1
x1, x2 = x2, x1
}
if y2 > y3 {
y2, y3 = y3, y2
x2, x3 = x3, x2
}
if y2 == y3 {
fillBottomFlatTriangle(x1, y1, x2, y2, x3, y3)
} else if y1 == y2 {
fillTopFlatTriangle(x1, y1, x2, y2, x3, y3)
} else {
x4 := x1 + int((float32(y2)-float32(y1))/(float32(y3)-float32(y1))*(float32(x3)-float32(x1)))
fillBottomFlatTriangle(x1, y1, x2, y2, x4, y2)
fillTopFlatTriangle(x2, y2, x4, y2, x3, y3)
}
*w4.DRAW_COLORS /= 0xF
w4.Line(x1, y1, x2, y2)
w4.Line(x1, y1, x3, y3)
w4.Line(x2, y2, x3, y3)
*w4.DRAW_COLORS = colors
}
func fillBottomFlatTriangle(x1, y1, x2, y2, x3, y3 int) {
invslope1 := (float32(x2) - float32(x1)) / (float32(y2) - float32(y1))
invslope2 := (float32(x3) - float32(x1)) / (float32(y3) - float32(y1))
curx1 := float32(x1)
curx2 := float32(x1)
for scanlineY := y1; scanlineY <= y2; scanlineY++ {
w4.Line(int(curx1), scanlineY, int(curx2), scanlineY)
curx1 += invslope1
curx2 += invslope2
}
}
func fillTopFlatTriangle(x1, y1, x2, y2, x3, y3 int) {
invslope1 := (float32(x3) - float32(x1)) / (float32(y3) - float32(y1))
invslope2 := (float32(x3) - float32(x2)) / (float32(y3) - float32(y2))
curx1 := float32(x3)
curx2 := float32(x3)
for scanlineY := y3; scanlineY > y1; scanlineY-- {
w4.Line(int(curx1), scanlineY, int(curx2), scanlineY)
curx1 -= invslope1
curx2 -= invslope2
}
}
Add a triangle draw