Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Student: adi camus
- ID: 209020999
- Assignment no. 7
- Program: lines.py
- """
- def Xerr(x):
- if not isinstance(x,float) and not isinstance(x,int):
- raise ValueError("x must be a number", x)
- def Yerr(y):
- if not isinstance(y,float) and not isinstance(y,int):
- raise ValueError("y must be a number", y)
- class Point:
- def __init__(self,x,y):
- Xerr(x)
- Yerr(y)
- self.__x=x
- self.__y=y
- def get_x(self):
- return self.__x
- def get_y(self):
- return self.__y
- def set_x(self,x):
- Xerr(x)
- self.__x=x
- def set_y(self,y):
- Yerr(y)
- self.__y=y
- def __str__(self):
- return "({0:.2f},{1:.2f})".format(self.__x,self.__y)
- def p1err(p1):
- if not isinstance(p1, Point):
- raise Exception("must be type Point", p1)
- def p2err(p2):
- if not isinstance(p2, Point):
- raise Exception("must be type Point", p2)
- class line:
- def __init__(self,p1,p2):
- p1err(p1)
- p2err(p2)
- self.__p1=p1
- self.__p2=p2
- def get_p1(self):
- return self.__p1
- def get_p2(self):
- return self.__p2
- def set_p1(self,p1):
- p1err(p1)
- self.__p1=p1
- def set_p2(self,p2):
- p2err(p2)
- self.__p2=p2
- def is_vertical(self):
- try:
- return (self.__p2.get_y() - self.__p1.get_y()) / (self.__p2.get_x() - self.__p1.get_x())
- except ZeroDivisionError:
- return True
- def slope(self):
- try:
- return (self.__p2.get_y() - self.__p1.get_y()) / (self.__p2.get_x() - self.__p1.get_x())
- except ZeroDivisionError:
- return None
- def y_intersect(self):
- m=self.slope()
- if m==None:
- return None
- b=self.__p2.get_y() - m*self.__p2.get_x()
- return b
- def __str__(self):
- t=self.slope()
- if t==None:
- return "x = {},".format(self.__p2.get_x())
- return "y = {0:.2f}x + {1:.2f},".format(t,self.y_intersect())
- def parallel(self, other):
- if self.slope()==other.slope():
- return True
- return False
- def equals(self, other):
- # if self.__p2.get_y()-self.__p1.get_y()==other.__p2.get_y()-other.__p1.get_y() and self.__p2.get_x()-self.__p1.get_x()==other.__p2.get_x()-other.__p1.get_x():
- if self.__str__() == other.__str__():
- return True
- return False
- def intersection(self,other):
- if self.parallel(other)==True:
- return None
- if other.y_intersect()==None:
- x=other.get_p1().get_x()
- y=self.slope()*x+self.y_intersect()
- return "({0:.2f},{1:.2f})".format(x,y)
- if self.y_intersect()==None:
- x=self.get_p1().get_x()
- y=other.slope()*x+other.y_intersect()
- return "({0:.2f},{1:.2f})".format(x,y)
- x= (other.y_intersect()-self.y_intersect()) / (self.slope()-other.slope())
- y= self.slope()*x+self.y_intersect()
- return "({0:.2f},{1:.2f})".format(x,y)
- def not_enough_data(i):
- return print("not enough data in line {0}".format(i+1))
- def coordinate_y(i):
- return print("Line {0} error: y coordinate must be a number".format(i+1))
- def coordinate_x(i):
- return print("Line {0} error: x coordinate must be a number".format(i+1))
- def equal_exception():
- return print("Equal lines. This is illegal")
- def check(m):
- new_lst_check=[]
- for i in range(len(m)):
- element=m[i]
- element=element.split()
- if len(element) != 4:
- not_enough_data(i)
- return new_lst_check
- for x in range(len(element)):
- if element[x].isalpha() and x%2==0:
- coordinate_x(i)
- return new_lst_check
- # raise ValueError("x coordinate must be number")
- if element[x].isalpha() and x%2 !=0:
- coordinate_y(i)
- return new_lst_check
- # raise ValueError("y coordinate must be number")
- element[x]=float(element[x])
- new_lst_check.append(element)
- return new_lst_check
- def is_equal(lineslist):
- is_equal_list=[]
- a=""
- for i in range(len(lineslist)):
- z=0
- while z<len(lineslist):
- if z==i:
- z+=1
- else:
- if lineslist[i].equals(lineslist[z])==True:
- is_equal_list.append(lineslist[i])
- z+=1
- a+=str(i)+" "
- else:
- z+=1
- return is_equal_list,a
- with open("input6.txt") as doc:
- m=doc.read().splitlines()
- new_m=check(m)
- lineslist=[]
- for block in new_m:
- p1=Point(float(block[0]),float(block[1]))
- p2=Point(float(block[2]),float(block[3]))
- lineslist.append(line(p1,p2))
- equal , nums = is_equal(lineslist);nums=nums.split();nums=[int(i) for i in nums]
- if len(equal)>0:
- equal_exception()
- lineslist=lineslist[0:nums[-1]+1]
- print("Lines:")
- for i in range(len(lineslist)):
- z=0
- a=""
- while z<len(lineslist):
- if i==z:
- a+=""
- z+=1
- else:
- if lineslist[i].parallel(lineslist[z])==True:
- a+=" "+"parallel to line {0}".format(z+1)
- z+=1
- else:
- z+=1
- print("line {0}: {1} {2}".format(i+1,lineslist[i],a))
- xdxd=[]
- for i in lineslist:
- for j in lineslist:
- if i.intersection(j)==None:
- continue
- xdxd.append(i.intersection(j))
- xdxd=list(set(xdxd))
- if len(xdxd)==0:
- print("no intersections")
- else:
- print("Intersection points:")
- for i in xdxd:
- print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement