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

huangapple go评论76阅读模式
英文:

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

问题

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

例如:

中心点是 0,0

角度是 50 度

距离是 10

下面的代码是错误的,试图解释这个答案 https://math.stackexchange.com/questions/143932/calculate-point-given-x-y-angle-and-distance,然而我不知道如何在 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.

答案1

得分: 1

Go数学函数使用弧度(radians)。您可能需要先将角度(degrees)转换为弧度(radians)。请参考下面的示例:

package main

import (
	"fmt"
	"math"
)

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

	// 假设正X轴表示0度(逆时针方向)。
	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)

huangapple
  • 本文由 发表于 2022年4月14日 03:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/71862946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定