Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute Python-3 Online

import math

def distance(p1, p2):
    d = math.sqrt( ((p1[0]-p2[0])**2) + ((p1[1]-p2[1])**2) )
    return d
    
def triclosestdistance(p1, p2, p3):
    d1 = math.sqrt( ((p1[0]-p2[0])**2) + ((p1[1]-p2[1])**2) )
    
    d2 = math.sqrt( ((p1[0]-p3[0])**2) + ((p1[1]-p3[1])**2) )
   
    d3 = math.sqrt( ((p3[0]-p2[0])**2) + ((p3[1]-p2[1])**2) )
    
    if (d1 >= d2) and (d1 >= d3):
        largest = d1
    elif (d2 >= d1) and (d2 >= d3):
        largest = d2
    else:
        largest = d3
    return largest
    
def trishortestpath(p1, p2, p3):
    d1 = math.sqrt( ((p1[0]-p2[0])**2) + ((p1[1]-p2[1])**2) ) #1 to 2
    
    d2 = math.sqrt( ((p1[0]-p3[0])**2) + ((p1[1]-p3[1])**2) ) #1 to 3
  
    d3 = math.sqrt( ((p3[0]-p2[0])**2) + ((p3[1]-p2[1])**2) ) #2 to 3
    
    path1 = d1 + d3 # 1,2,3
    path2 = d2 + d3 # 1,3,2
    path3 = d1 + d2 # 2,1,3
    path4 = d3 + d2 # 2,3,1
    path5 = d2 + d1 # 3,1,2
    path6 = d3 + d1 # 3,2,1
    
    if (path1 <= path2) and (path1 <= path3) and (path1 <= path4) and (path1 <= path5) and (path1 <= path6):
        largest = "1 to 2 to 3 is (one of) the shortest: " + str(path1)
    elif (path2 <= path1) and (path2 <= path3) and (path2 <= path4) and (path2 <= path5) and (path2 <= path6):
        largest = "1 to 3 to 2 is (one of) the shortest: " + str(path2)
    elif (path3 <= path1) and (path3 <= path2) and (path3 <= path4) and (path3 <= path5) and (path3 <= path6):
        largest = "2 to 1 to 3 is (one of) the shortest: " + str(path3)
    elif (path4 <= path1) and (path4 <= path2) and (path4 <= path3) and (path4 <= path5) and (path4 <= path6):
        largest = "2 to 3 to 1 is (one of) the shortest: " + str(path4)
    elif (path5 <= path1) and (path5 <= path2) and (path5 <= path3) and (path5 <= path4) and (path5 <= path6):
         largest = "3 to 1 to 2 is (one of) the shortest: " + str(path5)
    else:
        largest = "3 to 2 to 1 is (one of) the shortest: " + str(path6)
    return largest
    
    
x = [324, 100]
y = [1341, 31]
z = [-13243410,3.5]
print (trishortestpath(x,y,z))






















Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.