0

I would like to find the intersection point of a vector or its extension with the surrounding rectangle, that is, in the image (1) and (2), given (x1, y1), (x2, y2), (a1, a2), (b1, b2), we would like to obtain the point (c1, c2).

enter image description here

I have found the article Find collision point between vector and fencing rectangle but, since the positive y-axis is downward in python/windows, I could not manage the equations and parameters to acheive the correct result. The two following links are also related.

intersection between a line and square

Get intersection point of rectangle and line.

But they do not include the extension of the vector/line segment. How should the equations change to obtain the correct result?

Javad.Rad
  • 93
  • 1
  • 8
  • 1
    My cited [answer](https://stackoverflow.com/questions/58055629/intersection-between-a-line-and-square) is perfectly fine for your problem. `ox,oy` in my answer might be both inside and outside. – MBo Oct 11 '22 at 01:43
  • Why would the direction of the y axis affect the equations or the parameters? Coordinates are coordinates. – Sneftel Oct 11 '22 at 12:27
  • @Sneftel y-axis affects since, the same line will have a slop with opposite sign in the coordinate system on the paper vs. opencv/python. In python, (0,0) is top left corner and y increases downward, while, in conventional coordinate system in mathematics, y increases upward. – Javad.Rad Oct 11 '22 at 12:57
  • Right. Which affects both the inputs and the outputs, resulting in the algorithm working in either case. The number 3 doesn't care whether it's left or right. – Sneftel Oct 11 '22 at 13:00
  • @MBo I can not clearly get from your [answer](https://stackoverflow.com/questions/58055629/intersection-between-a-line-and-square) what if the vector is fully inside the rectangle? i.e. Does it give the solution for the extension of the vector intersecting the rectangle like figure (1) in my question? – Javad.Rad Oct 11 '22 at 13:13
  • Yes. The second point just gives a direction. – MBo Oct 11 '22 at 13:14
  • "since the positive y-axis is downward": in your case or in the case of the article ? –  Oct 12 '22 at 14:11
  • @YvesDaoust my case. This [solution](https://stackoverflow.com/questions/58055629/intersection-between-a-line-and-square) is the answer. – Javad.Rad Oct 12 '22 at 17:42

1 Answers1

1

A point on the ray is given by the coordinates (a1 + t (b1 - a1), a2 + t (b2 - a2)).

The intersections are obtained by solving the four equations

a1 + t (b1 - a1) = x1
a1 + t (b1 - a1) = x2
a2 + t (b2 - a2) = y1
a2 + t (b2 - a2) = y2

for t and taking the smallest positive solution. If some of the coefficients of t is zero, just ignore the corresponding equation.

The orientations of the axis play no role.