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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/674225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:54:30  来源:igfitidea点击:

Calculating point on a circle's circumference from angle in C#?

c#mathgeometryangle

提问by x4000

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.

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

My current 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 ) );

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?

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

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.

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

采纳答案by MartinStettner

You forgot to add the center point:

您忘记添加中心点:

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?)

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

回答by John Rasch

Firstly, since you're in radians it's probably beneficial to define your angle as such:

首先,由于您使用弧度,因此定义您的角度可能是有益的:

double angle = (Math.PI / 3); // 60 degrees...

The functions themselves are working fine. The rounding will only affect your answer if your distance is sufficiently small enough. Other than that, the answers should come out just fine.

功能本身运行良好。如果您的距离足够小,舍入只会影响您的答案。除此之外,答案应该会很好。

If it's the rounding you're worried about, remember that by default, .NET does banker's rounding, and you may want:

如果您担心的是四舍五入,请记住,默认情况下,.NET 会进行银行家的四舍五入,您可能需要:

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

instead.

反而。

Additionally, in the question you want distance X and angle Y... I assume you're not relating that to the point (X,Y), because that's completely different.

此外,在这个问题你要距离X和角γ......我以为你不与该给点意见(X,Y),因为这是完全不同的。

The distance formula is:

距离公式为:

double distance = Math.Sqrt((centerPoint.X + result.X)^2 + (centerPoint.Y + result.Y)^2);

回答by JonBWalsh

Without more information on the exact errors it's hard to tell what's wrong. The equations look right and should work. Are you sure the angles you are passing in are correct for angles > 90 degrees? The only other thing I could think of would be that you're multiplying distance (an int) by the result of Math.sin (double) but that shouldn't really be an issue.

如果没有关于确切错误的更多信息,就很难判断出了什么问题。方程看起来正确,应该可以工作。您确定您传入的角度对于大于 90 度的角度是正确的吗?我唯一能想到的另一件事是你将距离(一个整数)乘以 Math.sin(双精度)的结果,但这应该不是问题。

回答by Andrea Ambu

I don't know c#, anyway if you are trying to draw the points somewhere you have to consider the fact that the Y axis crease from the top to the bottom of the screen, so your sinelement should have be -sin(...)and not +sin(...)

我不知道 c#,无论如何,如果你试图在某处绘制点,你必须考虑 Y 轴从屏幕顶部到底部折痕的事实,所以你的sin元素应该是-sin(.. .)而不是+sin(...)

so

所以

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

should become:

应该变成:

result.Y = centerPoint.Y - (int)Math.Round( distance * Math.Sin( angle ) );

If you are not trying to draw them I could not imagine what the problem is, can you give some example?

如果你不是想画它们,我无法想象问题是什么,你能举一些例子吗?

回答by Tahir Waseem

-(NSMutableArray *)GetPointsForCircle
    {
       NSMutableArray *Points = [[NSMutableArray alloc] init];
       CGPoint CenterPoint = CGPointMake(160, 230);
       CGPoint Point;
       for (float Angel = 0; Angel <= 360; Angel+= 60)
         {
           Point.x = CenterPoint.x + 100 * cos(Angel);
           Point.y = CenterPoint.y + 100 * sin(Angel);
           [Points addObject:[NSValue valueWithCGPoint:Point]];
         }
       return Points;
    }

    - (CGPoint)pointOnCircle:(int)thisPoint withTotalPointCount:(int)totalPoints
     {
    ? ? CGPoint centerPoint = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
    ? ? float radius = 100.0;
    ? ? float angle = ( 2 * M_PI / (float)totalPoints ) * (float)thisPoint;
    ? ? CGPoint newPoint;
        newPoint.x = (centerPoint.x) + (radius * cosf(angle));
        newPoint.y = (centerPoint.y) + (radius * sinf(angle));
    ? ? return newPoint; ? 
    }

回答by Joe Susnick

A Swift3 version

一个 Swift3 版本

func pointOnCircle(radius: Double, angleInDegrees: Double, origin: CGPoint) -> CGPoint {
    let x = abs(Double(origin.x) + radius * cos(angleInDegrees * (.pi / 180)))
    let y = abs(Double(origin.y) - radius * sin(angleInDegrees * (.pi / 180)))

    return CGPoint(x: x, y: y)
}