c# - 如何将点移动给定距离 d(并获得新坐标) [英] c# - how to move point a given distance d (and get a new coordinates)
问题描述
嗨我想知道是否有任何有效的方法来计算点的坐标(从它的原始位置移动了距离 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)
这将是一个以 (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(并获得新坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!