gg/examples/star.go

41 lines
719 B
Go
Raw Normal View History

2016-02-19 03:30:24 +01:00
package main
import (
"math"
2016-02-19 15:43:37 +01:00
"github.com/fogleman/gg"
2016-02-19 03:30:24 +01:00
)
type Point struct {
X, Y float64
}
func Polygon(n int, x, y, r float64) []Point {
result := make([]Point, n)
for i := 0; i < n; i++ {
a := float64(i)*2*math.Pi/float64(n) - math.Pi/2
result[i] = Point{x + r*math.Cos(a), y + r*math.Sin(a)}
}
return result
}
func main() {
2016-02-19 03:34:57 +01:00
n := 5
points := Polygon(n, 512, 512, 400)
2016-02-19 15:43:37 +01:00
dc := gg.NewContext(1024, 1024)
2016-02-19 17:07:25 +01:00
dc.SetHexColor("fff")
dc.Clear()
2016-02-19 03:34:57 +01:00
for i := 0; i < n+1; i++ {
index := (i * 2) % n
2016-02-19 03:30:24 +01:00
p := points[index]
dc.LineTo(p.X, p.Y)
}
2016-02-19 17:07:25 +01:00
dc.SetRGBA(0, 0.5, 0, 1)
2016-02-19 15:43:37 +01:00
dc.SetFillRule(gg.FillRuleEvenOdd)
2016-02-19 03:30:24 +01:00
dc.FillPreserve()
2016-02-19 17:07:25 +01:00
dc.SetRGBA(0, 1, 0, 0.5)
2016-02-19 03:34:57 +01:00
dc.SetLineWidth(16)
2016-02-19 03:30:24 +01:00
dc.Stroke()
2016-02-19 19:54:55 +01:00
dc.SavePNG("out.png")
2016-02-19 03:30:24 +01:00
}