对象旋转不正确 - iPhone 应用程序
我在旋转物体时遇到了一个典型的问题。说明如下
我已经采取了两个 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
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我猜猜,它实际上旋转了大约 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.