-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.py
More file actions
139 lines (131 loc) · 4.68 KB
/
draw.py
File metadata and controls
139 lines (131 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#from MST import generatemst
import random
import copy
import csv
import sys
import time
import Queue as Q
import sys
if sys.version_info[0] < 3:
import Tkinter as tk
else:
import tkinter as tk
import ttk
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
sx= 0
sum = 0
queue = Q.PriorityQueue()
class Item(object):
def __init__(self,list):
self.list=list
return
def __cmp__(self, other):
if(self.list[0]>other.list[0]):
return True
else:
return False
def draw_reset():
global sx
global sum
global queue
sx= 0
sum = 0
queue = Q.PriorityQueue()
# if without weight:
def draw_graph(list,weight_flag):
w= weight_flag
length = 0
total_weight = 0
effort=0
startx=0
starty=0
x1=0
x2=0
y1=0
y2=0
dict={}
root = tk.Tk()
for i in range(0,len(list)):
length=0
effort=0
total_weight=0
top = Toplevel()
top.title('#'+str(i+1))
top.wm_geometry("200x300")
dict=list[i]
f = Figure(figsize=(3, 3.5), dpi=100)
a = f.add_subplot(111)
a.set_axis_off()
plt.xlim(-1,21)
plt.ylim(-1,21)
for i in range(1,11):
for j in range(1,11):
a.plot(2*i,2*j,'go')
for key in dict:
if key==0:
startx = dict[key][1]
starty=dict[key][2]
a.plot(startx,starty,'ro')
print "(", str(startx),",",str(starty),")->",
else:
a.plot(int(dict[key][1]),int(dict[key][2]),'ro')
#[startx,dict[key][1]],[starty,dict[key][2]]
#a.plot([dict[key][1],startx],[dict[key][2],starty],'k')
x1=startx
x2=int(dict[key][1])
y1=starty
y2=int(dict[key][2])
print "(", str(x2),",",str(y2),")->",
cur_w = dict[key][0]
effort = (abs(x2-x1)+abs(y2-y1))*total_weight
total_weight+=cur_w
length+=(abs(x2-x1)+abs(y2-y1))
if x1==x2:
if not y1==y2:
# a.plot([x1, x2],[y1, y2],'k')
#ax.arrow(x1, x2, y1, y2, head_width=0.02, head_length=0.02, fc='k', ec='k')
a.arrow(x1,y1,x2-x1,y2-y1,head_width=0.6,head_length=0.1)
elif y1==y2:
if not x1==x2:
# a.plot([x1, x1],[y1, y1+1],'k')
# a.plot([x1, x2],[y1+1, y2+1],'k')
# a.plot([x2, x2],[y2+1, y2],'k')
# ax.arrow(x1, x1, y1, y1+1, head_width=0.01, head_length=0.02, fc='k', ec='k')
# ax.arrow(x1, x2, y1+1, y2+1, head_width=0.01, head_length=0.02, fc='k', ec='k')
# ax.arrow(x2, x2, y2+1, y2, head_width=0.01, head_length=0.02, fc='k', ec='k')
a.arrow(x1,y1,0,1,head_width=0.6,head_length=0.3)
a.arrow(x1,y1+1,x2-x1,y2-y1,head_width=0.6,head_length=0.3)
a.arrow(x2,y2+1,0,-1,head_width=0.6,head_length=0.3)
else:
dirx = (x2-x1)/abs(x2-x1)
diry=(y2-y1)/abs(y2-y1)
cory1=diry*(abs(y2-y1)-1)+y1
# a.plot([x1, x1],[y1, cory1],'k')
# a.plot([x1, x2],[cory1, cory1],'k')
# a.plot([x2, x2],[cory1, y2],'k')
# ax.arrow(x1, x1, y1, cory1, head_width=0.01, head_length=0.02, fc='k', ec='k')
# ax.arrow(x1, x2, cory1, cory1, head_width=0.01, head_length=0.02, fc='k', ec='k')
# ax.arrow(x2, x2, cory1, y2, head_width=0.01, head_length=0.02, fc='k', ec='k')
a.arrow(x1,y1,0,cory1-y1,head_width=0.6,head_length=0.3)
a.arrow(x1,cory1,x2-x1,0,head_width=0.6,head_length=0.3)
a.arrow(x2,cory1,0,y2-cory1,head_width=0.6,head_length=0.3)
startx = int(dict[key][1])
starty=int(dict[key][2])
print "END"
print "The optimal path length is :", length
if w=='y':
print "The total effort in this order is:",effort
canvas = FigureCanvasTkAgg(f, master=top)
canvas.draw()
canvas.get_tk_widget().pack(side="top", expand=1)
go_button = tk.Button(root, text='Quit!', width=3,
height=1, command=root.destroy)
go_button.pack(side="top")
root.mainloop()