Advertisement
ijontichy

quad.py

Mar 24th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import fractions
  2.  
  3. def quadratic(a, b, c):
  4.     s1 = ( (b**2) - (4*a*c) ) ** 0.5
  5.     s2 = 2*a
  6.  
  7.     ret = [(-b - s1) / s2, (-b + s1) / s2]
  8.     return ret
  9.  
  10. def curveFromVP(vertex, point):
  11.     """Finds a quadratic curve given vertex and point coordinate pairs.
  12. Returns in the format ("equation string", a), where the equation is in
  13. the form y=a(x-h)**2 + k."""
  14.     vX, vY = vertex
  15.     pX, pY = point
  16.     aMult = (pX - vX) ** 2
  17.     ret = fractions.Fraction(pY - vY, aMult)
  18.  
  19.     if ret == 1: retA = ""
  20.     elif ret._denominator == 1: retA = str(ret)
  21.     else: retA = "({})".format(ret)
  22.  
  23.     if vX == 0:
  24.         retX = "x"
  25.     else:
  26.         retX = "(x{0:+})".format(-vX)
  27.  
  28.     if vY == 0:
  29.         retY = ""
  30.     elif vY < 0:
  31.         retY = " - {}".format(abs(vY))
  32.     else:
  33.         retY = " + {}".format(abs(vY))
  34.  
  35.     retStr = "f(x) = {}{}**2{}".format(retA, retX, retY)
  36.  
  37.     return retStr, ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement