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

26

本文介绍了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(并获得新坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14