0

I writing a python program in which a circle bounces off of user drawn lines. There are multiple circles that bounce off the wall. For each one, the shortest distance from the center of the circle and the ball should be calculated. I would prefer if this code was very efficient because my current algorithm lags the computer a lot. If point a is the starting point ,and point b is the end point, and point c is the center, and r is the radius, how would I calculate the shortest distance between the ball? This algorithm should also work if the X coordinate of the ball is out of range of x coordinates in segment AB.

Please post python code

Any help would be appreciated!

Here's what I have so far:

lineList is a list with 4 values that contains beginning and end coordinates of the user drawn lines

center is the center of the ball

global lineList, numobjects
        if not(0 in lineList):

            beginCoord = [lineList[0],lineList[1]]

            endCoord = [lineList[2]-500,lineList[3]-500]

            center = [xCoordinate[i],yCoordinate[i]+15]

            distance1 = math.sqrt((lineList[1] - center[1])**2 + (lineList[0] - center[0])**2)

            slope1 = math.tan((lineList[1] - lineList[3]) / (lineList[0] - lineList[2]))

            try:
                slope2 = math.tan((center[1] - beginCoord[1])/(center[0]-beginCoord[0]))

                angle1 = slope2 + slope1

                circleDistance = distance1 * math.sin(angle1)

            except:

                #If the circle is directly above beginCoord
                circleDistance = center[1] - lineList[1]


            global numbounces

            if circleDistance < 2 and circleDistance > -2:

                print(circleDistance)

                b = False

                b2=False

                if xCoordinate[i] < 0:

                    xCoordinate[i] += 1

                    speed1[i] *= -1

                    b=True


                elif xCoordinate[i] > 0:

                    xCoordinate[i] -= 1

                    speed1[i] *= -1

                    b=True


                if yCoordinate[i] < 0:

                    yCoordinate[i] += 1

                    speed2[i] *= -1

                    b2=True


                elif yCoordinate[i] > 0:

                    yCoordinate[i] -= 1

                    speed2[i] *= -1

                    b2=True


                if b and b2:

                 #Only delete the line if the ball reversed directions

                    numbounces += 1

                    #Add a ball after 5 bounces

                    if numbounces % 5 == 0 and numbounces != 0:

                        numobjects = 1

                        getData(numobjects)
                    canvas.delete("line")

                    lineList = [0,0,0,0]

Diagram of circles

1 Answer 1

1

I don't know what is the mean of shortest distance between the ball, but if you want to calculation the point where the circle will contact the line you can use sympy to figure the formula:

from sympy import *
from sympy.geometry import *
x1, y1, x2, y2, xc, yc = symbols("x1,y1,x2,y2,xc,yc")
p1 = Point(x1, y1)
p2 = Point(x2, y2)
pc = Point(xc, yc)

line = Line(p1, p2)
pline = line.perpendicular_line(pc)
p = line.intersection(pline)[0]
cse(p, symbols=numbered_symbols("t"))

the output is :

([(t0, x1 - x2), (t1, y1 - y2), (t2, x1*y2 - x2*y1), (t3, t0**2 + t1**2)],
 [Point((t0**2*xc + t0*t1*yc - t1*t2)/t3, (t0*t1*xc + t0*t2 + t1**2*yc)/t3)])

this means that you can calculate the perpendicular point as:

t0 = x1 - x2
t1 = y1 - y2
t2 = x1*y2 - x2*y1
t3 = t0**2 + t1**2

xp = (t0**2*xc + t0*t1*yc - t1*t2)/t3
yp = (t0*t1*xc + t0*t2 + t1**2*yc)/t3    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.