Advertisement
billysback

line function python

Feb 3rd, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. class Line:
  2.     def __init__(self, x1, y1, x2, y2):
  3.         diffx = (x1-x2)
  4.         diffy = (y1-y2)
  5.  
  6.         c = 0
  7.         if diffx != 0:
  8.             grad = diffy/diffx
  9.             c = y1 - (x1*grad)
  10.         else:
  11.             c = x1
  12.         self.c = c
  13.         self.diffx = diffx
  14.         self.diffy = diffy
  15.  
  16.     def getY(self, x):
  17.         if self.diffx != 0:
  18.             grad = self.diffy/self.diffx
  19.             return (x*grad) + self.c
  20.         else:
  21.             return None
  22.  
  23.     def getX(self, y):
  24.         if self.diffx != 0:
  25.             grad = self.diffy/self.diffx
  26.             return (y/grad) - (self.c/grad)
  27.         else:
  28.             return self.c
  29.  
  30.     def getNormal(self, x, y):
  31.         if self.diffx != 0:
  32.             if self.diffy != 0:
  33.                 grad = -1/(self.diffy/self.diffx)
  34.                 y2 = y - (grad*x) + (grad*(x+1))
  35.                 return Line(x, y, (x+1), y2)
  36.             else:
  37.                 return Line(x, y, x, y + 1)
  38.         else:
  39.             return Line(x, y, x + 1, y)
  40.  
  41.     def getAngle(self):
  42.         if self.diffx != 0 and self.diffy != 0:
  43.             x1, y1 = self.getX(1), 1
  44.             x2, y2 = self.getX(2), 2
  45.             nx = x2 - x1
  46.             ny = y2 - y1
  47.             na = 0
  48.             if nx > 0 and ny > 0:
  49.                 na = math.asin(nx/nforce)
  50.             elif nx > 0 and ny < 0:
  51.                 na = pi - math.asin(math.fabs(nx)/nforce)
  52.             elif nx < 0 and ny < 0:
  53.                 na = pi + math.asin(math.fabs(nx)/nforce)
  54.             elif nx < 0 and ny > 0:
  55.                 na = (2*pi) - math.asin(math.fabs(nx)/nforce)
  56.             else:
  57.                 raise Exception(" Impossible Coordinates: ", nx, ",", ny)
  58.             return na, False
  59.            
  60.         elif self.diffx != 0:
  61.             return (0), True
  62.         elif self.diffy != 0:
  63.             return (pi/2), True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement