从计算的角度上圆的周长点在C#中? [英] Calculating point on a circle's circumference from angle in C#?
问题描述
我想,这是一个简单的问题,但我发现了我目前的code一些奇怪的结果,我没有数学背景,充分理解为什么。我的目标很简单,正如标题说:我只是想找到从中心点一定距离和角度的点
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.
我目前的code:
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?
更新:这是我的错,这code正常工作 - 即不工作实际上是少数异常值由于是如何被计算1.5PI角度的错误。我想我已经检查了还不错,但显然没有。感谢大家的时间,希望工作code以上将证明是有益的给别人。
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屋!