从GO中的中心点找到一个特定角度的点

发布于 2025-01-21 15:15:33 字数 557 浏览 1 评论 0原文

我希望能够使用中心点(x,y)和一个角度在距中心的给定距离处找到第二点(x2,y2)。

例如:

中心为0,0

角度为50度

距离为10,

下面的代码是错误的,试图解释此答案 https://math.stackexchange.com/questions/143932/calculate-point-point-point-given-given-gin--xy-xy-angle-ang-and-andance 不知道如何使用GO合并θ。

抱歉,我已经有很多年没有做过这种数学了,这个问题听起来可能很愚蠢。

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

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

如何使用Golang找到X2,Y2?任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2025-01-28 15:15:33

GO数学功能使用弧度。您可能需要首先将度转换为 readians 。请参阅下面的示例:

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)
    }
}

输出:

(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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文