iOS - 如何将CGPoint转换为视图坐标?

时间:2018-01-10 06:21:58

标签: ios xcode

在我的iOS应用中,使用CIDetector检测矩形。从那里我得到四个CGPoint值的矩形。

但是我需要转换成视图坐标,以便我可以评估矩形的角度。

- (CIRectangleFeature *)_biggestRectangleInRectangles:(NSArray *)rectangles
{
if (![rectangles count]) return nil;

float halfPerimiterValue = 0;

CIRectangleFeature *biggestRectangle = [rectangles firstObject];

for (CIRectangleFeature *rect in rectangles)
{
    CGPoint p1 = rect.topLeft;
    CGPoint p2 = rect.topRight;
    CGFloat width = hypotf(p1.x - p2.x, p1.y - p2.y);

    CGPoint p3 = rect.topLeft;
    CGPoint p4 = rect.bottomLeft;
    CGFloat height = hypotf(p3.x - p4.x, p3.y - p4.y);

    CGFloat currentHalfPerimiterValue = height + width;

    if (halfPerimiterValue < currentHalfPerimiterValue)
    {
        halfPerimiterValue = currentHalfPerimiterValue;
        biggestRectangle = rect;
    }
}

return biggestRectangle;
}

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

如果您想将转换点转换为地图坐标,可以尝试使用:

open func convert(_ point: CGPoint, toCoordinateFrom view: UIView?) -> CLLocationCoordinate2D

它是MKMapView

的函数

答案 1 :(得分:0)

我为自己的问题找到了解决方案。

使用下面的方法我可以找到两个CGPoints之间的距离,根据我可以找到图像角度的距离。

- (CGFloat)calcDistanceFromCGPoints:(CGPoint )pt1 point2:(CGPoint )pt2{
CGPoint p2 = pt2;
CGPoint p1 = pt1;
//Assign the coord of p2 and p1...
//End Assign...
CGFloat xDist = (p2.x - p1.x); //[2]
CGFloat yDist = (p2.y - p1.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]
return distance;
}

-(NSString *)getDirection{

CGFloat width1 = [self calcDistanceFromCGPoints:_borderDetectLastRectangleFeature.bottomRight point2:_borderDetectLastRectangleFeature.bottomLeft];
CGFloat width2 = [self calcDistanceFromCGPoints:_borderDetectLastRectangleFeature.topRight point2:_borderDetectLastRectangleFeature.topLeft];
CGFloat height1 = [self calcDistanceFromCGPoints:_borderDetectLastRectangleFeature.bottomLeft point2:_borderDetectLastRectangleFeature.topLeft];
CGFloat height2 = [self calcDistanceFromCGPoints:_borderDetectLastRectangleFeature.bottomRight point2:_borderDetectLastRectangleFeature.topRight];

CGFloat absWidth = fabs(width2 - width1);
CGFloat absHeight = fabs(height2 - height1);

NSString *direction = @"";
if(absWidth > absHeight){
    CGFloat heightDiff = height1-height2;
    if((width1-width2) > 0){                // BOTTOM
        if(heightDiff > 0){
            if(heightDiff > DIRECTION_DIFF)
                direction = @"BottomLeft";
            else
                direction = @"Bottom";
        }
        else{
            if(heightDiff > DIRECTION_DIFF)
                direction = @"BottomRight";
            else
                direction = @"Bottom";
        }
    }
    else{                                   // TOP
        if(heightDiff > 0){
            if(heightDiff > DIRECTION_DIFF)
                direction = @"TopLeft";
            else
                direction = @"Top";
        }
        else{
            if(heightDiff > DIRECTION_DIFF)
                direction = @"TopRight";
            else
                direction = @"Top";
        }
    }
}
else{
    CGFloat widthDiff = width1-width2;      // LEFT
    if((height1-height2) > 0){
        if(widthDiff > 0){
            if(widthDiff > DIRECTION_DIFF)
                direction = @"TopLeft";
            else
                direction = @"Left";
        }
        else{
            if(widthDiff > DIRECTION_DIFF)
                direction = @"BottomLeft";
            else
                direction = @"Left";
        }
    }
    else{                                   // RIGHT
        if(widthDiff > 0){
            if(widthDiff > DIRECTION_DIFF)
                direction = @"TopRight";
            else
                direction = @"Right";
        }
        else{
            if(widthDiff > DIRECTION_DIFF)
                direction = @"TopLeft";
            else
                direction = @"Right";
        }
    }
}
return direction;
}