I'm drawing a square inside a circle of diameter 1, the diagonal of the square is the diameter of the circle. I then split this square into 4 right angled triangles, using cosine law and knowing that the lengths of a and b on the triangle are 0.5, I create 4 triangles whose hypotenuses add together to form the perimeter of the square. Giving us the equation perimeter = number of sides * (a^2 + b^2 -2abcos(360 / number of sides)) By increasing the number of sides on this shape the perimeter gets closer and closer to the perimeter of the circle (3.14).
I've done this in python before, and it worked, but there was a problem with using cosine law on degrees instead of rad in python that messed it up.
package main
import "fmt"
import "math"
func main() {
for n := float64(4) ; n == n; n *= 2 {
fmt.Println(n)
c := math.Pow(0.5 - (0.5 * math.Cos(360 / n)), 0.5)
fmt.Println(c * n)
}
}
The answer should start at about 3, and go up approaching 3.14, but instead the answer goes up to 180 instead. I've checked my math over and over again, but I think it's a problem with the language not what I am doing.