c# - how to move point a given distance d (and get a new coordinates)(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(并获得新坐标)
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
