Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an code example in python:</p> <pre><code>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) # =&gt; (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 &lt;= y_for_xmin &lt;= ymax: if xmin &lt;= x_for_ymax &lt;= xmax: return (xmin, y_for_xmin, x_for_ymax, ymax) if xmin &lt;= x_for_ymin &lt;= xmax: return (xmin, y_for_xmin, x_for_ymin, ymin) if ymin &lt;= y_for_xmax &lt;= ymax: if xmin &lt;= x_for_ymin &lt;= xmax: return (x_for_ymin, ymin, xmax, y_for_xmax) if xmin &lt;= x_for_ymax &lt;= 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() </code></pre> <p>It doesn't check that the segment is contained in the rectangle and should work also if it is exterior to it (returns None -implicit- if there is no actual segment intersection).</p> <p>It is based on the assumption that the rectangle has the segments parallel with the axes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload