'Find a point at a specific angle from a center point in Go

I want to be able to use a center point (x,y) and an angle to find a second point (x2,y2) at a given distance from center.

For example:

Center is 0,0

Angle is 50 degrees

Distance is 10

The code below is wrong and trying to interpret this answer https://math.stackexchange.com/questions/143932/calculate-point-given-x-y-angle-and-distance however I have no idea how to incorporate the θ using Go.

Sorry I have not done this kind of math for many years and the question may sound stupid.

x := math.Cos(50)
y := math.Sin(50)

x2 := x *10
y2 := x *10

How do I find x2, y2 using Golang? Any help would be appreciated.



Solution 1:[1]

The Go math functions use radians. You may need to convert from degrees to radians first. See the example below:

package main

import (
    "fmt"
    "math"
)

func main() {
    x0 := float64(0)
    y0 := float64(0)

    // Assume the positive X axis represents 0 degrees (anti-clockwise direction).
    fmt.Printf("(x0,y0)\tDegrees\tRadians\t(x1,y1)\n")
    for _, degrees := range []float64{0, 30, 45, 90, 135, 180, 225, 270, 315} {
        radians := degrees * math.Pi / 180
        distance := float64(1)
        x1 := x0 + distance*math.Cos(radians)
        y1 := y0 + distance*math.Sin(radians)
        fmt.Printf("(%g, %g)\t%g\t%.3f\t(%.3f, %.3f)\n", x0, y0, degrees, radians, x1, y1)
    }
}

Output:

(x0,y0) Degrees Radians (x1,y1)
(0, 0)  0       0.000   (1.000, 0.000)
(0, 0)  30      0.524   (0.866, 0.500)
(0, 0)  45      0.785   (0.707, 0.707)
(0, 0)  90      1.571   (0.000, 1.000)
(0, 0)  135     2.356   (-0.707, 0.707)
(0, 0)  180     3.142   (-1.000, 0.000)
(0, 0)  225     3.927   (-0.707, -0.707)
(0, 0)  270     4.712   (-0.000, -1.000)
(0, 0)  315     5.498   (0.707, -0.707)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1