Základný problém tkvie v tom že v programe boli vytvorené 2 classes, a metódy z prvej sú použité aj v druhej. Z vysvetlenia som pochopil že tie metódy sú určené LEN pre daný class a tým to končí, je to teda tak alebo nie? V kóde jasne vidno ako metódy z class CircularDomain (random_pos/normal/inside) sú použité v class Ball.
Druhý problém je vlastne o tom istom, nechápem syntax volania tých domén. Napr. prečo v "self.domain.random_pos(self.radius)" je použité na začiatku self.domain? Rovnako pri "self.domain.normal(self.pos)".
To je všetko nepochopené, nie že by toho bolo málo
class 1
Spoiler
Kód: Vybrať všetko
class CircularDomain:
def __init__(self, center, radius):
self.center = center
self.radius = radius
self.border = 2
# return if bounding circle is inside the domain
def inside(self, center, radius):
dx = center[0] - self.center[0]
dy = center[1] - self.center[1]
dr = math.sqrt(dx ** 2 + dy ** 2)
return dr < (self.radius - radius - self.border)
# return a unit normal to the domain boundary point nearest center
def normal(self, center):
dx = center[0] - self.center[0]
dy = center[1] - self.center[1]
dr = math.sqrt(dx ** 2 + dy ** 2)
return [dx / dr, dy / dr]
# return random location
def random_pos(self, radius):
r = random.random() * (self.radius - radius - self.border)
theta = random.random() * 2 * math.pi
x = r * math.cos(theta) + self.center[0]
y = r * math.sin(theta) + self.center[1]
return [x, y]
# Draw boundary of domain
def draw(self, canvas):
canvas.draw_circle(self.center, self.radius, self.border*2, "Red")
Spoiler
Kód: Vybrať všetko
class Ball:
def __init__(self, radius, color, domain):
self.radius = radius
self.color = color
self.domain = domain
self.pos = self.domain.random_pos(self.radius) # problem 01
self.vel = [random.random() + .1, random.random() + .1]
# bounce
def reflect(self):
norm = self.domain.normal(self.pos)
norm_length = dot(self.vel, norm)
self.vel[0] = self.vel[0] - 2 * norm_length * norm[0]
self.vel[1] = self.vel[1] - 2 * norm_length * norm[1]
# update ball position
def update(self):
self.pos[0] += self.vel[0]
self.pos[1] += self.vel[1]
if not self.domain.inside(self.pos, self.radius): # problem 02
self.reflect()
# draw
def draw(self, canvas):
canvas.draw_circle(self.pos, self.radius, 1,
self.color, self.color)