从C#中的角度计算圆圆周上的点? [英] Calculating point on a circle's circumference from angle in C#?

查看:38
本文介绍了从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#中的角度计算圆圆周上的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