Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "image"
- "image/color"
- "image/draw"
- "image/png"
- "log"
- "math"
- "os"
- )
- func main() {
- s := newState(6, 150, 42)
- s.Set(color.RGBA{0, 255, 255, 255})
- savePNG(s.image, "output.png")
- }
- func newState(margin, radius, points int) *state {
- s := &state{
- theta: (math.Pi * 2) / float64(points),
- image: uniformImage((margin+radius)*2, (margin+radius)*2, color.Black),
- offset: image.Point{X: margin + radius, Y: margin + radius},
- }
- for n := 0; n < points; n++ {
- s.AddPoint(float64(radius), n)
- }
- return s
- }
- type state struct {
- theta float64
- image *image.RGBA
- offset image.Point
- points []image.Point
- }
- func (s *state) AddPoint(r float64, n int) {
- angle := s.theta * float64(n)
- s.add(image.Point{
- X: s.offset.X + int(r*math.Cos(angle)),
- Y: s.offset.Y + int(r*math.Sin(angle)),
- })
- }
- func (s *state) add(p image.Point) {
- s.points = append(s.points, p)
- }
- func (s *state) Set(c color.Color) {
- for _, p := range s.points {
- s.image.Set(p.X, p.Y, c)
- s.image.Set(p.X+1, p.Y, c)
- s.image.Set(p.X+1, p.Y+1, c)
- }
- }
- func uniformImage(w, h int, c color.Color) *image.RGBA {
- m := image.NewRGBA(image.Rect(0, 0, w, h))
- draw.Draw(m, m.Bounds(), &image.Uniform{c}, image.ZP, draw.Src)
- return m
- }
- func savePNG(m image.Image, fn string) {
- if f, err := os.Create(fn); err != nil {
- log.Printf("unable to create %s: %v", fn, err)
- } else {
- defer f.Close()
- enc := png.Encoder{
- CompressionLevel: png.BestCompression,
- }
- enc.Encode(f, m)
- }
- }
Add Comment
Please, Sign In to add comment