编写一个叫做distance_between_points的函数,它接受两个Point作为参数,然后返回这两个点之间的距离。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| import math
class Point:
"""表示一个二维的点
"""
def distance_between_points(p1,p2):
return math.sqrt((p1.x - p2.x)**2+(p1.y - p2.y)**2)
Po1 = Point()
Po2 = Point()
Po1.x = 5
Po1.y = 3
Po2.x = 6
Po2.y = 2
distance_between_points(Po1,Po2)
|
编写一个叫做move_rectangle的函数,接受一个Rectangle以及两个数字dx和dy。它把corner的x坐标加上dx,把corner的y坐标加上dy,从而改变矩形的位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| def print_point(p):
print('(%g, %g)' % (p.x, p.y))
class Point:
"""表示一个二维的点
"""
class Rectangle:
"""Represents a rectangle.
attributes: width, height, corner."""
def move_rectangle(rect,dx,dy):
rect.corner.x += dx
rect.corner.y += dy
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0
move_rectangle(box,20,30)
print_point(box.corner)
|