扩展线段以适合边界框

时间:2011-08-29 23:03:22

标签: algorithm math c#-3.0 line computational-geometry

我有一个由两个pointF定义的线段,以及一个2D边界矩形。我想尽可能地在两个方向上扩展线段,以便该段与边界框的墙齐平。以下是我正在尝试做的一些例子:

enter image description here

有没有人对如何做到这一点有任何建议?

6 个答案:

答案 0 :(得分:3)

将矩形定义为四行。

找到您的线与四条线中的每条线之间的交点。 (你的高中几何学怎么样?)

在这四个交叉点中,确定哪些点位于矩形的边界内。 (找到x和y值都在矩形范围内的交点。)

您的算法还必须允许以下边缘情况:

  • 该线与矩形的水平边缘的垂直方向
  • 平行
  • 该线实际上与矩形的一角相交。

答案 1 :(得分:3)

这是python中的代码示例:

def extend(xmin, ymin, xmax, ymax, x1, y1, x2, y2):
    if y1 == y2:
        return (xmin, y1, xmax, y1)
    if x1 == x2:
        return (x1, ymin, x1, ymax)

    # based on (y - y1) / (x - x1) == (y2 - y1) / (x2 - x1)
    # => (y - y1) * (x2 - x1) == (y2 - y1) * (x - x1)
    y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
    y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)

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

    if ymin <= y_for_xmin <= ymax:
        if xmin <= x_for_ymax <= xmax:
            return (xmin, y_for_xmin, x_for_ymax, ymax)
        if xmin <= x_for_ymin <= xmax:
            return (xmin, y_for_xmin, x_for_ymin, ymin)
    if ymin <= y_for_xmax <= ymax:
        if xmin <= x_for_ymin <= xmax:
            return (x_for_ymin, ymin, xmax, y_for_xmax)
        if xmin <= x_for_ymax <= xmax:
            return (x_for_ymax, ymax, xmax, y_for_xmax)

def test():
    assert (2, 1,  2, 5) == extend(1, 1,  5, 5,  2, 3,  2, 4)
    assert (1, 2,  4, 5) == extend(1, 1,  5, 5,  2, 3,  3, 4)
    assert (1, 3,  5, 3) == extend(1, 1,  5, 5,  3, 3,  4, 3)
    assert (1, 1,  5, 5) == extend(1, 1,  5, 5,  2, 2,  3, 3)
    assert (3, 1,  5, 5) == extend(1, 1,  5, 5,  3.5, 2,  4, 3)

if __name__ == '__main__':
    test()

它不会检查该段是否包含在矩形中,并且如果它在其外部也应该起作用(如果没有实际的段交叉,则返回None -implicit)。

它基于矩形具有与轴平行的段的假设。

答案 2 :(得分:1)

一种选择是定义在某个变量t范围内的线段的参数表示,然后定义四个线性方程式,用于定义盒子侧面的线条(在所有方向上无限延伸)。我们的想法是,当您 check where the segment hits these lines 时,对于每个方向,您可以扩展细分,您将获得两个交叉点 - 一个用于水平交叉点,另一个用于垂直交叉点。无论哪种内容都在你想要选择的内容中。

要执行此操作,请计算通过在每个方向上延伸线段而形成的线的参数t的值,在该方向上您可以击中四条边界线中的一条。我假设线段被参数化,使得t∈[0,1]。然后,您将获得(最多)四个t值,这些值对应于线与边界框相交的参数 - 两个值≥1表示线在一个方向上的延伸,两个值≤0表示线在另一个方向上的延伸。在这四个值中,您要选择最小的t≥1值和最大的t≥0的值(这些值表示在撞到墙壁之前延伸出每个方向上最短距离的线的参数) 。获得这两个参数后,将t的值插回到原始参数化中,以获得所需的两个交叉点,然后将新线段定义为从第一个到第二个的跨越。< / p>

请注意,此算法通常可用于扩展线段以填充任何凸多边形的边界,包括非轴对齐的矩形。您实际上不需要测试您找到的任何点是否包含在边界框中;您只需查看参数t的值,即可了解哪个交叉点更接近细分的端点。

希望这有帮助!

答案 3 :(得分:0)

改进的andredor代码 - 为线与顶部和底部或左右边缘相交时添加边缘情况。提供的代码用于处理以测试算法。通过单击鼠标设置第一个点,并使用当前鼠标指针位置连续更新第二个点。

int px = 100, py = 100;

void setup() {
  size(480, 640);
  background(102);
}

void draw() {
  stroke(255);
  rect(0, 0, 480, 640);
  stroke(100);

  if (mousePressed == true) {
    px = mouseX;
    py = mouseY;
  }
  extendLine(0, 0, 480, 640, px, py, mouseX, mouseY);
}

