对象旋转不正确 - iPhone 应用程序

发布于 2024-12-07 19:46:55 字数 1797 浏览 0 评论 0原文

我在旋转物体时遇到了一个典型的问题。说明如下
我已经采取了两个 CGPoint 比如说 point1 和 point2
点1 = (50,50)
点2 = (150, 50)
该点将画一条水平线。

现在我正在画一个矩形,上面有那个点。宽度为 100,高度为 10。角度为 0。
参见屏幕截图
在此处输入图像描述

工作正常
现在我改变一下观点
点1 = (50,50)
点2 = (50,150)
该点将绘制一条垂直线。

对于矩形角度为 90。
此时矩形无法正确绘制
请参阅屏幕截图
在此处输入图像描述

我绘制矩形的代码是:

    CGPoint mid = CGPointMake((point1.x+point2.x)/2, (point1.y+point2.y)/2)
    CGPoint UL = CGPointMake(mid.x + ( Width / 2 ) * cos (A) - ( Height / 2 ) * sin (A) ,  mid.y + ( Height / 2 ) * cos (A)  + ( Width / 2 ) * sin (A));
    CGContextMoveToPoint(context, UL.x,routeView.frame.size.height - UL.y);
    CGPoint UR = CGPointMake(mid.x - ( Width / 2 ) * cos (A) - ( Height / 2 ) * sin (A) ,  mid.y + ( Height / 2 ) * cos (A)  - ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, UR.x,routeView.frame.size.height - UR.y);
    CGPoint BR = CGPointMake(mid.x - ( Width / 2 ) * cos (A) + ( Height / 2 ) * sin (A) ,  mid.y - ( Height / 2 ) * cos (A)  - ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, BR.x,routeView.frame.size.height - BR.y);
    CGPoint BL = CGPointMake(mid.x + ( Width / 2 ) * cos (A) + ( Height / 2 ) * sin (A) ,  mid.y - ( Height / 2 ) * cos (A)  + ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, BL.x,routeView.frame.size.height - BL.y);
    CGContextAddLineToPoint(context, UL.x,routeView.frame.size.height - UL.y);

    CGContextStrokePath(context);

这里 A 是角度,它不是静态的,中间是point1 和 point2 的中间点

以获取更多参考查看此

我错过了什么吗? 如果您有任何想法,请帮助我......

谢谢,

I am facing a typical problem in rotating an object. Description is as given below
I have taken two CGPoint let say point1 and point2
point1 = (50,50)
point2 = (150, 50)
this point will draw a horizontal line.

Now i am drawing a rectangle with that point on it. Width is 100 and height is 10. Angle is 0.
see screen shot
enter image description here

works fine
now i change the point let say
point1 = (50,50)
point2 = (50,150)
this point will draw a vertical line.

For rectangle Angle is 90.
With this point rectangle is not drawing properly
see screen shot
enter image description here

My code for drawing rectangle is :

    CGPoint mid = CGPointMake((point1.x+point2.x)/2, (point1.y+point2.y)/2)
    CGPoint UL = CGPointMake(mid.x + ( Width / 2 ) * cos (A) - ( Height / 2 ) * sin (A) ,  mid.y + ( Height / 2 ) * cos (A)  + ( Width / 2 ) * sin (A));
    CGContextMoveToPoint(context, UL.x,routeView.frame.size.height - UL.y);
    CGPoint UR = CGPointMake(mid.x - ( Width / 2 ) * cos (A) - ( Height / 2 ) * sin (A) ,  mid.y + ( Height / 2 ) * cos (A)  - ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, UR.x,routeView.frame.size.height - UR.y);
    CGPoint BR = CGPointMake(mid.x - ( Width / 2 ) * cos (A) + ( Height / 2 ) * sin (A) ,  mid.y - ( Height / 2 ) * cos (A)  - ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, BR.x,routeView.frame.size.height - BR.y);
    CGPoint BL = CGPointMake(mid.x + ( Width / 2 ) * cos (A) + ( Height / 2 ) * sin (A) ,  mid.y - ( Height / 2 ) * cos (A)  + ( Width / 2 ) * sin (A));
    CGContextAddLineToPoint(context, BL.x,routeView.frame.size.height - BL.y);
    CGContextAddLineToPoint(context, UL.x,routeView.frame.size.height - UL.y);

    CGContextStrokePath(context);

Here A is Angle and it is not static, mid is middle point of point1 and point2

for more ref see this

Am I missing something?
Please help me if you have any idea.......

Thanks,

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

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

发布评论

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

评论(1

怎言笑 2024-12-14 19:46:55

让我猜猜,它实际上旋转了大约 26 度,太远了,对吧?

(90 x 180) / Pi ~= 5156.62 = (360 x 14) + 90 + 26.62

您将其旋转了 90 弧度< /strong> 错误地。

Let me guess, it's actually rotated about 26 degrees too far, right?

(90 x 180) / Pi ~= 5156.62 = (360 x 14) + 90 + 26.62

You rotated it 90 radians by mistake.

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