留学生Python作业与答案Point类和Line类

留学生Python作业与答案Point类和Line类

作业描述:
CSE 30
Programming Abstractions: Python
Programming Assignment 3
In this assignment you will create a class called Lines that will be placed in a file called lines.py. Much of
the work is already done in the template file lines_stub.py, posted at
https://classes.soe.ucsc.edu/cse030/Spring21/Examples/pa3/lines_stub.py
The Line class provides an abstract representation of a line in the ??-?? coordinate plane. Included in the
above stub is the Point class we covered in the geometry.py example, also posted on the class webpage.
Your task is fill in the definitions of the methods (10 in all) belonging to the Line class, and to add one new
method to the Point class.
The Line class represents a line in the plane by maintaining two attributes: a Point object representing a
point on the line, and a numeric value giving its slope. The obvious names for these attributes are point and
slope respectively, which are used in this document, but you can give them any names you like. One logical
problem entailed by this representation is how to represent vertical lines, i.e., lines that have no numerical
value as slope. Such lines are often said to have “infinite slope” in Geometry textbooks. We will handle
this situation by simply setting the slope attribute, normally a number, to be the string ‘infinity’. This
choice will create some special cases in the design of several of the required functions, and it will be your
task to sort out these cases logically.
This project requires some familiarity with basic facts of plane geometry, but nothing more than would be
encountered in a class like MATH 3 (precalculus algebra), a prerequisite for this course. For instance, the
function parallel() in the Line class, when called as ??.parallel(??), returns True if line ?? is parallel to line
??, and False otherwise. But as is well known, two lines are parallel if and only if they have the same slope.
Another example is intersect(), contained in the Line class. The call ??.intersect(??) will return the Point
object representing the point of intersection of line ?? with line ??. This is a common homework problem in
elementary algebra, i.e., find the point of intersection of two lines whose equations are given. You must
solve the general problem: find formulas for the coordinates of the intersection point, and account for the
special cases where the lines are parallel (return None), or where one line is vertical.
The correct output for function main() is included as a comment in the above template. If your function
definitions are correct, the output will match the quoted output exactly. However, main() should not be
considered a complete test of all of the required functions. As always, you should thoroughly test every
logical pathway within every function you design, making sure that it strictly adheres to its specification.
To do this testing, you may alter main() temporarily, but be sure to return it to its original state before you
submit the file.
Once all coding and testing are complete, change the name of the file from lines_stub.py to lines.py and
submit it to Gradescope before the due date. As always, ask questions and get help early.

帮国外留学生做的一个练习,关于面向对象编程的,以下是参考答案:


import math

class Point(object):
   """在x-y平面代表一个点的类.Class representing a Point in the x-y coordinate plane."""

   def __init__(self, x, y):
      """初始化点对象。Initialize a Point object."""
      self.xcoord = x
      self.ycoord = y
   # end

   def __str__(self):
      """返回代表点的字符串表示。Return the string representation of a Point."""
      return '({}, {})'.format(self.xcoord, self.ycoord)
   # end

   def __repr__(self):
      """返回代表点的详细的字符串表示Return the detailed string representation of a Point."""
      return 'geometry.Point({}, {})'.format(self.xcoord, self.ycoord)
   # end

   def __eq__(self, other):
      """
      判断两个点是否相等。Return True if self and other have the same coordinates, False otherwise.
      """
      if not isinstance(self,Point):return False
      if not isinstance(other,Point):return False      
      eqx = (self.xcoord==other.xcoord)
      eqy = (self.ycoord==other.ycoord)
      return eqx and eqy
   # end

   def distance(self, other):
      """返回两点距离。Return the distance between self and other."""
      diffx = self.xcoord - other.xcoord
      diffy = self.ycoord - other.ycoord
      return math.sqrt( diffx**2 + diffy**2 )
   # end

   def norm(self):
      """返回到原点距离。Return the distance from self to the origin (0, 0)."""
      return self.distance(Point(0,0))
   # end

   def midpoint(self, other):
      """返回中间点。Return the midpoint of the line segment from self to other."""
      midx = (self.xcoord + other.xcoord)/2
      midy = (self.ycoord + other.ycoord)/2
      return Point(midx, midy)
   # end

   #---------------------------------------------------------------------------
   #  Fill in the definition of this function, belonging to the Point class.
   #---------------------------------------------------------------------------
   def join(self, other):
      """
      两点确定和条线。If self==other return None. Otherwise return the line passing through 
      self and other. 两点连线
      """
      if self==other:return None
      dy = other.ycoord - self.ycoord
      dx = self.xcoord - self.xcoord
      if dx ==0:
         return Line(self,'infinity')
      else:
         return Line(self,dy/dx)
   # end     

# end


