c# - 如何将点移动给定距离d(并获得新坐标)

c# - how to move point a given distance d (and get a new coordinates)(c# - 如何将点移动给定距离d(并获得新坐标))

本文介绍了c# - 如何将点移动给定距离d(并获得新坐标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想知道是否有任何有效的方法来计算点的坐标(从原始位置移动距离 d).

Hi I was wondering if there is any efficent way to calculating coordinates of point (which was moved distance d from it's original location).

假设我有一个点 P(0.3,0.5),我需要以距离 d 随机移动该点.

Let's say I have a point P(0.3,0.5) and I need to move that point random direction with distance d.

到目前为止,我通过随机选择新的 x 和 y 坐标来做到这一点,并且我正在检查新旧点之间的距离是否等于 d.我确实意识到这样做不是太有效的方法.你会怎么做??

So far I did it by random picking new x and y coordinates and I was checking if distance between old and new point equals d. I do realize that is't too eficient way to do that. How would You do it ??

推荐答案

给定一个点(x1, y1),我们想找一个随机"点(x2, y2) 距离它 d.

Given a point (x1, y1), we want to find a "random" point (x2, y2) at a distance d from it.

选择一个随机角度theta.那么:

Pick a random angle theta. Then:

x2 = x1 + d * cos(theta)
y2 = y1 + d * sin(theta)

这将是半径为 d 的圆上的一个随机点,该圆以 (x1, y1)

This will be a random point on a circle of radius d centered at (x1, y1)

证明:

Distance between (x1, y1) and (x2, y2)
= sqrt ( (x2 - x1) ^ 2 + (y2 - y1) ^ 2)
= sqrt ( d^2 * (sin^2 (theta) + cos^2 (theta) ) )
= d

你可能想看看:

  • 极坐标系
  • 距离公式
  • 毕达哥拉斯三角恒等式

这篇关于c# - 如何将点移动给定距离d(并获得新坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:c# - 如何将点移动给定距离d(并获得新坐标)

基础教程推荐