偏斜与角度的关系 - CGAffine变换

时间:2012-07-25 15:01:10

标签: objective-c ios5 trigonometry quartz-2d cgaffinetransform

我正在开发一个iPad应用程序,允许用户用手指画线,然后使用该线的起点和终点我计算相对于iPad的x / y平面的线的角度。

我的问题是我想使用绘制线的角度来设置我正在绘制到当前上下文中的图像的倾斜。

这是我的drawRect方法的相关代码:

         CGSize size =  CGSizeMake(1024, 768);
         UIGraphicsBeginImageContext(size);
         CGContextRef ctx = UIGraphicsGetCurrentContext();
         CGAffineTransform skewIt = CGAffineTransformMake(1, 0, skewValue, 1, 0, 0);
         CGContextConcatCTM(ctx, skewIt);

         CGContextDrawImage(UIGraphicsGetCurrentContext(),
                            CGRectMake(0,0,size.width, size.height),
                            theImage.CGImage);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

我在touchEnded方法中计算线条的角度,绘制线条的点值存储在名为skewArray的数组中:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (stepThree){
        CGContextClearRect(skewContext, CGRectMake(0, 0, 1024, 668));

        CGPoint pt1 = CGPointMake([[skewArray objectAtIndex:1]point3].x, [[skewArray objectAtIndex:1]point3].y);
        CGPoint pt2 = CGPointMake([[skewArray objectAtIndex:[skewArray count]-1]point3].x, [[skewArray objectAtIndex:[skewArray count]-1]point3].y);
        CGPoint pt3 = CGPointMake([[skewArray objectAtIndex:[skewArray count]-1]point3].x, [[skewArray objectAtIndex:1]point3].y);

        CGFloat dis1 = sqrt((pow((pt2.x - pt1.x), 2) + pow((pt2.y - pt1.y), 2)));
        CGFloat dis2 = sqrt((pow((pt3.x - pt2.x), 2) + pow((pt3.y - pt2.y), 2)));
        CGFloat dis3 = sqrt((pow((pt1.x - pt3.x), 2) + pow((pt1.y - pt3.y), 2)));

        angle = acos(((-1*pow(dis2, 2))+pow(dis1, 2)+pow(dis3, 2))/(2*dis1*dis3)) * 180/3.14;

        //Do something with the angle to produce the appropriate skew value

        [self setNeedsDisplay];
       }
   }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在剪切映射上查看此Wikipedia article。看起来你在倾斜值时会更好地获得斜率并使用1.0 / m。