On 2017-12-23 19:44, G Yu wrote:
My program has two circles: one stationary circle, drawn at a random location;
and one moving circle, consistently drawn in the same place in the graphics
window.
The moving circle moves towards the stationary one. However, when the moving
circle hits the stationary one (when the x-coordinates of the circles' centers
are equal), I want the moving circle to stop moving and then disappear.
I tried the command acircle.getCenter() to determine when the centers are equal, but it
doesn't work; I suspect because the centers are never truly equal, and only come within
something like .000001 pixels of each other. So to account for the approximation, I used
the math function "isclose":
move_moving_circle = True
while move_moving_circle:
moving_circle.move(P_to_R/P_to_E, E_to_R/P_to_E)
time.sleep(0.01)
if isclose(moving_circle.getCenter(), stationary_circle.getCenter(),
rel_tol=1e-4, abs_tol=0.0):
move_moving_circle= False
else:
move_moving_circle = True
But this doesn't work. Apparently isclose can't be applied to points - only
floating-point decimals.
So now, I want to convert the output of "acircle.getCenter()" to Cartesian
(x,y) coordinates for both circles. Then I can use the two circle centers' x-coordinates
in the if statement.
Currently, acircle.getCenter() outputs this:
<graphics.Point object at 0x0000013E0D263668>
<graphics.Point object at 0x0000013E0D263B00>
I don't understand/can't use the format that the locations are printed in.
How do I convert the above format to "usable" Cartesian coordinates? Or is
there a simpler way to solve this problem?
You didn't say what graphics library you're using, but from a quick
search on the internet I think the answer is to use the .getX and .getY
methods of the Point object.
You could calculate the distance between the 2 points, which is sqrt((x1
- x2) ** 2 + (y1 - y2) ** 2). You can speed it up a little by omitting
the sqrt and just remember that you're working with the square of the
distance.
Another point: why do they need to be so close to each other?
Personally, I'd just say they collide when round(distance) < 1 or
round(distance ** 2) < 1.
--
https://mail.python.org/mailman/listinfo/python-list