Skip to content

Commit 14e84c8

Browse files
committed
Actually adding files
1 parent 8e7e29c commit 14e84c8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+74325
-0
lines changed
Binary file not shown.
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'''
2+
Created on Nov 8, 2014
3+
4+
@author: Gary
5+
'''
6+
from math import sqrt
7+
from math import pow
8+
9+
class graph:
10+
def __init__(self):
11+
self.vertexNameArray = [] # vertex names in an array
12+
self.vertexIndexMap = {} # vertex names to index dictionary
13+
self.vertexPositionArray = [] # x,y pair position array
14+
self.edgeArray = [] # array of (vertex index pair, weight)
15+
16+
def addVertex(self, name, x, y):
17+
self.vertexIndexMap[name] = self.vCount()
18+
self.vertexNameArray.append(name)
19+
self.vertexPositionArray.append((x,y))
20+
21+
def addEdge(self, vName1, vName2, weight):
22+
self.edgeArray.append((self.vertexIndexMap[vName1],self.vertexIndexMap[vName2],weight))
23+
24+
def vCount(self): return len(self.vertexNameArray)
25+
26+
def eCount(self): return len(self.edgeArray)
27+
28+
# Access functions for vertex information
29+
def vX( self, i): return self.vertexPositionArray[i][0]
30+
def vY( self, i): return self.vertexPositionArray[i][1]
31+
def vName(self, i): return self.vertexNameArray[i]
32+
33+
# Access functions for edge information
34+
def eV0X( self, i): return self.vertexPositionArray[self.edgeArray[i][0]][0]
35+
def eV0Y( self, i): return self.vertexPositionArray[self.edgeArray[i][0]][1]
36+
def eV1X( self, i): return self.vertexPositionArray[self.edgeArray[i][1]][0]
37+
def eV1Y( self, i): return self.vertexPositionArray[self.edgeArray[i][1]][1]
38+
def eWght(self, i): return self.edgeArray[i][2]
39+
40+
#uses the cross product to find wether the angle turns left or right
41+
def angleTurnsLeft(self, point1, middlePoint, point3):
42+
crossProduct = (self.vX(middlePoint) - self.vX(point1)) * (self.vY(point3) - self.vY(point1)) - (self.vX(point3) - self.vX(point1)) * (self.vY(middlePoint) - self.vY(point1))
43+
if crossProduct >= 0.0:
44+
return True
45+
else:
46+
return False
47+
48+
49+
def findConvexHullEdges(self):
50+
#find lowest vertex
51+
yValMap = {}
52+
for i in range(self.vCount()):
53+
if self.vY(i) not in yValMap.keys():
54+
yValMap[self.vY(i)] = []
55+
yValMap[self.vY(i)].append(i)
56+
lowestVertexIndex = yValMap[sorted(yValMap.keys())[0]][0]
57+
58+
59+
#order other verticies acording to decreasing normalized x value
60+
xNormalizedMap = {}
61+
for i in range(self.vCount()):
62+
if i != lowestVertexIndex:
63+
#using formula: (Xi - X0)/(sqrt( (Xi - X0)^2 + (Yi - Y0)^2 )
64+
normalizedXValue = (self.vX(i) - self.vX(lowestVertexIndex)) / sqrt( (self.vX(i) - self.vX(lowestVertexIndex))**2 + (self.vY(i) - self.vY(lowestVertexIndex))**2 )
65+
if normalizedXValue not in xNormalizedMap.keys():
66+
xNormalizedMap[normalizedXValue] = i
67+
else:
68+
if self.vX(i) < self.vX(xNormalizedMap[normalizedXValue]):
69+
xNormalizedMap[normalizedXValue] = i
70+
orderedVerticies = [lowestVertexIndex]
71+
for xValue in sorted(xNormalizedMap.keys(), reverse = True):
72+
orderedVerticies.append(xNormalizedMap[xValue])
73+
orderedVerticies.append(lowestVertexIndex)
74+
75+
#select final and original edges
76+
originallyTriedEdges = []
77+
finalEdges = [(orderedVerticies[0], orderedVerticies[1])]
78+
i=0
79+
while i < len(orderedVerticies)-2:
80+
if self.angleTurnsLeft(orderedVerticies[i], orderedVerticies[i+1], orderedVerticies[i+2]):
81+
finalEdges.append((orderedVerticies[i+1],orderedVerticies[i+2]))
82+
i += 1
83+
else:
84+
orderedVerticies.pop(i+1)
85+
originallyTriedEdges.append(finalEdges.pop())
86+
i -= 1
87+
return originallyTriedEdges, finalEdges
88+
89+
90+
91+
92+
93+
5.29 KB
Binary file not shown.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
a 20 30
2+
b 200 40
3+
c 30 350
4+
d 350 380
5+
e 100 300
6+
----------
7+
a b 3
8+
b c 4
9+
a c 5
10+
a d 4
11+
b d 2
12+
c d 1
13+
e a 2
14+
a e 5
15+
e c 3
16+
e d 3
17+
d e 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'''
2+
Created on Nov 8, 2014
3+
4+
@author: Gary
5+
'''
6+
7+
import fileinput
8+
import graph
9+
import matplotlib.pyplot as plt
10+
from matplotlib.path import Path
11+
import matplotlib.patches as patches
12+
import math
13+
14+
def plotEdge( plot_axis, x0, y0, x1, y1, weight, clr):
15+
d0 = 4 # This is an offset so the edge is not drawn to the middle of vertex
16+
d2 = 20 # This is an offset to the end of the arrow tails
17+
dx = x1-x0
18+
dy = y1-y0
19+
length = math.sqrt(dx*dx+dy*dy)
20+
if length > 0:
21+
vx = dx/length
22+
vy = dy/length
23+
#plot_axis.plot([x0+vx*d0,x1-vx*d0],[y0+vy*d0,y1-vy*d0], color=clr) # Draw a line
24+
#plot_axis.text( x0+dx/2, y0+dy/2, weight)
25+
26+
verts = [(x0+vy*d0,y0-vx*d0),(x0+vy*40,y0-vx*40),(x1-vx*80,y1-vy*80),(x1-vx*d0,y1-vy*d0),]
27+
codes = [Path.MOVETO,Path.CURVE4,Path.CURVE4,Path.CURVE4,]
28+
path = Path(verts,codes)
29+
patch = patches.PathPatch( path, facecolor = 'none', edgecolor = clr)
30+
plot_axis.add_patch( patch )
31+
32+
plot_axis.plot([x1-vx*d2+vy*3,x1-vx*d0],[y1-vy*d2-vx*3,y1-vy*d0], color=clr)
33+
plot_axis.plot([x1-vx*d2-vy*3,x1-vx*d0],[y1-vy*d2+vx*3,y1-vy*d0], color=clr)
34+
35+
plot_axis.text( x0+dx/2+vy*10, y0+dy/2-vx*10, weight)
36+
37+
# Parse graph.txt and populate mygraph structure.
38+
mygraph = graph.graph()
39+
isVertices = True
40+
for line in fileinput.input("graph.txt"):
41+
if isVertices:
42+
if "----------" in line:
43+
isVertices = False
44+
else: #read vertices in this part
45+
split = line.split()
46+
mygraph.addVertex(split[0],float(split[1]),float(split[2]))
47+
else: #read edges in this part
48+
split = line.split()
49+
mygraph.addEdge(split[0], split[1], float(split[2]))
50+
print line, isVertices
51+
52+
fig = plt.figure()
53+
plt_ax = fig.add_subplot(111)
54+
55+
56+
57+
# Display vertices
58+
minX = minY = 1e1000
59+
maxX = maxY = -1e1000
60+
for iV in range (0, mygraph.vCount()):
61+
x = mygraph.vX(iV)
62+
y = mygraph.vY(iV)
63+
plt_ax.plot(x,y,'wo', ms = 15)
64+
minX = min(minX,x)
65+
minY = min(minY,y)
66+
maxX = max(maxX,x)
67+
maxY = max(maxY,y)
68+
plt_ax.text(x, y, mygraph.vName(iV), ha = 'center', va = 'center')
69+
padX = .10*(maxX-minX)+10
70+
padY = .10*(maxY-minY)+10
71+
plt_ax.axis([minX-padX, maxX+padX, minY-padY, maxY+padY])
72+
73+
originallyTriedEdges, finalEdges = mygraph.findConvexHullEdges();
74+
75+
# Display edges originally tried edges
76+
for edge in originallyTriedEdges:
77+
x0 = mygraph.vX(edge[0])
78+
y0 = mygraph.vY(edge[0])
79+
x1 = mygraph.vX(edge[1])
80+
y1 = mygraph.vY(edge[1])
81+
plotEdge(plt_ax, x0, y0, x1, y1, "", 'red')
82+
83+
# Display final edges
84+
for edge in finalEdges:
85+
x0 = mygraph.vX(edge[0])
86+
y0 = mygraph.vY(edge[0])
87+
x1 = mygraph.vX(edge[1])
88+
y1 = mygraph.vY(edge[1])
89+
plotEdge(plt_ax, x0, y0, x1, y1, "", 'black')
90+
91+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
a 20 30
2+
b 100 40
3+
c 30 250
4+
d 60 130
5+
e 150 90
6+
f 240 60
7+
g 120 200
8+
h 200 160
9+
i 260 100
10+
j 360 400
11+
l 180 350
12+
m 280 300
13+
n 80 350
14+
o 350 40
15+
----------
16+
a b 3
17+
a c 2
18+
a d 5
19+
b c 4
20+
b d 1
21+
b g 2
22+
c d 7
23+
c g 5
24+
e f 4
25+
e g 3
26+
e h 2
27+
f h 3
28+
f i 4
29+
g l 1
30+
g n 1
31+
h i 1
32+
h m 3
33+
i m 2
34+
i o 2
35+
j l 7
36+
j m 3
37+
j o 4
38+
l m 5
39+
l n 6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
a 10 100
2+
b 100 200
3+
c 200 200
4+
d 300 200
5+
e 390 100
6+
f 300 10
7+
g 200 10
8+
h 100 10
9+
i 150 100
10+
----------
11+
a b 4
12+
b c 8
13+
b h 11
14+
a h 8
15+
h i 7
16+
h g 1
17+
i c 2
18+
i g 6
19+
c f 4
20+
g f 2
21+
c d 7
22+
d e 9
23+
d f 14
24+
e f 10

0 commit comments

Comments
 (0)