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

Python海龟宝典含200多个原创的用turtle模块制作的创意程序,原名《Python趣味编程200例》。准备参加全国创意编程与智能设计大赛的同学们可以用来做参考。

作业描述:
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()

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 留学生Python作业与答案Point类和Line类

李兴球Python微信公众号文章列表

Python游戏海龟模块教程说明书与案例若干免费发放

爱的纪念_Python创意情景动画源代码解析

少儿Python编程到底学些什么?这些代码或许回答了问题.

Python编程家长会花絮_萍乡中小学Python家长会现场

火星路上等着你_少儿从小学什么最好呢?

国家大力整顿教育培训机构,Scratch或Python少儿编程还有得教吗?

鸿蒙系统支持Python开发_可视化编程特别兴趣小组

Scratch作品转Python作品_小猴接桃

python海龟数据可视化。第七次全国人口普查历年数据图表

你的孩子Python编程学到哪个阶段了?给孩子报编程的家长,务必仔细一读。

五一神女来对话,看看她们聊什么?赠Python教案等。

五一快乐有大礼,告诉大家我是如何上Python课的。

Python名堂多,趣味到处有,劈开机械手,帧帧是图片。速算达人之猫狮大战正在进行。 逐字动画不独享,自动生成皆有它。2行代码自动生成字幕gif动画。 Python之潮来临,我在安源区教师科技创新能力的Python讲座

小心你的Python程序,它会是你的一面镜子。小方块闯迷宫.py源代码简析。送Scratch算法集。?

铃儿响钉铛_音效怎能忘_Python配音之Pygame混音器

人面桃花相映红_winsound模块简介

《Python昨晚我想你了》_开源的游戏海龟模块实例案例浅析

《八猫联动初体验》_来自游戏海龟模块的问候

喜爱春天的人儿啊 心地纯洁的人_Python逐行像素显示

旋转之三叶炫彩扇_蟒蛇与海龟的表演

彩虹欢迎字幕_三模合体滚图形

《Python海龟宝典》简介

100%错误的算法还在用,明明没有错别字,说我有11个错别字

奇怪的Python代码,谁能帮我解释一下??

人造地球系统让人类文明充满整个宇宙之Python32768版

深夜,是什么把你的大脑搞成一团浆糊!再谈少儿编程!

5线城市萍乡的少儿Python寒假班学的是什么内容?

关于纯少儿编程课程进化的自然选择

Python海龟画图经典作品_国庆中秋双重喜庆源代码免费下载

海龟为什么要自杀!turtle制作游戏秘籍之一

朋友,你是否知道我在仰望着你_Python神笔马良案例集

酷酷的爆炸效果_Python海龟画图不仅仅是画图

虫子满屏爬_三bug多线程示例程序浅析 少儿Python视频课程A级简介

给的gif图片加文字水印_拆帧与合帧(免费下载180个Python创意源码

用Python制作酷炫图形之如意金箍棒_颜色增加模块应用

简单的用Python做酷炫图形与动画

sb3转exe,sb3素材提取器,编程小子apk, 未公开的pygame游戏集/scratch/python少儿编程免费下载集合

夜幕下的霓虹

学本领,探索更大的世界!

李兴球博客 风火轮编程主页
error: Content is protected !!