title: Intersection of parabolic curve and line segment
body: <p>I have an equation for a parabolic curve intersecting a specified point, in my case where the user clicked on a graph. </p>
  
  <pre><code> // this would typically be mouse coords on the graph
   var _target:Point = new Point(100, 50);
  
   public static function plot(x:Number, target:Point):Number{
    return (x * x) / target.x * (target.y / target.x);
   }
  </code></pre>
  
  <p>This gives a graph such as this: </p>
  
  <p><img src="https://i.stack.imgur.com/FqrgS.png" alt="parabolic curve"></p>
  
  <p>I also have a series of line segments defined by start and end coordinates:</p>
  
  <pre><code>startX:Number, startY:Number, endX:Number, endY:Number
  </code></pre>
  
  <p>I need to find if and where this curve intersects these segments (A):</p>
  
  <p><img src="https://i.stack.imgur.com/X8nHt.png" alt="alt text"></p>
  
  <p>If it's any help, <code>startX</code> is always <code>&lt; endX</code></p>
  
  <p>I get the feeling there's a fairly straight forward way to do this, but I don't really know what to search for, nor am I very well versed in "proper" math, so actual code examples would be very much appreciated. </p>
  
  <p>UPDATE:</p>
  
  <p>I've got the intersection working, but my solution gives me the coordinate for the wrong side of the y-axis. </p>
  
  <p>Replacing my target coords with A and B respectively, gives this equation for the plot:</p>
  
  <pre><code>(x * x) / A * (B/A)
  
  // this simplifies down to:
  (B * x * x) / (A * A)
  
  // which i am the equating to the line's equation
  (B * x * x) / (A * A) =  m * x + b
  
  // i run this through wolfram alpha (because i have no idea what i'm doing) and get:
  (A * A * m - A * Math.sqrt(A * A * m * m + 4 * b * B)) / (2 * B)
  </code></pre>
  
  <p>This <em>is</em> a correct answer, but I want the second possible variation. 
  I've managed to correct this by multiplying m with -1 before the calculation and doing the same with the x value the last calculation returns, but that feels like a hack.</p>
  
  <p>SOLUTION:</p>
  
  <pre><code> public static function intersectsSegment(targetX:Number, targetY:Number, startX:Number, startY:Number, endX:Number, endY:Number):Point {
    // slope of the line
    var m:Number = (endY - startY) / (endX - startX);
  
    // where the line intersects the y-axis
    var b:Number = startY - startX * m;
  
    // solve the two variatons of the equation, we may need both
    var ix1:Number = solve(targetX, targetY, m, b);
    var ix2:Number = solveInverse(targetX, targetY, m, b);
  
    var intersection1:Point;
    var intersection2:Point;
  
    // if the intersection is outside the line segment startX/endX it's discarded
    if (ix1 &gt; startX &amp;&amp; ix1 &lt; endX) intersection1 = new Point(ix1, plot(ix1, targetX, targetY));
    if (ix2 &gt; startX &amp;&amp; ix2 &lt; endX) intersection2 = new Point(ix2, plot(ix2, targetX, targetY));
  
    // somewhat fiddly code to return the smallest set intersection
    if (intersection1 &amp;&amp; intersection2) {
     // return the intersection with the smaller x value
     return intersection1.x &lt; intersection2.x ? intersection1 : intersection2;
    } else if (intersection1) {
     return intersection1;
    }
  
    // this effectively means that we return intersection2 or if that's unset, null
    return intersection2;
   }
  
   private static function solve(A:Number, B:Number, m:Number, b:Number):Number {
    return (m + Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
   }
  
   private static function solveInverse(A:Number, B:Number, m:Number, b:Number):Number {
    return (m - Math.sqrt(4 * (B / (A * A)) * b + m * m)) / (2 * (B / (A * A)));
   }
  
   public static function plot(x:Number, targetX:Number, targetY:Number):Number{
    return (targetY * x * x) / (targetX * targetX);
   }
  </code></pre>