8 Commits

Author SHA1 Message Date
dd0655ea3c Add icon example 2023-07-01 11:20:51 -04:00
41720caef5 Tweaked example 2023-07-01 11:06:54 -04:00
cc921ddb29 Move circle function out of example and into new file 2023-07-01 11:05:20 -04:00
c8b022f5f1 Add a PolyLine function 2023-07-01 11:01:51 -04:00
1003203bb1 Glaggle example draws glaggle as expected 2023-07-01 04:01:03 -04:00
5ca69d90e6 Fixed polygon filling 2023-07-01 03:44:36 -04:00
85b71a91a3 Fix stretched shapes 2023-07-01 03:18:31 -04:00
70a8567646 Add example 2023-07-01 03:16:34 -04:00
7 changed files with 129 additions and 18 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
TEST.png

39
examples/glaggle/main.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import "os"
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 := ggfx.Circle(32, 100, midpoint)
canvas.FillPolygon(yellow, face...)
canvas.StrokePolygon(black, 2, face...)
eye := ggfx.Circle(12, 16, midpoint.Add(image.Pt(-40, -30)))
canvas.FillPolygon(black, eye...)
eye = ggfx.Circle(12, 16, midpoint.Add(image.Pt(40, -30)))
canvas.FillPolygon(black, eye...)
mouth := ggfx.Circle(24, 70, midpoint)
canvas.PolyLine(black, 2, mouth[1:12]...)
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.")
}

40
examples/icon/main.go Normal file
View File

@@ -0,0 +1,40 @@
package main
import "os"
import "image"
import "image/png"
import "git.tebibyte.media/tomo/ggfx"
func main () {
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
canvas := ggfx.Image[uint8] {
Pix: img.Pix,
Stride: img.Stride,
Bounds: img.Rect,
Width: 4,
}
black := []uint8 { 0, 0, 0, 255 }
orange := []uint8 { 255, 48, 0, 255 }
midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2)
canvas.FillRectangle(orange, canvas.Bounds)
for x := 1; x <= 6; x ++ {
point := image.Pt(x, 7 - x)
canvas.FillRectangle(black, image.Rectangle {
Min: point.Mul(32),
Max: point.Add(image.Pt(1, 1)).Mul(32),
})
}
ring := ggfx.Circle(32, 88, midpoint)
canvas.StrokePolygon(orange, 32, ring...)
canvas.StrokePolygon(black, 16, ring...)
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.")
}

View File

@@ -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
}}
}

View File

@@ -5,7 +5,7 @@ import "image"
func (this Image[T]) FillPolygon (fill []T, points ...image.Point) {
this.assertWidth(fill)
if len(points) < 2 { return }
if len(points) < 3 { return }
// figure out the bounds of the polygon so we don't test empty space
var area image.Rectangle
@@ -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
}
@@ -82,3 +88,12 @@ func (this Image[T]) StrokePolygon (stroke []T, weight int, points ...image.Poin
prevPoint = point
}
}
func (this Image[T]) PolyLine (stroke []T, weight int, points ...image.Point) {
if len(points) < 2 { return }
prevPoint := points[0]
for _, point := range points[1:] {
this.Line(stroke, weight, prevPoint, point)
prevPoint = point
}
}

View File

@@ -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
}}}
}

16
shapes.go Normal file
View File

@@ -0,0 +1,16 @@
package ggfx
import "math"
import "image"
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
}