此代码需要获取由我输入的2个圆的x,y和半径,并查看它是否发生碰撞。我对碰撞和距离方法的圆类部分感到困惑。我已经输入了公式,我认为找到距离是正确的,但我不知道如何调用c1和c2坐标,以便我知道的部分是错误的。至于碰撞,我打算写一个if语句,检查c1和c2半径之和是否等于或大于距离,我也不知道怎么做。所以,如果有人能提供帮助,我们非常感激。我的教授还给了我们主要的课程部分,我不明白为什么他在那里放了一个for循环所以我不知道那个循环是什么
class Shape:
"""Shape class: has methods move(), location(), and __init__().
Complete the location() method."""
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
self.x = self.x + deltaX
self.y = self.y + deltaY
def location(self):
'''Returns a tuple containing the ~x,y~ coordinates of an object.
return "Circle at coordinates (%d, %d)"\ % (self.x, self.y)
pass
class Circle(Shape):
"""Circle is a sub-class of shape and inherits the move() and location() methods."""
pi = 3.14159
def __init__(self, x=0, y=0, r=1):
Shape.__init__(self, x, y)
self.radius = r
def area(self):
return self.radius * self.radius * self.pi
def __str__(self):
return "Circle of radius %s at coordinates (%d, %d)"\ % (self.radius, self.x, self.y)
# Class methods
@classmethod
def is_collision(Circle, c1, c2):
'''Return True or None'''
if c1.r + c2.r >= dist:
pass # fill this in
else:
not
@classmethod
def distance(Circle, c1, c2):
"""calculate distance between two circles"""
dist = math.sqrt(((c1.x-c2.x)**2)+((c1.y-c2.y)**2))
pass
from shape import Shape
from circle import Circle
c1 = Circle(100, 100, 100)
c2 = Circle(150, 150, 100)
c1_xdelta = 2
c1_ydelta = 3
c2_xdelta = 1
c2_ydelta = -1
for i in range(1,20):
c1.move(c1_xdelta, c1_ydelta)
c2.move(c2_xdelta, c2_ydelta)
# Print c1.__str__()
print(c1)
# Print c2.__str__()
print(c2)
# Print collision True or None
print("Collision: {collision}".format(collision = Circle.is_collision())
示例输出:
~Circle~ 1: Circle: x,y; coordinates: 0, 0; radius: 5.
~Circle~ 2: Circle: x,y; coordinates: 0, 0; radius: 5.
Collision: True
~Circle~ 1: Circle: x,y; coordinates: 16, 16; radius: 5.
~Circle~ 2: Circle: x,y; coordinates: 16, 136; radius: 5.
Collision: None
答案 0 :(得分:0)
在更正collision()
和distance()
方法后,您的代码应如下所示:
import math
class Shape:
"""Shape class: has methods move(), location(), and __init__()."""
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
self.x = self.x + deltaX
self.y = self.y + deltaY
def location(self):
return "Circle at coordinates (%d, %d)" % (self.x, self.y)
class Circle(Shape):
"""Circle is a sub-class of shape and inherits the move() and location() methods."""
pi = 3.14159
def __init__(self, x=0, y=0, r=1):
Shape.__init__(self, x, y)
self.radius = r
def area(self):
return self.radius * self.radius * self.pi
def __str__(self):
return "Circle of radius %s at coordinates (%d, %d)" % (self.radius, self.x, self.y)
# Class methods
@classmethod
def is_collision(Circle, c1, c2):
'''Return True or False'''
return c1.radius + c2.radius >= Circle.distance(c1, c2)
@classmethod
def distance(Circle, c1, c2):
"""calculate distance between two circles"""
return math.sqrt(((c1.x-c2.x)**2)+((c1.y-c2.y)**2))
使用示例:
from circle import Circle
c1 = Circle(0, 0, 5)
c2 = Circle(0, 0, 5)
print(c1)
print(c2)
print("Collision: {collision}".format(collision = Circle.is_collision(c1, c2)))
c1.move(16, 16)
c2.move(16, 136)
print(c1)
print(c2)
print("Collision: {}".format(Circle.is_collision(c1, c2)))
示例输出:
Circle of radius 5 at coordinates (0, 0)
Circle of radius 5 at coordinates (0, 0)
Collision: True
Circle of radius 5 at coordinates (16, 16)
Circle of radius 5 at coordinates (16, 136)
Collision: False