Skip to content Skip to sidebar Skip to footer

Finding The Intersect Between A Quadratic And Line

I am trying to find the intersect between a straight line and a quadratic curve, however the result I am getting appears to be imaginary although I don't see how this can be the ca

Solution 1:

The graphed curves you presented vs. your equations are not the same, and your equations do not intersect.

I rewrote some example code for you. numpy isn't needed, and an exact solution is possible.

import math
import collections


def calculateIntersection(p, l):
  b = p.B - l.slope
  c = p.C - l.yInt

  discriminant = b**2 - (4 * p.A * c)

  if discriminant > 0.0:
    # 2 points of intersection
    x1 = (-b + math.sqrt(discriminant)) / (2.0 * p.A)
    x2 = (-b - math.sqrt(discriminant)) / (2.0 * p.A)

    return discriminant, [(x1, l.slope * x1 + l.yInt), (x2, l.slope * x2 + l.yInt)]

  elif discriminant == 0.0:
    # 1 point of intersection
    x1 = -b / (2.0 * p.A)

    return discriminant, [(x1, slope * x1 + l.yInt)]
  else:
    # no points of intersection
    return discriminant, []


Line = collections.namedtuple('Line', 'slope yInt')
Poly = collections.namedtuple('Poly', 'A B C')

p = Poly(A=-3.09363812e-04, B=1.52138019e+03, C=-1.87044961e+09)
print(p)

l = Line(slope=1.06446434e-03, yInt=-2.61660911e+03)
print(l)

(discriminant, points) = calculateIntersection(p, l)

if (len(points) > 0):
  print("Intersection: {}".format(points))
else:
  print("No intersection: {}".format(discriminant))

Post a Comment for "Finding The Intersect Between A Quadratic And Line"