void extendLine(int xmin, int ymin, int xmax, int ymax, int x1, int y1, int x2, int y2) {
    if (y1 == y2) {
        line(xmin, y1, xmax, y1);
        return;
    }
    if(x1 == x2) {
        line(x1, ymin, x1, ymax);
        return;
    }

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

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

    if (ymin <= y_for_xmin && y_for_xmin <= ymax
     && ymin <= y_for_xmax && y_for_xmax <= ymax) {
       line(xmin, y_for_xmin, xmax, y_for_xmax);
       return;
    } else if (ymin <= y_for_xmin && y_for_xmin <= ymax) {
        if (xmin <= x_for_ymax && x_for_ymax <= xmax) {
            line(xmin, y_for_xmin, x_for_ymax, ymax);
            return;
        }
        else if(xmin <= x_for_ymin && x_for_ymin <= xmax) {
            line(xmin, y_for_xmin, x_for_ymin, ymin);
            return;
        }
    } else if (ymin <= y_for_xmax && y_for_xmax <= ymax){
        if (xmin <= x_for_ymin && x_for_ymin <= xmax){
            line(x_for_ymin, ymin, xmax, y_for_xmax);
            return;
        }
        if(xmin <= x_for_ymax && x_for_ymax <= xmax){
            line(x_for_ymax, ymax, xmax, y_for_xmax);
            return;
        }
    } else if (xmin <= x_for_ymin && x_for_ymin <= xmax
     && xmin <= x_for_ymax && x_for_ymax <= xmax) { 
         line(x_for_ymin, ymin, x_for_ymax, ymax);
         return;
    }
}

答案 4 :(得分:0)

@andredor算法的扩展版本,可以覆盖所有情况(还包括线段不平行于轴的情况,例如,线段对角线时)。对该方法作了详尽的解释作为文档。

def extend_line(xmin, ymin, xmax, ymax, x1, y1, x2, y2):
    """
    Extend a line so that it reaches the walls of the bbox.

    Args:
        xmin(int): The very left coordinate of the bbox.
        ymin(int): The very top coordinate of the bbox.
        xmax(int): The very right coordinate of the bbox.
        ymax(int): The very bottom coordinate of the bbox.
        x1(int): The start x coordinate of the line.
        y1(int): The start y coordinate of the line.
        x2(int): The end x coordinate of the line.
        y2(int): The end y coordinate of the line.

    Returns:
        - (list): The start and end (x, y) coordinates of the extended line.
    """

    # If we imagine extending the line until it crosses the top wall of the 
    # bbox at point `(xmin, y_for_xmin)` and then imagine drawing 
    # perpendicular lines from each point `(x1, y1)`, `(x2, y2)` to the wall 
    # of the bbox, we end up with 2 perpendicular trianlges with the same 
    # angles - similar triangles. The rule of the similar triangles is that   
    # the side lengths of two similar triangles are proportional. 
    # That's how we get the equal ratios:
    # `| y_for_xmin - y1 | / | xmin - x1 | == | y2 - y1 | / | x2 - x1 |`
    # After we move some numbers from one to the other side of this equation, 
    # we get the value for `y_for_xmin`. That's where the line should cross 
    # the top wall of the bbox. We do the same for all other coordinates.
    # NOTE: These calculations are valid if one starts to draw a line from top 
    # to botton and from left to right. In case the direction is reverted, we 
    # need to switch the min and max for each point (x, y). We do that below.
    y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
    y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
    x_for_ymin = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
    x_for_ymax = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)

    # The line is vertical
    if (x2 - x1) < (y2 - y1):
        # The line is drawn from right to left
        if x1 > x2:
            # Switch the min and max x coordinates for y, 
            # because the direction is from right (min) to left (max)
            y_for_xmin, y_for_xmax = y_for_xmax, y_for_xmin
    # The line is horizontal
    else:
        # The line is drawn from bottom to top
        if y1 > y2:
            # Switch the min and max y coordinates for x,
            # because the direction is from bottom (min) to top (max)
            x_for_ymin, x_for_ymax = x_for_ymax, x_for_ymin

    # The line is drawn from right to left
    if x1 > x2:
        # Get the maximal value for x1.
        # When `x_for_ymin < xmin`(line goes out of the 
        # bbox from the top), we clamp to xmin.
        x1 = max(max(int(x_for_ymin), xmin), x1)
    # The line is drawn from left to right
    else:
        # Get the minimal value for x1.
        # When `x_for_ymin < xmin`(line goes out of the 
        # bbox from the top), we clamp to xmin. 
        x1 = min(max(int(x_for_ymin), xmin), x1)

    # Get the maximal value for x2.
    # When `x_for_ymax > xmax` (line goes out of the 
    # bbox from the bottom), we clamp to xmax.
    x2 = max(min(int(x_for_ymax), xmax), x2)

    # Get the minimal value for y1
    # When `y_for_xmin < ymin`(line goes out of the 
    # bbox from the left), we clamp to ymin.
    y1 = min(max(int(y_for_xmin), ymin), ymax)

    # Get the minimal value for y2
    y2 = min(int(y_for_xmax), ymax)

    # Done
    return [x1, y1, x2, y2]

答案 5 :(得分:0)

我通过@tsveti_iko 修改了代码,因为当 y_for_xmin 为“无穷大”(如果 x2 - x1 为 0)时,我遇到了一些 int 转换无法正常工作的问题

import math
extend_line(xmin, ymin, xmax, ymax, x1, y1, x2, y2):
        
        """
        Extend a line so that it reaches the walls of the bbox.

        Args:
            xmin(int): The very left coordinate of the bbox.
            ymin(int): The very top coordinate of the bbox.
            xmax(int): The very right coordinate of the bbox.
            ymax(int): The very bottom coordinate of the bbox.
            x1(int): The start x coordinate of the line.
            y1(int): The start y coordinate of the line.
            x2(int): The end x coordinate of the line.
            y2(int): The end y coordinate of the line.

        Returns:
            - (list): The start and end (x, y) coordinates of the extended line.
        """

        # If we imagine extending the line until it crosses the top wall of the 
        # bbox at point `(xmin, y_for_xmin)` and then imagine drawing 
        # perpendicular lines from each point `(x1, y1)`, `(x2, y2)` to the wall 
        # of the bbox, we end up with 2 perpendicular trianlges with the same 
        # angles - similar triangles. The rule of the similar triangles is that   
        # the side lengths of two similar triangles are proportional. 
        # That's how we get the equal ratios:
        # `| y_for_xmin - y1 | / | xmin - x1 | == | y2 - y1 | / | x2 - x1 |`
        # After we move some numbers from one to the other side of this equation, 
        # we get the value for `y_for_xmin`. That's where the line should cross 
        # the top wall of the bbox. We do the same for all other coordinates.
        # NOTE: These calculations are valid if one starts to draw a line from top 
        # to botton and from left to right. In case the direction is reverted, we 
        # need to switch the min and max for each point (x, y). We do that below.
        y_for_xmin = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
        y_for_xmax = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
        x_for_ymin = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
        x_for_ymax = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)

        # The line is vertical
        if (x2 - x1) < (y2 - y1):
            # The line is drawn from right to left
            if x1 > x2:
                # Switch the min and max x coordinates for y, 
                # because the direction is from right (min) to left (max)
                y_for_xmin, y_for_xmax = y_for_xmax, y_for_xmin
        # The line is horizontal
        else:
            # The line is drawn from bottom to top
            if y1 > y2:
                # Switch the min and max y coordinates for x,
                # because the direction is from bottom (min) to top (max)
                x_for_ymin, x_for_ymax = x_for_ymax, x_for_ymin

        # The line is drawn from right to left
        if x1 > x2:
            # Get the maximal value for x1.
            # When `x_for_ymin < xmin`(line goes out of the 
            # bbox from the top), we clamp to xmin.
            x1 = max(max(int(x_for_ymin), xmin), x1)
        # The line is drawn from left to right
        else:
            # Get the minimal value for x1.
            # When `x_for_ymin < xmin`(line goes out of the 
            # bbox from the top), we clamp to xmin.
            if math.isinf(x_for_ymin):
                x1 = min(xmin,x1)
            else:
                x1 = min(max(int(x_for_ymin), xmin), x1)

        # Get the maximal value for x2.
        # When `x_for_ymax > xmax` (line goes out of the 
        # bbox from the bottom), we clamp to xmax.
        if math.isinf(x_for_ymax):
            x2 = max(xmax,x2)
        else:
            x2 = max(min(int(x_for_ymax), xmax), x2)

        # Get the minimal value for y1
        # When `y_for_xmin < ymin`(line goes out of the 
        # bbox from the left), we clamp to ymin.
        if math.isinf(y_for_xmin):
            y1 = min(ymin,ymax)
        else:
            y1 = min(max(int(y_for_xmin), ymin), ymax)


        # Get the minimal value for y2
        if math.isinf(y_for_xmin):
            y2 = ymax
        else:
            y2 = min(int(y_for_xmax), ymax)
        # Done
        return [x1, y1, x2, y2]