So I'm pretty new to using python and a beginner. I've written a code on how to figure out the length of 2 polylines and now I'm trying to figure how to display the data using canvas... I've tried like a million times and I just can't get it to work. Can somebody help me out?

Heres my code for the polylines:

from math import *

def distance(p1, p2):
	x=[0,0]
	y=[0,0]
	x[0] = float(p1[0])
	x[1] = float(p1[1])
	y[0] = float(p2[0])
	y[1] = float(p2[1])
	i = 0
	
	xx=x[0]-y[0]
	yy=x[1]-y[1]
	d=sqrt(pow(xx,2)+pow(yy,2))
	return d



try: 
    f = open("C:\line1.txt","r")
    txt = f.read()
    data = txt.split('; ')
    f.close()
    f = open("C:\line2.txt","r")
    txt2 = f.read()
    data2 = txt2.split('; ')
    f.close()
    #f.readlines()
    #f.seek(0)
    #f.read()
    #f.write("this is a test")
    f.close()
except:
    print "there are errors"


#file1
i=0
polyline1=[None]*(len(data)-1)
while i < len(polyline1):
        polyline1[i] = data[i].split(',')
        i = i+1

#file2
i=0
polyline2=[None]*(len(data2)-1)
while i < len(polyline2):
        polyline2[i] = data2[i].split(',')
        i = i+1

i=0
distance1 = 0
while i < len(polyline1)-1:
        distance1 += distance(polyline1[i],polyline1[i+1])
        i = i+1

print "Distance of the first polyline is %s" %distance1

i=0
distance2 = 0
while i < len(polyline2)-1:
        distance2 += distance(polyline2[i],polyline2[i+1])
        i = i+1

print "Distance of the second polyline is %s" %distance2

Here are the two polylines I'm doing. They were in text files. Line 1 & Line 2.

Line 1: 1603714.835939442,142625.48838266544; 1603749.4678153452,142620.21243656706; 1603780.3769339535,142607.37201781105; 1603801.475846678,142582.27024446055; 1603830.4767344964,142536.14692804776;

Line 2: 1602514.2066492266,142330.66992144473; 1602521.4127475217,142414.92978276964; 1602520.1146955898,142433.93817959353; 1602501.3840010355,142439.12358761206; 1602371.6780588734,142417.84858870413; 1602351.6610373354,142408.02716448065; 1602334.5180692307,142388.58748627454; 1602331.6999511716,142376.66073128115; 1602334.8067251327,142348.965322732; 1602338.308919772,142323.6111663878; 1602349.0226452332,142314.50124930218; 1602363.9090971674,142310.79584660195; 1602514.2066492266,142330.66992144473;

display the data using canvas

Which GUI toolkit are you using? If it is Tkinter, begin at Effbot for line drawing. Also, the polyline1 & 2 should be calculated by one function that receives the parameters and returns the number, and the same for distance1 & 2.

##  pseudo-code example
def calc_poly(data):
    ret_polyline = []
    for ctr in range(len(data)):
        ret_polyline.append(data[ctr].split(','))

    return ret_polyline

polyline1 = calc_poly(data)
polyline2 = calc_poly(data2)

Oh thank you! Yeah I've been trying to use Tkinter, but this really helps! Thank you so much!

import Tkinter as tk
# (x1, y1, x2, y2, ...)
data = (100,100,200,300,35,495,450,45)
root = tk.Tk()
canvas = tk.Canvas(root, height = 500, width = 500)
canvas.pack()
canvas.create_line(data, fill = 'red')
root.mainloop()

you will need to adjust (normalize?)your data to fit within the pixel-limits of the screen. That is at least to start, it is possible to scroll a canvas. But at first it might be better to tinker with the x and y values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.