user907729
user907729

Reputation:

object rotation is not proper - iPhone application

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,

Upvotes: 2

Views: 226

Answers (1)

David Schwartz
David Schwartz

Reputation: 182827

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.

Upvotes: 1

Related Questions