Skip to content

gg

Repositório oficial: fogleman/gg: Go Graphics - 2D rendering in Go with a simple API. (github.com)

Documentação oficial: gg package - github.com/fogleman/gg - Go Packages

Exemplos oficiais: gg/examples at master · fogleman/gg (github.com)

gg é um mecanismo de renderização gráfica 2D bem estabelecido, adequado para gerar imagens.

Instalação

go get -u github.com/fogleman/gg

Início Rápido

go
package main

import "github.com/fogleman/gg"

func main() {
    dc := gg.NewContext(1000, 1000) // Cria uma tela com altura 1000 e largura 1000
    dc.DrawCircle(500, 500, 400) // Desenha um círculo com raio 400 na coordenada (500,500)
    dc.SetRGB(0, 0, 0) // Define a cor preta
    dc.Fill() // Preenche
    dc.SavePNG("out.png") // Salva em arquivo de imagem
}

Pontos

![](/images/gg/points.png =400x400)

go
func TestDot(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   for i := 1; i < 10; i++ {
      dc.DrawPoint(float64(50*i), float64(50*i), 5) // Define coordenada e raio do ponto
   }
   dc.Fill() // Preenche
   dc.SavePNG("out.png")
}

Linhas

![](/images/gg/lines.png =400x400)

go
func TestLines(t *testing.T) {
   dc := gg.NewContext(1000, 1000)
   dc.SetRGB(0, 0, 0)
   dc.SetLineWidth(5) // Define largura da linha
   dc.DrawLine(1000, 0, 0, 1000)
   dc.DrawLine(1000, 1000, 0, 0)
   dc.Stroke() // Conecta linhas
   dc.SavePNG("lines.png")
}

Golang por www.golangdev.cn edit