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

发布于 2024-07-15 02:06:37 字数 671 浏览 7 评论 0原文

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

我当前的代码:

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 轴上的点时。 显然我做错了什么——你想知道那是什么吗?

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离旧人 2024-07-22 02:06:37

你忘记添加中心点:

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

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

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

乙白 2024-07-22 02:06:37

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

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

函数本身运行良好。 仅当您的距离足够小时,四舍五入才会影响您的答案。 除此之外,答案应该很好。

如果您担心的是舍入,请记住,默认情况下,.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);

相反。

此外,在您想要距离 X 和角度 Y 的问题中...我假设您没有将其与点 (X,Y) 联系起来,因为那是完全不同的。

距离公式为:

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

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:

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.

The distance formula is:

double distance = Math.Sqrt((centerPoint.X + result.X)^2 + (centerPoint.Y + result.Y)^2);
电影里的梦 2024-07-22 02:06:37

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

A Swift3 version

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)
}
一曲琵琶半遮面シ 2024-07-22 02:06:37

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

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.

长安忆 2024-07-22 02:06:37
-(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;   
    }
-(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;   
    }
幸福不弃 2024-07-22 02:06:37

我不懂 C#,无论如何,如果您尝试在某处绘制点,您必须考虑 Y 轴从屏幕顶部到底部折痕的事实,因此您的 sin 元素应该是 -sin(...) 而不是 +sin(...)

所以

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

应该变成:

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

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

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 sin element should have be -sin(...) and not +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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文