Fix line issues

This commit is contained in:
Carlos Sanchez 2024-07-23 20:48:49 -04:00
parent 4b09b01617
commit 9a4ba6be67
3 changed files with 51 additions and 4 deletions

View File

@ -44,9 +44,9 @@ func main() {
// Just draw a simple line (a million times or something)
for range LineRepeat {
LineDumb3(&fb, 0xFFFFFF, 100, 100, 350, 200)
LineDumb3(&fb, 0xFF0000, 120, 100, 200, 350)
LineDumb3(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
LineDumb4(&fb, 0xFFFFFF, 100, 100, 350, 200)
LineDumb4(&fb, 0xFF0000, 120, 100, 200, 350)
LineDumb4(&fb, 0xFF0000, 350, 200, 100, 100) // backward first line
}
log.Printf("Exporting ppm to stdout")

View File

@ -50,3 +50,45 @@ func LineDumb3(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
}
}
}
func LineDumb4(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
steep := false
// This one makes sure that going pixel by pixel will not make holes in the
// other direction by always moving in the less steep direction.
if math.Abs(float64(x1-x0)) < math.Abs(float64(y1-y0)) {
x0, y0 = y0, x0
x1, y1 = y1, x1
steep = true
}
// Don't let lines be invisible, always go left to right
if x0 > x1 { // need to be left to right
x0, x1 = x1, x0
y0, y1 = y1, y0 // Why are we swapping these? I guess because we're swapping the points...
}
dx := x1 - x0 // These MUST be POST calcs
dy := y1 - y0
// Delta error or something? Basically, as we move in one direction, we get farther
// and farther off course if we never move in the OTHER direction. This is the amount
// of that per x direction.
derror := math.Abs(float64(dy) / float64(dx))
y := uint(y0)
var err float64
// Same as dumb2, going across pixel by pixel. But "x" here might actually be y
// because of the inverted direction
for x := x0; x <= x1; x++ {
if steep {
fb.Set(y, uint(x), color)
} else {
fb.Set(uint(x), y, color)
}
err += derror
if err > 0.5 {
if y1 > y0 {
y += 1
} else {
y = y - 1
}
err -= 1
}
}
}

View File

@ -1,6 +1,11 @@
#!/bin/sh
if [ $# -ne 1 ]; then
echo "You must pass the basename for the .prof and .ppm"
exit 1
fi
echo "Building"
go build
echo "Running"
./tinyrender1 "$@" > image.ppm
./tinyrender1 "-cpuprofile=$1.prof" >"$1.ppm"