Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Last active October 23, 2022 18:02
Show Gist options
  • Save wuriyanto48/ff4318a955fbe7e7ea28414181c0133e to your computer and use it in GitHub Desktop.
Save wuriyanto48/ff4318a955fbe7e7ea28414181c0133e to your computer and use it in GitHub Desktop.
Draw Circle to Console (implementation with Golang)
package main
import (
"bytes"
"fmt"
"math"
"strings"
)
// α(radians) = α(degrees) × π / 180°
func DegToRad(deg float64) float64 {
return deg * (math.Pi / 180)
}
// α(degrees) = α(radians) × 180° / π
func RadToDeg(rad float64) float64 {
return rad * (180 / math.Pi)
}
func main() {
fmt.Println(1*(math.Pi/180), " ", math.Cos(2*(math.Pi/180)))
radius := 10
n := 31
circleDot := "*"
canvasDot := "#"
var plane bytes.Buffer
var cartesian [][]string
// draw cartesian plane fill with "#"
for y := 0; y <= n; y++ {
var cartI []string
for x := 0; x <= n; x++ {
cartI = append(cartI, canvasDot)
}
cartesian = append(cartesian, cartI)
}
// draw cirle
var xx float64 = 0
var yy float64 = 0
for t := 0; t <= 360; t++ {
fmt.Println(float64(t), " ", math.Cos(DegToRad(float64(t))), " ", math.Sin(DegToRad(float64(t))))
yy = (float64(n) / 2) + math.Round(float64(radius)*math.Sin(DegToRad(float64(t))))
xx = (float64(n) / 2) + math.Round(float64(radius)*math.Cos(DegToRad(float64(t))))
cartesian[int(math.Abs(yy))][int(math.Abs(xx))] = circleDot
}
for i := 0; i < len(cartesian); i++ {
ss := strings.Join(cartesian[i], "")
plane.WriteString(ss)
plane.WriteString("\n")
}
fmt.Print(plane.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment