<p>The fifth null doesn't wander randomly, it just extends the red and blue lines off beyond their ends. You need to detect whether the intersection point is on the segment between the start and end nulls for each line.</p>

<p>You can test for it with this rather horrifying conditional: basically it tests to see if the intersection is outside the bounds of each point's x then y coordinates. </p>

<p>Replace the last line of your expression with this:</p>

<pre class="lang-js prettyprint-override"><code>if ((x &gt; p1[0] &amp;&amp; x &gt; p2[0])||(x &gt; p3[0] &amp;&amp; x &gt; p4[0])||(x &lt; p1[0] &amp;&amp; x &lt; p2[0])||(x &lt; p3[0] &amp;&amp; x &lt; p4[0])||(y &gt; p1[1] &amp;&amp; y &gt; p2[1])||(y &gt; p3[1] &amp;&amp; y &gt; p4[1])||(y &lt; p1[1] &amp;&amp; y &lt; p2[1])||(y &lt; p3[1] &amp;&amp; y &lt; p4[1])){
    value
}else{
    [x,y]
}
</code></pre>

<p>Another version, does the same thing but with nice readable code, and a re-useable function:</p>

<pre class="lang-js prettyprint-override"><code>function outsideBounds(samplePt, p1, p2){
  if (samplePt[0] &gt; Math.max(p1[0], p2[0])) {return true}
  if (samplePt[1] &gt; Math.max(p1[1], p2[1])) {return true}
  if (samplePt[0] &lt; Math.min(p1[0], p2[0])) {return true}
  if (samplePt[1] &lt; Math.min(p1[1], p2[1])) {return true}
  return false 
} 

if (outsideBounds([x,y], p1, p2)||outsideBounds([x,y], p3, p4)){
    value
}else{
    [x,y]
}
</code></pre>

<p><div class="youtube-embed"><div>
                <iframe width="640px" height="395px" src="https://www.youtube.com/embed/X_p8SrIqbug?start=0"></iframe>
            </div></div></p>

CC BY-SA 4.0
2019-01-04T02:58:18.473Z
2019-01-04T04:07:50.207Z
2019-01-04T04:07:50.207Z
stib
<p>I have two lines (The lines are imaginary, i have two Nulls as endpoints for each line)
I want a fifth Null two appear where the lines intersect.
I have used this code so far:</p>

<pre><code>p1 = thisComp.layer("red_end").transform.position;
p2 = thisComp.layer("red_start").transform.position;
p3 = thisComp.layer("blue_end").transform.position;
p4 = thisComp.layer("blue_start").transform.position;

m1vert = Math.abs(p1[0]-p2[0]) &lt; .0001;
m2vert = Math.abs(p3[0]-p4[0]) &lt; .0001;

if (! m1vert){
m1 = (p2[1]-p1[1])/(p2[0]-p1[0]);
b1 = p1[1]-m1*p1[0];
}
if (! m2vert){
  m2 = (p4[1]-p3[1])/(p4[0]-p3[0]);
  b2 = p3[1]-m2*p3[0];
}
if (m1vert &amp;&amp; m2vert){
  x = value[0];
  y = value[1];
}else if(m1vert){
  x = p1[0];
  y = m2*x + b2;
}else if (m2vert){
  x = p3[0];
  y = m1*x + b1;
}else if (m1 == m2){
  x = value[0];
  y = value[1];
}else{
  x = (b1-b2)/(m2-m1);
  y = m1*x +b1;
}
[x,y]
</code></pre>

<p>The fifth Null does appear in the intersections when the lines intersect, but also miserably stumble around on screen when the lines don't intersect.
I want the Null to go to a specific position whenever the lines don't intersect.
So how would I do that?
Any help is appreciated.</p>

<after-effects><after-effects-expressions>
After Effects: Detect if Lines are Intersecting
Walter
# After Effects: Detect if Lines are Intersecting

(Asked by: username_0 on 2019-01-02)

I have two lines (The lines are imaginary, i have two Nulls as endpoints for each line)
I want a fifth Null two appear where the lines intersect.
I have used this code so far:
p1 = thisComp.layer("red_end").transform.position;
p2 = thisComp.layer("red_start").transform.position;
p3 = thisComp.layer("blue_end").transform.position;
p4 = thisComp.layer("blue_start").transform.position;

m1vert = Math.abs(p1[0]-p2[0]) < .0001;
m2vert = Math.abs(p3[0]-p4[0]) < .0001;

if (! m1vert){
m1 = (p2[1]-p1[1])/(p2[0]-p1[0]);
b1 = p1[1]-m1*p1[0];
}
if (! m2vert){
  m2 = (p4[1]-p3[1])/(p4[0]-p3[0]);
  b2 = p3[1]-m2*p3[0];
}
if (m1vert && m2vert){
  x = value[0];
  y = value[1];
}else if(m1vert){
  x = p1[0];
  y = m2*x + b2;
}else if (m2vert){
  x = p3[0];
  y = m1*x + b1;
}else if (m1 == m2){
  x = value[0];
  y = value[1];
}else{
  x = (b1-b2)/(m2-m1);
  y = m1*x +b1;
}
[x,y]

The fifth Null does appear in the intersections when the lines intersect, but also miserably stumble around on screen when the lines don't intersect.
I want the Null to go to a specific position whenever the lines don't intersect.
So how would I do that?
Any help is appreciated.

**Tags:** after-effects, after-effects-expressions


---

## 1 answers

### Answer by username_1 on 2019-01-04. Score: 0 | Accepted answer

The fifth null doesn't wander randomly, it just extends the red and blue lines off beyond their ends. You need to detect whether the intersection point is on the segment between the start and end nulls for each line.
You can test for it with this rather horrifying conditional: basically it tests to see if the intersection is outside the bounds of each point's x then y coordinates. 
Replace the last line of your expression with this:
if ((x > p1[0] && x > p2[0])||(x > p3[0] && x > p4[0])||(x < p1[0] && x < p2[0])||(x < p3[0] && x < p4[0])||(y > p1[1] && y > p2[1])||(y > p3[1] && y > p4[1])||(y < p1[1] && y < p2[1])||(y < p3[1] && y < p4[1])){
    value
}else{
    [x,y]
}

Another version, does the same thing but with nice readable code, and a re-useable function:
function outsideBounds(samplePt, p1, p2){
  if (samplePt[0] > Math.max(p1[0], p2[0])) {return true}
  if (samplePt[1] > Math.max(p1[1], p2[1])) {return true}
  if (samplePt[0] < Math.min(p1[0], p2[0])) {return true}
  if (samplePt[1] < Math.min(p1[1], p2[1])) {return true}
  return false 
} 

if (outsideBounds([x,y], p1, p2)||outsideBounds([x,y], p3, p4)){
    value
}else{
    [x,y]
}


---

