2024-07-26 21:32:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Bresenham2(fb *Framebuffer, color uint, x0 int, y0 int, x1 int, y1 int) {
|
|
|
|
dx := int(math.Abs(float64(x1 - x0)))
|
|
|
|
sx := -1
|
|
|
|
if x0 < x1 {
|
|
|
|
sx = 1
|
|
|
|
}
|
|
|
|
dy := -int(math.Abs(float64(y1 - y0)))
|
|
|
|
sy := -1
|
|
|
|
if y0 < y1 {
|
|
|
|
sy = 1
|
|
|
|
}
|
|
|
|
err := dx + dy
|
|
|
|
|
|
|
|
for {
|
|
|
|
fb.SetSafe(uint(x0), uint(y0), color)
|
|
|
|
if x0 == x1 && y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
e2 := 2 * err
|
|
|
|
if e2 >= dy {
|
|
|
|
if x0 == x1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err += dy
|
|
|
|
x0 += sx
|
|
|
|
}
|
|
|
|
if e2 <= dx {
|
|
|
|
if y0 == y1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err += dx
|
|
|
|
y0 += sy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 21:48:47 +00:00
|
|
|
func line(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i) {
|
|
|
|
Bresenham2(fb, color, v0.X, v0.Y, v1.X, v1.Y)
|
|
|
|
}
|
|
|
|
|
2024-07-26 23:37:25 +00:00
|
|
|
/*func LineSweep(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2024-07-26 21:48:47 +00:00
|
|
|
func Triangle1(fb *Framebuffer, color uint, v0 Vec2i, v1 Vec2i, v2 Vec2i) {
|
2024-07-26 23:37:25 +00:00
|
|
|
// Very silly manual sorting by Y
|
|
|
|
if v2.Y < v0.Y {
|
|
|
|
v0, v2 = v2, v0
|
|
|
|
}
|
|
|
|
if v1.Y < v0.Y {
|
|
|
|
v0, v1 = v1, v0
|
|
|
|
}
|
|
|
|
if v2.Y < v1.Y {
|
|
|
|
v1, v2 = v2, v1
|
|
|
|
}
|
2024-07-26 23:57:36 +00:00
|
|
|
//log.Print(v0, v1, v2)
|
2024-07-26 23:37:25 +00:00
|
|
|
|
|
|
|
// Don't worry about speed for now...
|
|
|
|
var v02step, v01step, v12step, xlong, xshort float32
|
|
|
|
|
2024-07-26 23:57:36 +00:00
|
|
|
xlong = float32(v0.X)
|
2024-07-26 23:37:25 +00:00
|
|
|
xshort = xlong
|
|
|
|
|
|
|
|
// We can check just for greater than because we sorted the vertices
|
2024-07-26 23:57:36 +00:00
|
|
|
// 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
|
2024-07-26 23:37:25 +00:00
|
|
|
|
|
|
|
for y := v0.Y; y <= v2.Y; y++ {
|
2024-07-26 23:45:12 +00:00
|
|
|
xleft := int(math.Floor(float64(xshort)))
|
|
|
|
xright := int(math.Floor(float64(xlong)))
|
|
|
|
if xleft > xright {
|
|
|
|
xleft, xright = xright, xleft
|
|
|
|
}
|
|
|
|
// Draw a horizontal line from elft to right
|
|
|
|
for x := xleft; x <= xright; x++ {
|
2024-07-26 23:37:25 +00:00
|
|
|
fb.SetSafe(uint(x), uint(y), color)
|
|
|
|
}
|
|
|
|
xlong += v02step
|
|
|
|
if y < v1.Y {
|
|
|
|
xshort += v01step
|
|
|
|
} else {
|
|
|
|
xshort += v12step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// line(fb, color, v0, v1)
|
|
|
|
// line(fb, color, v1, v2)
|
|
|
|
// line(fb, color, v2, v0)
|
2024-07-26 21:32:20 +00:00
|
|
|
}
|