在C#中从角度计算圆周上的点?

Calculating point on a circle#39;s circumference from angle in C#?(在C#中从角度计算圆周上的点?)

本文介绍了在C#中从角度计算圆周上的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这是一个简单的问题,但是我当前的代码得到了一些奇怪的结果,而且我没有数学背景来完全理解原因.正如标题中所述,我的目标很简单:我只想找到与中心点有一定距离和角度的点.

I imagine that this is a simple question, but I'm getting some strange results with my current code and I don't have the math background to fully understand why. My goal is simple, as stated in the title: I just want to find the point at some distance and angle from a center point.

我当前的代码:

Point centerPoint = new Point ( 0, 0 );
Point result      = new Point ( 0, 0 );
double angle      = 0.5; //between 0 and 2 * PI, angle is in radians
int distance      = 1000;

result.Y = centerPoint.Y + (int)Math.Round( distance * Math.Sin( angle ) );
result.X = centerPoint.X + (int)Math.Round( distance * Math.Cos( angle ) );

一般来说,这似乎工作得相当合理,但我在不同的地方遇到了问题,最明显的是当角度对应于负 x 和 y 轴上的点时.很明显我做错了什么——想想那是什么?

In general, this seems to work fairly reasonably, but I get problems at various spots, most notably when the angle corresponds to points in the negative x and y axis. Clearly I'm doing something wrong -- thoughts on what that is?

更新:这是我的错误,这段代码工作正常——少数不工作的异常值实际上是由于计算 1.5PI 的角度存在错误.我以为我已经检查得足够好,但显然没有.感谢大家的宝贵时间,希望上面的工作代码对其他人有帮助.

UPDATE: This was my mistake, this code works fine -- the few outliers that were not working were actually due to a bug in how the angle for 1.5PI was being calculated. I thought I had checked that well enough, but evidently had not. Thanks to everyone for their time, hopefully the working code above will prove helpful to someone else.

推荐答案

你忘了添加中心点:

result.Y = (int)Math.Round( centerPoint.Y + distance * Math.Sin( angle ) );
result.X = (int)Math.Round( centerPoint.X + distance * Math.Cos( angle ) );

其余的应该没问题...(你得到了什么奇怪的结果?你能给出准确的输入吗?)

The rest should be ok... (what strange results were you getting? Can you give an exact input?)

这篇关于在C#中从角度计算圆周上的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在C#中从角度计算圆周上的点?

基础教程推荐