Introduction Lines Circles Sharp Waves Curly Waves Colors Lines Between Two Points Arcs Between Two Points

Circles - Maths317

To draw a circle knowing the radius (r) and the center of the circle at point (x0,y0). The circle formulas are:

x1:=x0+(r×sinθ)
y1:=y0+(r×cosθ)

Loop through 360 and set the radius to the loops index, and use the formula above to get all the points of the circle.

Test Code


package main

import (
  "github.com/disintegration/imaging"
  "image/color"
  "math"
)

func main() {
  width := 500
  height := 300
  img := imaging.New(width, height, color.White)

  radius := 50

  for angle := 0; angle <= 360; angle++ {
    angleInRadians := float64(angle) * (math.Pi / 180)
    x := float64(radius) * math.Sin(angleInRadians)
    y := float64(radius) * math.Cos(angleInRadians)

    img.Set(width/2 + int(x), height/2 + int(y), color.Black)
  }

  err := imaging.Save(img, "out_circles1.png")
  if err != nil {
    panic(err)
  }
}

< Previous Next >