#------------------------------------------------------------------------------
#  Fill in the definitions of each method in the Line class.
#------------------------------------------------------------------------------
class Line(object):
   """在x-y平面中表示线的Line类。Class representing a Line in the x-y coordinate plane."""
   
   def __init__(self, P, m):
      """初始化一根线,P表示点,m表示斜率。Initialize a Line object."""
      self.P = P                  # 线上的一个点
      self.m = m                  # 斜率
      if m=='infinity':           # 如果位子斜率无限大
         self.b = 'infinity'
      else:
         self.b = str(P.ycoord- m * P.xcoord) # 根据y = k*x + b算出在y轴上的截距
         self._b = P.ycoord- m * P.xcoord
      # end

   def __str__(self):
      """Return a string representation of a Line."""
      #return '({}, {})'.format(self.P, self.m)
      return 'Line through ({}, {}) of slope {}'.format(self.P.xcoord,self.P.ycoord,self.m)
   # end

   def __repr__(self):
      """ Return a detailed string representation of a Line."""
      #return 'geometry.Line({}, {})'.format(self.P, self.m)
      return 'lines.Line(point=({}, {}), slope={})'.format(self.P.xcoord,self.P.ycoord,self.m)
   # end

   def __eq__(self, other):
      """
      返回两线是否是同一根线。Return True if self and other are identical Lines, False otherwise.
      """
      if not isinstance(self,Line):return False
      if not isinstance(other,Line):return False
     
      # 第一种情况,都是垂直,第二种情况self垂直x轴,other不垂直x轴,第三种情况self不垂直x轴,other垂直x轴
      if self.m == 'infinity' and other.m == 'infinity' and self.P.xcoord == other.P.xcoord:return True
      if self.m == 'infinity' and  not isinstance(other.m,str):return False
      if not isinstance(self.m,str) and  other.m=='infinity' :return False      
      
      b = self.b == other.b
      m = self.m == other.m
      return b and m
   # end

   def parallel(self, other):
      """
      Return True if self and other are parallel lines, False otherwise.
      """
      if self == other:return False
      if self.m == 'infinity' and  not isinstance(other.m,str):return False
      if not isinstance(self.m,str) and  other.m=='infinity' :return False   
      return self.m == other.m
   # end

   def perpendicular(self, other):
      """
      返回两线是否垂直。Return True if self and other are perpendicular lines, False otherwise.
      """
      if self.m == 'infinity' and other.m == 0 :return True
      if self.m == 0 and other.m == 'infinity': return True
      if self.m * other.m == -1 : return True
      return False
   
   # end

   def contains_point(self, P):
      """
      返回是否包括P点。Return True if self contains point P, False otherwise.
      """
      x = P.xcoord
      y = P.ycoord
      if self.m == 'infinity':              #  如果self垂直x轴
         return self.P.xcoord == x
      else:                                 # 否则不垂直x轴            
         ycor  = self.m * x + self._b       # 根据y = k* x + b算出应该的y值
         return y == ycor
   
   # end

   def intersect(self, other):
      """
      返回交叉点。If self and other are parallel, return None.  Otherwise return their
      Point of intersection.
      """
      # 第一种情况,两者相等
      if self == other:return None
      # 第二种情况是两者都垂直x轴,但平行
      if self.m == 'infinity' and other.m == 'infinity' :return None
      # 第三种情况,斜率都为0
      if self.m==0 and other.m==0 : return None
      # 第四种情况,self垂直x轴,other不垂直x轴
      if self.m == 'infinity' and  not isinstance(other.m,str):
         x = self.P.xcoord
         y = other.m * x + other._b
      # 第五种情况,self是斜的,other垂直于x轴
      if not isinstance(self.m,str) and  other.m=='infinity' :
         x = other.P.xcoord
         y = self.m * x + self._b

      if not isinstance(self.m,str) and  not isinstance(other.m,str):
         x = (other._b - self._b) / (self.m - other.m)
         y = self.m * x + self._b
      return Point(x,y)     
      
   # end

   def parallel_line(self, P):
      """返回通过P点的平行线。Returns the Line through P that is parallel to self."""
      m = self.m
      return Line(P,m)
   # end

   def perpendicular_line(self, P):
      """返回通过P点的垂直线。Returns the Line through P that is perpendicular to self."""
      if self.m=='infinity':
         m = 0
      elif self.m==0:
         m = 'infinity'
      else:
         m = -self.m
      return Line(P,m)
      
   # end

# end


#------------------------------------------------------------------------------
#  Do not change functon main(). Its role is just to test all of the above.
#  Actually you can change it during your own independent testing, but return
#  it to exactly this state before you submit the project.
#------------------------------------------------------------------------------
def main():

   P = Point(1, 3)
   Q = Point(3, 3)
   R = Point(1, 1)
   S = Point(3, 1)
   T = Point(4, 3)
   U = Point(5, 5)
   V = Point(2, 2)
   W = Point(2, 5)
   X = Point(2, -1)

   A = Line(P, -1)
   B = Line(R, 1)
   C = S.join(T) #points_to_line(S, T)
   D = Line(W, 'infinity')               # 垂直线
   E = Line(Q, 0)
   F = C.parallel_line(P)                # 是否平行  

   print()
   print('A =', A)
   print(repr(A))
   print()
   print('B =', B)
   print(repr(B))
   print()
   print('C =', C)
   print(repr(C))
   print()
   print('D =', D)
   print(repr(D))
   print()
   print('E =', E)
   print(repr(E))
   print()
   print('F =', F)
   print(repr(F))

   print()
   print(B.intersect(C)==U)
   print(A.intersect(B)==V)
   print(D.intersect(C)==X)
   print(D.intersect(Line(T,'infinity'))==None)
   print(A.perpendicular(B))
   print(D.perpendicular(E))
   print(A.parallel(B.perpendicular_line(Q)))
   print(A.contains_point(S))
   print(B.contains_point(U))
   print(C.contains_point(X))
   print(F.contains_point(W))

   print()

# end

#------------------------------------------------------------------------------
if __name__=='__main__':

   main()

李兴球

李兴球的博客是Python创意编程原创博客