<p>The arbitrary line can be expressed as <code>y = a*x+b</code> (assuming it's not parallel to the y axis). </p>

<p>If the other line is parallel to the y axis then you can simply fill in the x coordinate of any point on the line into the formula of the first line. Then you can check whether they intersect by ensuring the result lies between the endpoints of the second line.</p>

<p>For the second line parallel to the x axis you just flip the x and y coordinates.</p>

CC BY-SA 3.0
2016-03-04T16:28:17.557Z
2016-03-04T16:28:17.557Z
ratchet freak
<p>the arbitrary line can be represented as $P = (x,y) = P_0 + \lambda. \vec{dir}$ (works in n dimensions, no special case).
If your other line is $x=x_1$ simply inject this in to solve for $\lambda$ and get $y$: $y=y_0+(x_1-x_0).\frac{{dir}_y}{{dir}_x}$.</p>

CC BY-SA 3.0
2016-03-04T21:55:48.563Z
But the other line x = x1 is an infinite line parallel to the y-axis. Is not there a significant difference between a line and a line segment where both are parallel to y-axis ?
sajis997
CC BY-SA 3.0
2016-03-05T00:47:07.520Z
woops, you want segments. then verify that y match, and that $\lambda$ match.
Fabrice NEYRET
CC BY-SA 3.0
2016-03-04T21:23:22.713Z
2016-03-04T21:23:22.713Z
Fabrice NEYRET
<p>I believe that the issue may already have been discussed here. I want to find if two line segments does intersect and if they do then find and store and intesection points. Now it is already confirmed that one of the line segments will always be parallel to x-axis or y-axis. 
So in that case how should we reformulate the basic algorithm that checks the line segments intersection?</p>

CC BY-SA 3.0
2016-03-05T18:04:10.747Z
Is it known which line segment is axis parallel and which axis it is parallel to? Are you given two line segments, where one of them is parallel to one of the axes but you don't know which line segment or which axis, or are you given the first line segment, knowing it is always parallel to the x axis, and the second one may be any arbitrary line segment?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-03-05T18:05:11.483Z
These seemingly subtle differences may make a large difference to the approach, and so answerers will need to know which is the case.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-03-05T18:28:54.393Z
It is confirmed that one of the line segment is either parallel to x-axis or y-axis and the other line segment may be or may be not parallel to either of the axis.
sajis997
<geometry><mathematics><vectors>
intersection between line segments - narrowed precondition
sajis997
# intersection between line segments - narrowed precondition

(Asked by: username_0 on 2016-03-04)

I believe that the issue may already have been discussed here. I want to find if two line segments does intersect and if they do then find and store and intesection points. Now it is already confirmed that one of the line segments will always be parallel to x-axis or y-axis.
So in that case how should we reformulate the basic algorithm that checks the line segments intersection?

**Tags:** geometry, mathematics, vectors

> **Comments:**
> **username_1**: Is it known which line segment is axis parallel and which axis it is parallel to? Are you given two line segments, where one of them is parallel to one of the axes but you don't know which line segment or which axis, or are you given the first line segment, knowing it is always parallel to the x axis, and the second one may be any arbitrary line segment?  
> **username_1**: These seemingly subtle differences may make a large difference to the approach, and so answerers will need to know which is the case.  
> **username_0**: It is confirmed that one of the line segment is either parallel to x-axis or y-axis and the other line segment may be or may be not parallel to either of the axis.  

---

## 2 answers

### Answer by username_2 on 2016-03-04. Score: 1

The arbitrary line can be expressed as `y = a*x+b` (assuming it's not parallel to the y axis).

If the other line is parallel to the y axis then you can simply fill in the x coordinate of any point on the line into the formula of the first line. Then you can check whether they intersect by ensuring the result lies between the endpoints of the second line.

For the second line parallel to the x axis you just flip the x and y coordinates.


---

### Answer by username_3 on 2016-03-04. Score: 1

the arbitrary line can be represented as $P = (x,y) = P\_0 + \lambda. \vec{dir}$ (works in n dimensions, no special case).
If your other line is $x=x\_1$ simply inject this in to solve for $\lambda$ and get $y$: $y=y\_0+(x\_1-x\_0).\frac{{dir}\_y}{{dir}\_x}$.

> **Comments:**
> **username_0**: But the other line x = x1 is an infinite line parallel to the y-axis. Is not there a significant difference between a line and a line segment where both are parallel to y-axis ?  
> **username_3**: woops, you want segments. then verify that y match, and that $\lambda$ match.  

---

