将按钮放置在圆上

发布于 2024-11-09 03:12:29 字数 717 浏览 0 评论 0原文

我在 Objective-C 中计算圆上的点很费时间。即使在阅读了大量其他代码示例之后,我的圈子仍然偏离中心。 (这要考虑“中心”与“原点”,并调整 UIView 的大小,在本例中为 UIButton。)

这是我正在使用的代码。圆圈形成正确,只是偏离中心。我不确定这是弧度与度数的问题还是其他问题。这是 ViewController 中的一个辅助函数,它以编程方式创建 UIButton 并将它们添加到视图中:

- (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 / 2) + (radius * cosf(angle));
    newPoint.y = (centerPoint.y / 2) + (radius * sinf(angle));
    return newPoint;   
}

感谢您的帮助!

I'm having a heck of a time calculating points on a circle in Objective-C. Even after reading TONS of other code samples my circle is still way off center. (And that's taking into consideration "center" vs "origin" and adjusting for the size of the UIView, in this case a UIButton.)

Here's the code I'm using. The circle is formed correctly, it's just off center. I'm not sure if this is a radians vs degrees problem or something else. This is a helper function in a ViewController that programmatically creates the UIButtons and adds them to the view:

- (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 / 2) + (radius * cosf(angle));
    newPoint.y = (centerPoint.y / 2) + (radius * sinf(angle));
    return newPoint;   
}

Thanks for the help!

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

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

发布评论

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

评论(2

将军与妓 2024-11-16 03:12:30

按钮的中心(即圆上的点)是

newPoint.x = (centerPoint.x) + (radius * cosf(angle));  // <= removed / 2
newPoint.y = (centerPoint.y) + (radius * sinf(angle));  // <= removed / 2

请注意,如果您将按钮(即矩形)放在这些点上,则必须确保它们的中心位于该点(即减去 buttonWidth/2newPoint.x 的 code> 和来自 newPoint.ybuttonHeight/2 来获取左上角)。

The center of your buttons (i.e. points on the circle) is

newPoint.x = (centerPoint.x) + (radius * cosf(angle));  // <= removed / 2
newPoint.y = (centerPoint.y) + (radius * sinf(angle));  // <= removed / 2

Please note that if you place buttons (i.e. rectangles) on these points you have to make sure that their center lies at this point (i.e. subtract buttonWidth/2 from newPoint.x and buttonHeight/2 from newPoint.y to get the top left corner).

屋顶上的小猫咪 2024-11-16 03:12:30

不知道为什么你第二次除以 2 newPoint 坐标...

这个怎么样:

newPoint.x = centerPoint.x + (radius * cosf(angle));
newPoint.y = centerPoint.y + (radius * sinf(angle));
return newPoint;   

Not sure why you divide by 2 a second time newPoint coordinates...

What about this:

newPoint.x = centerPoint.x + (radius * cosf(angle));
newPoint.y = centerPoint.y + (radius * sinf(angle));
return newPoint;   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文