1

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

enter image description here

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:

enter image description here

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.

bateman
  • 61
  • 1
  • 6

1 Answers1

0

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

Loic Mouchard
  • 1,121
  • 7
  • 22
  • I understand that the transpose does not equal the division however this does not change the output of the code. Numerall is the numerator of the last fraction given in the Wikipedia article I think.? i.e the sum of (ni.ni^t)pi from the article.same with denomall. Thanks for your time. – bateman Oct 04 '17 at 11:30
  • Bateman, There are inverse matrix (-1) and transpose matrix(T). Where do you see a fraction in the formula ? https://wikimedia.org/api/rest_v1/media/math/render/svg/0cdfe02b31bc4747ea5fee71f9ee7a05fab8730c – Loic Mouchard Oct 04 '17 at 11:43
  • Okay I see your point. If I change point to: point = (np.linalg.inv(denomall)).dot(numerall) then I get an error because my denomall value is not two dimensional. – bateman Oct 04 '17 at 11:53
  • If I try to perform the inverse to denomall I achieve an error because denomll is not a 2 dimensional array. Which implies that this part of my script is incorrect. – bateman Oct 04 '17 at 11:59
  • Advice: in your function `directionalv`, only compute the `ni` vector. Then, you can apply the formula with the sums. – Loic Mouchard Oct 04 '17 at 12:16