Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dd0655ea3c | |||
| 41720caef5 | |||
| cc921ddb29 | |||
| c8b022f5f1 |
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "math"
|
||||
import "image"
|
||||
import "image/png"
|
||||
import "git.tebibyte.media/tomo/ggfx"
|
||||
@@ -18,17 +17,17 @@ func main () {
|
||||
yellow := []uint8 { 255, 255, 0, 255 }
|
||||
midpoint := image.Pt(canvas.Bounds.Dx() / 2, canvas.Bounds.Dy() / 2)
|
||||
|
||||
face := circle(32, 100, midpoint)
|
||||
face := ggfx.Circle(32, 100, midpoint)
|
||||
canvas.FillPolygon(yellow, face...)
|
||||
canvas.StrokePolygon(black, 2, face...)
|
||||
|
||||
eye := circle(12, 16, midpoint.Add(image.Pt(-40, -30)))
|
||||
eye := ggfx.Circle(12, 16, midpoint.Add(image.Pt(-40, -30)))
|
||||
canvas.FillPolygon(black, eye...)
|
||||
eye = circle(12, 16, midpoint.Add(image.Pt(40, -30)))
|
||||
eye = ggfx.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]...)
|
||||
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()) }
|
||||
@@ -38,15 +37,3 @@ func main () {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
40
examples/icon/main.go
Normal file
40
examples/icon/main.go
Normal 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.")
|
||||
}
|
||||
11
polygon.go
11
polygon.go
@@ -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
|
||||
@@ -88,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
|
||||
}
|
||||
}
|
||||
|
||||
16
shapes.go
Normal file
16
shapes.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user