c.编写一个函数 point_in_circle,接收一个 Circle 对象和一个 Point 对象,当 Point 位于圆内或圆边上时,返回为 True,否则返回 False。
时间: 2024-02-22 13:57:48 浏览: 15
好的,以下是 Python 代码实现:
```python
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Circle:
def __init__(self, center, radius):
self.center = center
self.radius = radius
def point_in_circle(circle, point):
distance = math.sqrt((point.x - circle.center.x)**2 + (point.y - circle.center.y)**2)
if distance <= circle.radius:
return True
else:
return False
```
使用示例:
```python
center = Point(0, 0)
circle = Circle(center, 5)
point1 = Point(3, 4) # 在圆内部
point2 = Point(5, 0) # 在圆边上
point3 = Point(6, 0) # 在圆外部
print(point_in_circle(circle, point1)) # 输出 True
print(point_in_circle(circle, point2)) # 输出 True
print(point_in_circle(circle, point3)) # 输出 False
```