Cleanup sweeping algo

This commit is contained in:
Carlos Sanchez 2024-07-26 20:10:18 -04:00
parent 7b2e52cedb
commit 37c6ce73a8

View File

@ -49,6 +49,10 @@ func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
}*/
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
// The dude gets rid of "degenerate" triangles so... we do too?
if v2.Y == v1.Y && v1.Y == v0.Y {
return
}
// Very silly manual sorting by Y
if v2.Y < v0.Y {
v0, v2 = v2, v0
@ -59,29 +63,30 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
if v2.Y < v1.Y {
v1, v2 = v2, v1
}
//log.Print(v0, v1, v2)
// Don't worry about speed for now...
var v02step, v01step, v12step, xlong, xshort float32
xlong = float32(v0.X)
xshort = xlong
// The first and last Y CAN'T be equal because sorting!!
if v1.Y == v0.Y {
xshort = float32(v1.X)
}
// We can check just for greater than because we sorted the vertices
// Assume 02 is on the right(?) and 01 on the left
v02step = (float32(v2.X - v0.X)) / (float32(v2.Y-v0.Y) + 0.001) // long side always
v01step = (float32(v1.X - v0.X)) / (float32(v1.Y-v0.Y) + 0.001) // first short side
v12step = (float32(v2.X - v1.X)) / (float32(v2.Y-v1.Y) + 0.001) // second short side
xshort += v01step / 2
xlong += v02step / 2
for y := v0.Y; y <= v2.Y; y++ {
xleft := int(math.Floor(float64(xshort)))
xright := int(math.Floor(float64(xlong)))
xleft := int(xshort)
xright := int(xlong)
if xleft > xright {
xleft, xright = xright, xleft
}
// Draw a horizontal line from elft to right
// Draw a horizontal line from left to right
for x := xleft; x <= xright; x++ {
fb.SetSafe(uint(x), uint(y), color)
}
@ -92,8 +97,4 @@ func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
xshort += v12step
}
}
// line(fb, color, v0, v1)
// line(fb, color, v1, v2)
// line(fb, color, v2, v0)
}