Finding the centre of multiple lines using least s

2020-04-30 02:03发布

问题:

I have a series of lines which roughly (but not exactly) intersect at some point.

I need to find the point which minimises the distance between each line in the centre. I have been trying to follow this methodology:

Nearest point to intersecting lines in 2D

When I create my script in Python to perform this function I get the incorrect answer:

Here is my code, I was wondering if anyone could suggest what I am doing wrong? Or an easier way of going about this. Each line is defined by two points x1 and x2.

def directionalv(x1,x2):
    point1=np.array(x1) #point1 and point2 define my line
    point2=np.array(x2)
    ortho= np.array([[0,-1],[1,0]]) #see wikipedia article
    subtract=point2-point1
    length=np.linalg.norm(subtract)
    fraction = np.divide(subtract,length)
    n1=ortho.dot(fraction)
    num1=n1.dot(n1.transpose())
    num = num1*(point1)
    denom=n1.dot(n1.transpose())
    return [num,denom]           

n1l1=directionalv(x1,x2) 
n1l2=directionalv(x3,x4)
n1l3=directionalv(x5,x6)
n1l4=directionalv(x7,x8)
n1l5=directionalv(x9,x10)

numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0]  #sum of (n.n^t)pi from wikipedia article
denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t
point=(numerall/denomall)

My points are as follows Line1 consists of points x1= [615, 396] and x2 = [616, 880]

Line 2, x3 = [799, 449] x4= [449, 799]

Line 3, x5 = [396, 637] x6 = [880, 636]

Line 4, x7 = [618, 396] x8 = [618, 880]

Line 5, x9 = [483, 456] x10 = [777, 875]

Any help would be really appreciated!

Thank you for your time.

回答1:

Could it simply be the fact that you should define in Python the matrix as 2 vectors (understand is a column of the matrix, not row! see: How to define two-dimensional array in python ), you'll then should define the ortho matrix like this:

    ortho= np.array([[0,1],[-1,0]])

Otherwise, what does the following means?

    numerall=n1l1[0]+n1l2[0]+n1l3[0]+n1l4[0]+n1l5[0]  #sum of (n.n^t)pi from wikipedia article
    denomall=n1l1[1]+n1l2[1]+n1l3[1]+n1l4[1]+n1l5[1] #sum of n.n^t        
    point=(numerall/denomall)

I do not understand your interpretation of the transposition of a Matrix; and the inverse of a matrix does not equals to a division.

Use an existing Python library like Numpy to do the computing instead of implementing it yourself. See: https://docs.scipy.org/doc/numpy-1.10.4/reference/generated/numpy.matrix.html