如何在矩形中拟合一条线

时间:2013-03-26 17:29:01

标签: iphone ios objective-c math

我正在尝试将python算法(此处:Extending a line segment to fit into a bounding box)转换为我的iPhone应用程序..但它只适用于我的startPoint.x>然后是endPointx

这里有什么改变? 我不明白..

如果我从左上角到右下角画一条线就行了!但是如果我从右上角到左下角画一条线就会失败。所以它只适用于一个方向..我认为我必须改变一些变量,如果它从右到左

MIN为(0,0),MAX取决于设备,但适用于iPhone Retina(300,568)

    - (NSMutableArray *) extendAlgorithm:(CGPoint)start withEnd:(CGPoint)end withBorderMin:(CGPoint)min andBorderMax:(CGPoint)max {

    int x1 = (int) start.x;     int y1 = (int) start.y;
    int x2 = (int) end.x;       int y2 = (int) end.y;
    int xmin = (int) min.x;     int ymin = (int) min.y;
    int xmax = (int) max.x;     int ymax = (int) max.y;

    if(y1 == y2) {
        return [[NSMutableArray alloc] initWithObjects:
                [NSNumber numberWithDouble:xmin],
                [NSNumber numberWithDouble:y1],
                [NSNumber numberWithDouble:xmax],
                [NSNumber numberWithDouble:y1],
                nil];
    }

    if(x1 == x2) {
        return [[NSMutableArray alloc] initWithObjects:
                [NSNumber numberWithDouble:x1],
                [NSNumber numberWithDouble:ymin],
                [NSNumber numberWithDouble:x1],
                [NSNumber numberWithDouble:ymax],
                nil];
    }

    double y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
    double y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);

    double x_for_ymin = x1  + (x2 - x1) * (ymin - y1) / (y2 - y1);
    double x_for_ymax = x1  + (x2 - x1) * (ymax - y1) / (y2 - y1);

    if(ymin <= y_for_xmin <= ymax) {

        if(xmin <= x_for_ymax <= xmax) {
            return [[NSMutableArray alloc] initWithObjects:
                    [NSNumber numberWithDouble:xmin],
                    [NSNumber numberWithDouble:y_for_xmin],
                    [NSNumber numberWithDouble:x_for_ymax],
                    [NSNumber numberWithDouble:ymax],
                    nil];
        }

        if(xmin <= x_for_ymin <= xmax) {
            return [[NSMutableArray alloc] initWithObjects:
                    [NSNumber numberWithDouble:xmin],
                    [NSNumber numberWithDouble:y_for_xmin],
                    [NSNumber numberWithDouble:x_for_ymin],
                    [NSNumber numberWithDouble:ymin],
                    nil];
        }
    }

    if(ymin <= y_for_xmax <= ymax) {

        if(xmin <= x_for_ymin <= xmax) {
            return [[NSMutableArray alloc] initWithObjects:
                    [NSNumber numberWithDouble:x_for_ymin],
                    [NSNumber numberWithDouble:ymin],
                    [NSNumber numberWithDouble:xmax],
                    [NSNumber numberWithDouble:y_for_xmax],
                    nil];
        }

        if(xmin <= x_for_ymax <= xmax) {
            return [[NSMutableArray alloc] initWithObjects:
                    [NSNumber numberWithDouble:x_for_ymax],
                    [NSNumber numberWithDouble:ymax],
                    [NSNumber numberWithDouble:xmax],
                    [NSNumber numberWithDouble:y_for_xmax],
                    nil];
        }
    }

    return nil;
}

2 个答案:

答案 0 :(得分:1)

if (ymin <= y_for_xmin <= ymax)

没有做你期望的事情(也许在Python中,但绝对不在C中),你必须把它写成

if (ymin <= y_for_xmin && y_for_xmin <= ymax)

我不知道你的算法是否涵盖了所有情况,所以我试图独立地找到一个算法。这就是我得到的:

- (NSArray *) extendAlgorithm:(CGPoint)start withEnd:(CGPoint)end withBorderMin:(CGPoint)min andBorderMax:(CGPoint)max
{
    CGFloat u1 = MAXFLOAT;
    CGFloat u2 = -MAXFLOAT;
    CGFloat tmp;
    if (end.x != start.x) {
        tmp = (max.x - start.x)/(end.x - start.x);
        if (tmp > 0 && tmp < u1)
            u1 = tmp;
        else if (tmp < 0 && tmp > u2)
            u2 = tmp;
        tmp = (min.x - start.x)/(end.x - start.x);
        if (tmp > 0 && tmp < u1)
            u1 = tmp;
        else if (tmp < 0 && tmp > u2)
            u2 = tmp;
    }
    if (end.y != start.y) {
        tmp = (max.y - start.y)/(end.y - start.y);
        if (tmp > 0 && tmp < u1)
            u1 = tmp;
        else if (tmp < 0 && tmp > u2)
            u2 = tmp;
        tmp = (min.y - start.y)/(end.y - start.y);
        if (tmp > 0 && tmp < u1)
            u1 = tmp;
        else if (tmp < 0 && tmp > u2)
            u2 = tmp;
    }
    CGPoint newStart, newEnd;
    newStart.x = start.x + u2 * (end.x - start.x);
    newStart.y = start.y + u2 * (end.y - start.y);
    newEnd.x = start.x + u1 * (end.x - start.x);
    newEnd.y = start.y + u1 * (end.y - start.y);
    NSLog(@"%@, %@", NSStringFromCGPoint(newStart), NSStringFromCGPoint(newEnd));

    return [[NSArray alloc] initWithObjects:
            [NSNumber numberWithDouble:newStart.x],
            [NSNumber numberWithDouble:newStart.y],
            [NSNumber numberWithDouble:newEnd.x],
            [NSNumber numberWithDouble:newEnd.y],
            nil];
}

(想法是考虑这条线

(x, y) = (start.x, start.y) + u * (end.x - start.x, end.y - start.y)

然后确定线穿过边界的参数u1u2。)

答案 1 :(得分:0)

我没有检查算法的任何内容,但跳出来的是这些行:

double y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
// ...

该表达式的结尾是整数除(您将xmin,x1,x2等转换为整数)。那些将有整数结果,被截断。因此,如果您期望1/2 == 0.5之类的东西,那么你会对(int)0感到惊讶。

尝试将堆栈变量声明为CGFloat,这是它们在CGPoint结构中的方式。