0
点赞
收藏
分享

微信扫一扫

Britain Student Python答疑「Python一对一学员答疑」

星巢文化 2022-03-17 阅读 101

你好,我是悦创。

一对一学员答疑题目:

实现一个类,命名为 Circle() ,x,y,radius 需要被收集。x 为圆的中心的 x 坐标;y 为圆的中心的 y 坐标;radius 则为圆的半径。实现一个 overlap(other) 函数,other 是 Circle() 的另一个实例。当该圆和 other 重合了或者碰到对方则返回 True,反之返回 False。

涉及公式:

# 实现一个类,命名为Circle(),
# x,y,radius需要被收集。
# x为圆的中心的x坐标;y为圆的中心的y坐标;
# radius则为圆的半径。
# 实现一个overlap(other)函数,other是Circle()的另一个实例。
# 当该圆和other重合了或者碰到对方则返回True,反之返回False
import math
class Circle():
	def __init__(self, x, y, radius):
		self.x = x
		self.y = y
		self.radius = radius
	def overlap(self, other):
		if self.x == other.x and self.y == other.y:
			return True
		min_distance = self.radius + other.radius
		distance = math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
		if distance > min_distance:
			return False
		else:
			return True

circle_one = Circle(3, 4, 4)
circle_two = Circle(5, 12, 5)
r = circle_two.overlap(circle_one)
print(r)

circle_one = Circle(3, 4, 4)
circle_two = Circle(5, 13, 5)
r = circle_two.overlap(circle_one)
print(r)

在这里插入图片描述
https://www.math10.com/en/geometry/geogebra/geogebra.html

举报

相关推荐

0 条评论