from __future__ import print_function, division
classPoint:"""Represents a point in 2-D space.
attributes: x, y
"""defprint_point(p):"""Print a Point object in human-readable format."""print('(%g, %g)'%(p.x, p.y))classRectangle:"""Represents a rectangle.
attributes: width, height, corner.
"""deffind_center(rect):"""Returns a Point at the center of a Rectangle.
rect: Rectangle
returns: new Point
"""
p = Point()
p.x = rect.corner.x + rect.width/2.0
p.y = rect.corner.y + rect.height/2.0return p
defgrow_rectangle(rect, dwidth, dheight):"""Modifies the Rectangle by adding to its width and height.
rect: Rectangle object.
dwidth: change in width (can be negative).
dheight: change in height (can be negative).
"""
rect.width += dwidth
rect.height += dheight
defmain():
blank = Point()
blank.x =3
blank.y =4print('blank', end=' ')
print_point(blank)
box = Rectangle()
box.width =100.0
box.height =200.0
box.corner = Point()
box.corner.x =0.0
box.corner.y =0.0
center = find_center(box)print('center', end=' ')
print_point(center)print(box.width)print(box.height)print('grow')
grow_rectangle(box,50,100)print(box.width)print(box.height)if __name__ =='__main__':
main()
15章内的练习
from __future__ import print_function, division
import copy
import math
from Point1 import Point, Rectangle
defdistance_between_points(p1, p2):"""Computes the distance between two Point objects.
p1: Point
p2: Point
returns: float
"""
dx = p1.x - p2.x
dy = p1.y - p2.y
dist = math.sqrt(dx**2+ dy**2)return dist
defmove_rectangle(rect, dx, dy):"""Move the Rectangle by modifying its corner object.
rect: Rectangle object.
dx: change in x coordinate (can be negative).
dy: change in y coordinate (can be negative).
"""print("****")
rect.corner.x += dx #改变中心点xprint("&&&&")
rect.corner.y += dy #改变中心点yprint("^^^^^^^^^")defmove_rectangle_copy(rect, dx, dy):"""Move the Rectangle and return a new Rectangle object.
rect: Rectangle object.
dx: change in x coordinate (can be negative).
dy: change in y coordinate (can be negative).
returns: new Rectangle
"""
new = copy.deepcopy(rect)#复制一个对象print("@@@@@@@@@@")
move_rectangle(new, dx, dy)# 改变复制对象的中心点print("!!!!!")return new # 返回改变中心点后的新复制的对象defmain():
blank = Point()
blank.x =0
blank.y =0
grosse = Point()
grosse.x =3
grosse.y =4print('distance', end=' ')print(distance_between_points(grosse, blank))
box = Rectangle()
box.width =100.0
box.height =200.0
box.corner = Point()
box.corner.x =50.0
box.corner.y =50.0print(box.corner.x)print(box.corner.y)print('move')
move_rectangle(box,50,100)print(box.corner.x)print(box.corner.y)
new_box = move_rectangle_copy(box,50,100)print("############")print(new_box.corner.x,end=" ")print(new_box.corner.y)if __name__ =='__main__':
main()
练习15.1
from __future__ import print_function, division
import copy
from Point1 import Point, Rectangle, print_point
from Point1_soln import distance_between_points
classCircle:"""Represents a circle.
Attributes: center, radius
"""defpoint_in_circle(point, circle):"""Checks whether a point lies inside a circle (or on the boundary).
point: Point object
circle: Circle object
"""
d = distance_between_points(point, circle.center)print(d)return d <= circle.radius
defrect_in_circle(rect, circle):"""Checks whether the corners of a rect fall in/on a circle.
rect: Rectangle object
circle: Circle object
Whether the Rectangle lies entirely in or on the boundary of the circle."""
p = copy.copy(rect.corner)# print("##")
print_point(p)ifnot point_in_circle(p, circle):returnFalse
p.x += rect.width
print("##")
print_point(p)ifnot point_in_circle(p, circle):returnFalse
p.y -= rect.height
print_point(p)ifnot point_in_circle(p, circle):returnFalse
p.x -= rect.width
print_point(p)ifnot point_in_circle(p, circle):returnFalsereturnTruedefrect_circle_overlap(rect, circle):"""Checks whether any corners of a rect fall in/on a circle.
rect: Rectangle object
circle: Circle object
"""
p = copy.copy(rect.corner)
print_point(p)if point_in_circle(p, circle):returnTrue
p.x += rect.width
print_point(p)if point_in_circle(p, circle):returnTrue
p.y -= rect.height
print_point(p)if point_in_circle(p, circle):returnTrue
p.x -= rect.width
print_point(p)if point_in_circle(p, circle):returnTruereturnFalsedefmain():
box = Rectangle()
box.width =100.0
box.height =200.0
box.corner = Point()
box.corner.x =120.0
box.corner.y =50.0print(box.corner.x)print(box.corner.y)
circle = Circle()
circle.center = Point()
circle.center.x =150.0
circle.center.y =100.0
circle.radius =70.0print(circle.center.x)print(circle.center.y)print(circle.radius)print('================================')print(point_in_circle(box.corner, circle))print('================================')print(rect_in_circle(box, circle))print('================================')print(rect_circle_overlap(box, circle))if __name__ =='__main__':
main()
from __future__ import print_function, divisionclass Point: """Represents a point in 2-D space. attributes: x, y """def print_point(p): """Print a Point object in human-readable form...