Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1003203bb1 | |||
| 5ca69d90e6 | |||
| 85b71a91a3 | |||
| 70a8567646 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
TEST.png
|
||||
52
examples/glaggle/main.go
Normal file
52
examples/glaggle/main.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "math"
|
||||
import "image"
|
||||
import "image/png"
|
||||
import "git.tebibyte.media/tomo/ggfx"
|
||||
|
||||
func main () {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 300, 300))
|
||||
canvas := ggfx.Image[uint8] {
|
||||
Pix: img.Pix,
|
||||
Stride: img.Stride,
|
||||
Bounds: img.Rect,
|
||||
Width: 4,
|
||||
}
|
||||
black := []uint8 { 0, 0, 0, 255 }
|
||||
yellow := []uint8 { 255, 255, 0, 255 }
|
||||
midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2)
|
||||
|
||||
face := circle(32, 100, midpoint)
|
||||
canvas.FillPolygon(yellow, face...)
|
||||
canvas.StrokePolygon(black, 2, face...)
|
||||
|
||||
eye := circle(12, 16, midpoint.Add(image.Pt(-40, -30)))
|
||||
canvas.FillPolygon(black, eye...)
|
||||
eye = circle(12, 16, midpoint.Add(image.Pt(40, -30)))
|
||||
canvas.FillPolygon(black, eye...)
|
||||
|
||||
mouth := circle(16, 70, midpoint)
|
||||
canvas.StrokePolygon(black, 2, mouth[1:8]...)
|
||||
|
||||
file, err := os.Create("TEST.png")
|
||||
if err != nil { panic(err.Error()) }
|
||||
defer file.Close()
|
||||
err = png.Encode(file, img)
|
||||
if err != nil { panic(err.Error()) }
|
||||
|
||||
println("Open TEST.png to see the result.")
|
||||
}
|
||||
|
||||
func circle (resolution int, radius float64, center image.Point) []image.Point {
|
||||
points := make([]image.Point, resolution)
|
||||
for index := range points {
|
||||
angle := float64(index) * ((2 * math.Pi) / float64(len(points)))
|
||||
point := image.Pt (
|
||||
int(math.Cos(angle) * radius),
|
||||
int(math.Sin(angle) * radius))
|
||||
points[index] = point.Add(center)
|
||||
}
|
||||
return points
|
||||
}
|
||||
2
plot.go
2
plot.go
@@ -23,7 +23,7 @@ func (context plottingContext[T]) plot (center image.Point) {
|
||||
square := context.square(center)
|
||||
for y := square.Min.Y; y < square.Max.Y; y ++ {
|
||||
for x := square.Min.X; x < square.Max.X; x ++ {
|
||||
index := (y * context.stride + x) * context.width + context.offset
|
||||
index := y * context.stride + x * context.width + context.offset
|
||||
context.data[index] = context.color
|
||||
}}
|
||||
}
|
||||
|
||||
36
polygon.go
36
polygon.go
@@ -14,10 +14,11 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
for _, point := range points[1:] {
|
||||
if point.X < area.Min.X { area.Min.X = point.X }
|
||||
if point.Y < area.Min.Y { area.Min.Y = point.Y }
|
||||
if point.X < area.Max.X { area.Max.X = point.X }
|
||||
if point.Y < area.Max.Y { area.Max.Y = point.Y }
|
||||
if point.X > area.Max.X { area.Max.X = point.X }
|
||||
if point.Y > area.Max.Y { area.Max.Y = point.Y }
|
||||
}
|
||||
area = this.Bounds.Intersect(area)
|
||||
if area.Empty() { return }
|
||||
|
||||
// this algorithm is adapted from:
|
||||
// https://www.alienryderflex.com/polygon_fill/
|
||||
@@ -28,30 +29,35 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
boundaryCount := 0
|
||||
prevPoint := points[len(points) - 1]
|
||||
for _, point := range points {
|
||||
fy := float64(y)
|
||||
fPointX := float64(point.X)
|
||||
fPointY := float64(point.Y)
|
||||
fPrevX := float64(prevPoint.X)
|
||||
fPrevY := float64(prevPoint.Y)
|
||||
addboundary :=
|
||||
point.Y < y && prevPoint.Y >= y ||
|
||||
prevPoint.Y < y && point.Y >= y
|
||||
(fPointY < fy && fPrevY >= fy) ||
|
||||
(fPrevY < fy && fPointY >= fy)
|
||||
if addboundary {
|
||||
boundaries[boundaryCount] =
|
||||
point.X +
|
||||
(y - point.Y) /
|
||||
(prevPoint.Y - point.Y) *
|
||||
(prevPoint.X - point.X)
|
||||
boundaries[boundaryCount] = int (
|
||||
fPointX +
|
||||
(fy - fPointY) /
|
||||
(fPrevY - fPointY) *
|
||||
(fPrevX - fPointX))
|
||||
boundaryCount ++
|
||||
}
|
||||
prevPoint = point
|
||||
}
|
||||
|
||||
// sort boundary list
|
||||
boundaries = boundaries[:boundaryCount]
|
||||
sort.Ints(boundaries)
|
||||
cutBoundaries := boundaries[:boundaryCount]
|
||||
sort.Ints(cutBoundaries)
|
||||
|
||||
// fill pixels between boundary pairs
|
||||
min := area.Min.X
|
||||
max := area.Max.X
|
||||
for index := 0; index < len(boundaries); index ++ {
|
||||
left := boundaries[index]
|
||||
right := boundaries[index + 1]
|
||||
for index := 0; index < len(cutBoundaries); index += 2 {
|
||||
left := cutBoundaries[index]
|
||||
right := cutBoundaries[index + 1]
|
||||
|
||||
// stop if we have exited the polygon
|
||||
if left >= max { break }
|
||||
@@ -65,7 +71,7 @@ func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
|
||||
for offset, part := range fill {
|
||||
for x := left; x < right; x ++ {
|
||||
index :=
|
||||
(y * this.Stride + x) *
|
||||
y * this.Stride + x *
|
||||
this.Width + offset
|
||||
this.Pix[index] = part
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func (this Image[T]) FillRectangle (fill []T, rectangle image.Rectangle) {
|
||||
for pos.Y = rectangle.Min.Y; pos.Y < rectangle.Max.Y; pos.Y ++ {
|
||||
for offset, part := range fill {
|
||||
for pos.X = rectangle.Min.X; pos.X < rectangle.Max.X; pos.X ++ {
|
||||
index := (pos.Y * this.Stride + pos.X) * this.Width + offset
|
||||
index := pos.Y * this.Stride + pos.X * this.Width + offset
|
||||
this.Pix[index] = part
|
||||
}}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user