Home > other >  How to calculate the proportional percentage of a point of an x,y coordinate with respect to a line
How to calculate the proportional percentage of a point of an x,y coordinate with respect to a line

Time:04-28

I need to know the percentage of the line trajectory(red line) that my point(blue dot) is on. In an abstraction, try to think of a street, regardless of its length, the beginning is always 0% and the end is always 100% of this street. If I happen to stop anywhere on this street, how many percent of this street have I walked.

So taking (X1, Y1) as the "Starting point" and (X2 and Y2) as the "Ending point" of the line (it can be straight lines or diagonals at any angle), how to get the percentage "traversed" by the point on the same line. All the points are actually the mouse coodinates The first click sets the beginning of the line, the second click sets the end of the line and the third click sets the point in which I want to know the value of it's percentage between the beginning and the end of the line

Not straight lines

Overview of what I mean

I've done this so far by watching some Python videos

from tkinter import *
clique = 0
X1=0
Y1=0
X2=0
Y2=0
XF=0
YF=0
ready = False   
def getCoord(event):
    global clique
    global X1
    global Y1
    global X2
    global Y2
    global XF
    global YF

    if clique == 0:
        myLabel['text'] = f'Starting point: x = {event.x} y= {event.y}'
        clique=1
        X1 = event.x
        Y1 = event.y
        return
    if clique == 1:
        myLabel2['text'] = f'Ending point: x = {event.x} y= {event.y}'
        clique=2
        X2 = event.x
        Y2 = event.y
        drawLine()
        return
    else:
        myLabel3['text'] = f'Dot point: x = {event.x} y= {event.y}'
        XF=event.x
        YF=event.y
        drawDot()
        clique = 0
        print(str(calculate()))
        return

def drawDot():
    myCanvas.create_rectangle(XF, YF, XF, YF, fill='blue', width=4, outline='blue')
def drawLine():
    myCanvas.create_line(X1,Y1,X2,Y2, fill='red', width=5)
        
def calculate():
  
    return "I have 0 ideas"

myWindow = Tk()
myCanvas = Canvas(myWindow, width=1270,height=720,background='black')
myLabel = Label(bd=2, relief='solid', font='Times 22 bold', bg='white', fg='blue')
myLabel2 = Label(bd=2, relief='solid', font='Times 22 bold', bg='white', fg='blue')
myLabel3 = Label(bd=2, relief='solid', font='Times 22 bold', bg='white', fg='blue')
myCanvas.bind('<Button-1>', getCoord)
myCanvas.grid(row=0,column=0)
myLabel.grid(row=1, column=0)
myLabel2.grid(row=2, column=0)
myLabel3.grid(row=3, column=0)
myWindow.mainloop()```

CodePudding user response:

The mathematical derivation of the equation to find the "percentage" is explained in this answer.

Given the two points describing the line P1=(X1,Y1) and P2=(X2,Y2), and a third point PF=(XF,YF) that resides on the line, you can calculate t, a value between 0 and 1 representing the traversal of PF on the vector P1->P2 as follows:

def calculate():
    t = (XF - X1) / (X2 - X1)

OR:

def calculate():
    t = (YF - Y1) / (Y2 - Y1)

You can use either X or Y. If the point PF is actually on the line, both of them will give the same t.

Now, there is a special case when the line is vertical or horizontal, we may end up dividing by zero. So, the best way is use the equation with larger denominator. That is, if (X2-X1) is larger than (Y2-Y1), we will use the first equation to divide by a larger number.

So, the final code will look like this:

def calculate():
    dx = X2-X1
    dy = Y2-Y1
    if dx>dy:
        t = (XF - X1) / dx
    else:
        t = (YF - Y1) / dy

This will work on all cases. You can multiply t by 100 to get the percentage (between 0% and 100%)

Now, if the point PF is outside the line, the two equations (using X or Y) may produce different values of t. Also, if the point is on the "extended" line, you may get a value of t that is less than zero or great than one.

  • Related