Math to rotate a point around an origin in Go

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

Math to rotate a point around an origin in Go

问题

无法记住学校里要使用的数学知识,但我想使用Go/Golang围绕原点旋转一个点。

下面是一个非常基本的想法,x,y是原点(中心),x2,y2是在圆周上移动的点。我希望x2,y2能够连续围绕x,y旋转360度。

x := 10 // 中心x坐标
y := 10 // 中心y坐标

x2 := 20 // 旋转点x坐标
y2 := 20 // 旋转点y坐标

xchange := ??? // 用什么数学方法计算x的变化
ychange := ??? // 用什么数学方法计算y的变化

希望能得到帮助。

英文:

Cannot remember the math to use from school however I want to rotate a point around an origin using Go/Golang.

Very basic idea below, x,y is the origin (center) and x2,y2 is the point on the circumference of the circle that moves. I want x2,y2 to rotate 360 around x,y continously.

x:= 10 // center x
y:= 10 // center y

x2 := 20 // rotating point x
y2 := 20 // rotating point y

xchange := ??? What math to use to calculate the change in x
ychange := ??? What math to use to calculate the change in y

Any help would be appreciated.

答案1

得分: 2

你可以将以点(x0,y0)为中心,半径为r的圆形轨道参数化为:

x(t) = x0 + r cos(t)
y(t) = y0 + r sin(t)

你可以根据初始的两个点计算出r。然后,通过平滑地增加t来模拟时间的推移。

英文:

You can parametrize a circular orbit around the point (x0, y0) with radius r as

x(t) = x0 + r cos(t)
y(t) = y0 + r sin(t)

You can work out r based on the two points you begin with. From there it’s just a matter of smoothly increasing t to simulate the progress of time.

答案2

得分: 0

关于任意中心点的旋转:

  • 将指定的旋转中心平移到 (0,0) 点
  • 绕 (0,0) 点进行旋转
  • 将 (0,0) 点平移到指定的旋转中心

示例代码:

// 将旋转中心平移到 (0,0) 点
dx := x_in - x_center
dy := y_in - y_center

// 绕 (0,0) 点进行旋转
c := math.Cos(angle)
s := math.Sin(angle)
rotx := dx * c - dy * s
roty := dx * s + dy * c

// 将 (0,0) 点平移到旋转中心
x_out := rotx + x_center
y_out := roty + y_center
英文:

To rotate about an arbitrary center:

  • translate the designated center of rotation to (0,0)
  • rotate about the (0,0)
  • translate (0,0) back to the designated center.

Example code:

// translate center of rotation to (0,0)
dx := x_in - x_center
dy := y_in - y_center

// rotate about (0,0)
c := math.Cos(angle)
s := math.Sin(angle)
rotx := dx * c - dy * s
roty := dx * s + dy * c

// translate (0,0) back to center of rotation
x_out := rotx + x_center
y_out := roty + y_center

huangapple
  • 本文由 发表于 2022年5月30日 02:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72426109.html
匿名

发表评论

匿名网友

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

确定