Circle line-segment collision detection algorithm?

前端 未结 28 1989
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:38

I have a line from A to B and a circle positioned at C with the radius R.

\"Image\"

What is a good alg

28条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 07:13

    I know it's been a while since this thread was open. From the answer given by chmike and improved by Aqib Mumtaz. They give a good answer but only works for a infinite line as said Aqib. So I add some comparisons to know if the line segment touch the circle, I write it in Python.

    def LineIntersectCircle(c, r, p1, p2):
        #p1 is the first line point
        #p2 is the second line point
        #c is the circle's center
        #r is the circle's radius
    
        p3 = [p1[0]-c[0], p1[1]-c[1]]
        p4 = [p2[0]-c[0], p2[1]-c[1]]
    
        m = (p4[1] - p3[1]) / (p4[0] - p3[0])
        b = p3[1] - m * p3[0]
    
        underRadical = math.pow(r,2)*math.pow(m,2) + math.pow(r,2) - math.pow(b,2)
    
        if (underRadical < 0):
            print("NOT")
        else:
            t1 = (-2*m*b+2*math.sqrt(underRadical)) / (2 * math.pow(m,2) + 2)
            t2 = (-2*m*b-2*math.sqrt(underRadical)) / (2 * math.pow(m,2) + 2)
            i1 = [t1+c[0], m * t1 + b + c[1]]
            i2 = [t2+c[0], m * t2 + b + c[1]]
    
            if p1[0] > p2[0]:                                           #Si el punto 1 es mayor al 2 en X
                if (i1[0] < p1[0]) and (i1[0] > p2[0]):                 #Si el punto iX esta entre 2 y 1 en X
                    if p1[1] > p2[1]:                                   #Si el punto 1 es mayor al 2 en Y
                        if (i1[1] < p1[1]) and (i1[1] > p2[1]):         #Si el punto iy esta entre 2 y 1
                            print("Intersection")
                    if p1[1] < p2[1]:                                   #Si el punto 2 es mayo al 2 en Y
                        if (i1[1] > p1[1]) and (i1[1] < p2[1]):         #Si el punto iy esta entre 1 y 2
                            print("Intersection")
    
            if p1[0] < p2[0]:                                           #Si el punto 2 es mayor al 1 en X
                if (i1[0] > p1[0]) and (i1[0] < p2[0]):                 #Si el punto iX esta entre 1 y 2 en X
                    if p1[1] > p2[1]:                                   #Si el punto 1 es mayor al 2 en Y
                        if (i1[1] < p1[1]) and (i1[1] > p2[1]):         #Si el punto iy esta entre 2 y 1
                            print("Intersection")
                    if p1[1] < p2[1]:                                   #Si el punto 2 es mayo al 2 en Y
                        if (i1[1] > p1[1]) and (i1[1] < p2[1]):         #Si el punto iy esta entre 1 y 2
                            print("Intersection")
    
            if p1[0] > p2[0]:                                           #Si el punto 1 es mayor al 2 en X
                if (i2[0] < p1[0]) and (i2[0] > p2[0]):                 #Si el punto iX esta entre 2 y 1 en X
                    if p1[1] > p2[1]:                                   #Si el punto 1 es mayor al 2 en Y
                        if (i2[1] < p1[1]) and (i2[1] > p2[1]):         #Si el punto iy esta entre 2 y 1
                            print("Intersection")
                    if p1[1] < p2[1]:                                   #Si el punto 2 es mayo al 2 en Y
                        if (i2[1] > p1[1]) and (i2[1] < p2[1]):         #Si el punto iy esta entre 1 y 2
                            print("Intersection")
    
            if p1[0] < p2[0]:                                           #Si el punto 2 es mayor al 1 en X
                if (i2[0] > p1[0]) and (i2[0] < p2[0]):                 #Si el punto iX esta entre 1 y 2 en X
                    if p1[1] > p2[1]:                                   #Si el punto 1 es mayor al 2 en Y
                        if (i2[1] < p1[1]) and (i2[1] > p2[1]):         #Si el punto iy esta entre 2 y 1
                            print("Intersection")
                    if p1[1] < p2[1]:                                   #Si el punto 2 es mayo al 2 en Y
                        if (i2[1] > p1[1]) and (i2[1] < p2[1]):         #Si el punto iy esta entre 1 y 2
                            print("Intersection")
    

提交回复
热议问题