• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jan 28th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """
3. ID: 209020999
4. Assignment no. 7
5. Program: lines.py
6. """
7. def Xerr(x):
8.     if not isinstance(x,float) and not isinstance(x,int):
9.         raise ValueError("x must be a number", x)
10.
11. def Yerr(y):
12.     if not isinstance(y,float) and not isinstance(y,int):
13.         raise ValueError("y must be a number", y)
14.
15. class Point:
16.     def __init__(self,x,y):
17.         Xerr(x)
18.         Yerr(y)
19.         self.__x=x
20.         self.__y=y
21.
22.     def get_x(self):
23.             return self.__x
24.
25.     def get_y(self):
26.             return self.__y
27.
28.     def set_x(self,x):
29.             Xerr(x)
30.             self.__x=x
31.
32.     def set_y(self,y):
33.             Yerr(y)
34.             self.__y=y
35.
36.     def __str__(self):
37.         return "({0:.2f},{1:.2f})".format(self.__x,self.__y)
38.
39.
40. def p1err(p1):
41.     if not isinstance(p1, Point):
42.         raise Exception("must be type Point", p1)
43.
44. def p2err(p2):
45.     if not isinstance(p2, Point):
46.         raise Exception("must be type Point", p2)
47. class line:
48.     def __init__(self,p1,p2):
49.         p1err(p1)
50.         p2err(p2)
51.         self.__p1=p1
52.         self.__p2=p2
53.
54.     def get_p1(self):
55.         return self.__p1
56.
57.     def get_p2(self):
58.         return self.__p2
59.
60.     def set_p1(self,p1):
61.         p1err(p1)
62.         self.__p1=p1
63.
64.     def set_p2(self,p2):
65.         p2err(p2)
66.         self.__p2=p2
67.
68.     def is_vertical(self):
69.         try:
70.             return (self.__p2.get_y() - self.__p1.get_y()) / (self.__p2.get_x() - self.__p1.get_x())
71.         except ZeroDivisionError:
72.             return True
73.
74.     def slope(self):
75.         try:
76.             return (self.__p2.get_y() - self.__p1.get_y()) / (self.__p2.get_x() - self.__p1.get_x())
77.         except ZeroDivisionError:
78.             return None
79.
80.     def y_intersect(self):
81.         m=self.slope()
82.         if m==None:
83.             return None
84.         b=self.__p2.get_y() - m*self.__p2.get_x()
85.         return b
86.
87.     def __str__(self):
88.         t=self.slope()
89.         if t==None:
90.             return "x = {},".format(self.__p2.get_x())
91.         return "y = {0:.2f}x + {1:.2f},".format(t,self.y_intersect())
92.
93.     def parallel(self, other):
94.         if self.slope()==other.slope():
95.             return True
96.         return False
97.
98.     def equals(self, other):
99. #        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():
100.         if self.__str__() == other.__str__():
101.             return True
102.         return False
103.
104.     def intersection(self,other):
105.         if self.parallel(other)==True:
106.             return None
107.         if other.y_intersect()==None:
108.             x=other.get_p1().get_x()
109.             y=self.slope()*x+self.y_intersect()
110.             return "({0:.2f},{1:.2f})".format(x,y)
111.         if self.y_intersect()==None:
112.             x=self.get_p1().get_x()
113.             y=other.slope()*x+other.y_intersect()
114.             return "({0:.2f},{1:.2f})".format(x,y)
115.         x= (other.y_intersect()-self.y_intersect()) / (self.slope()-other.slope())
116.         y= self.slope()*x+self.y_intersect()
117.         return "({0:.2f},{1:.2f})".format(x,y)
118.
119.
120. def not_enough_data(i):
121.     return print("not enough data in line {0}".format(i+1))
122. def coordinate_y(i):
123.     return print("Line {0} error: y coordinate must be a number".format(i+1))
124. def coordinate_x(i):
125.     return print("Line {0} error: x coordinate must be a number".format(i+1))
126. def equal_exception():
127.     return print("Equal lines. This is illegal")
128.
129.
130. def check(m):
131.     new_lst_check=[]
132.     for i in range(len(m)):
133.         element=m[i]
134.         element=element.split()
135.         if len(element) != 4:
136.             not_enough_data(i)
137.             return new_lst_check
138.         for x in range(len(element)):
139.             if element[x].isalpha() and x%2==0:
140.                 coordinate_x(i)
141.                 return new_lst_check
142. #                raise ValueError("x coordinate must be number")
143.             if element[x].isalpha() and x%2 !=0:
144.                 coordinate_y(i)
145.                 return new_lst_check
146. #                raise ValueError("y coordinate must be number")
147.             element[x]=float(element[x])
148.         new_lst_check.append(element)
149.     return new_lst_check
150.
151. def is_equal(lineslist):
152.     is_equal_list=[]
153.     a=""
154.     for i in range(len(lineslist)):
155.         z=0
156.         while z<len(lineslist):
157.             if z==i:
158.                 z+=1
159.             else:
160.                 if lineslist[i].equals(lineslist[z])==True:
161.                     is_equal_list.append(lineslist[i])
162.                     z+=1
163.                     a+=str(i)+" "
164.                 else:
165.                     z+=1
166.     return is_equal_list,a
167.
168.
169. with open("input6.txt") as doc:
171.     new_m=check(m)
172. lineslist=[]
173. for block in new_m:
174.     p1=Point(float(block[0]),float(block[1]))
175.     p2=Point(float(block[2]),float(block[3]))
176.     lineslist.append(line(p1,p2))
177. equal , nums = is_equal(lineslist);nums=nums.split();nums=[int(i) for i in nums]
178. if len(equal)>0:
179.     equal_exception()
180.     lineslist=lineslist[0:nums[-1]+1]
181. print("Lines:")
182. for i in  range(len(lineslist)):
183.     z=0
184.     a=""
185.     while z<len(lineslist):
186.         if i==z:
187.             a+=""
188.             z+=1
189.         else:
190.             if lineslist[i].parallel(lineslist[z])==True:
191.                 a+=" "+"parallel to line {0}".format(z+1)
192.                 z+=1
193.             else:
194.                 z+=1
195.     print("line {0}: {1} {2}".format(i+1,lineslist[i],a))
196. xdxd=[]
197. for i in lineslist:
198.     for j in lineslist:
199.         if i.intersection(j)==None:
200.             continue
201.         xdxd.append(i.intersection(j))
202. xdxd=list(set(xdxd))
203. if len(xdxd)==0:
204.     print("no intersections")
205. else:
206.     print("Intersection points:")
207.     for i in xdxd:
208.         print(i)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top