Add graphs

master
Oystein Kristoffer Tveit 2021-05-10 22:12:22 +02:00
parent 0cfd95ad59
commit 45e413eb0b
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,67 @@
from sys import argv
from pathlib import Path
from math import sin, cos, pi
def generateNodeCoords(n):
vectorLength = n / 2
degreeToTurn = (2 * pi) / n
nodeCoords = [(0, vectorLength)]
for node in range(n):
prev_x = nodeCoords[-1][0]
prev_y = nodeCoords[-1][1]
nodeCoords.append((
round(cos(degreeToTurn) * prev_x - sin(degreeToTurn) * prev_y, 5),
round(sin(degreeToTurn) * prev_x + cos(degreeToTurn) * prev_y, 5)
))
return nodeCoords
def latexify(graphType, nodes, edges):
zippedNodes = zip(nodes, generateNodeCoords(len(nodes)))
nodeString = '\n'.join(f'\\node ({name}) at ({x},{y}) {{${name}$}};' for name,(x,y) in zippedNodes)
if graphType == 'directed':
edgeString = '\n'.join(f'\\arrow{{{x}}}{{{y}}}' for x,y in edges)
elif graphType == 'undirected':
edgeString = '\n'.join(f'\\draw ({x}) -- ({y});' for x,y in edges)
else:
print('CAN\'t RECOGNIZE GRAPHTYPE: ' + graphType)
exit(1)
return (nodeString, edgeString)
def parseInput(inputData):
splitData = inputData.split('\n\n')
if splitData[0] == 'complete':
data1 = 'undirected'
nodes = [chr(i + 65) for i in range(int(splitData[1]))]
data2 = ' '.join(nodes)
data3 = '\n'.join([a+b for a in nodes for b in nodes if a < b])
pass
else:
data1, data2, data3 = splitData
graphType = data1
nodes = data2.split(' ')
edges = [(x,y) for x,y in data3.split('\n')]
return (graphType, nodes, edges)
def processFileContent(raw, template):
content = latexify(*parseInput(raw))
return template.replace('%NODES', content[0]).replace('%EDGES', content[1])
if __name__ == '__main__':
filename = argv[1]
with open(filename) as file:
content = latexify(*parseInput(file.read()))
with open(str(Path(__file__).parent.absolute()) + '/tex_templates/Graph.tex') as template:
with open(argv[2], 'w') as destination_file:
destination_file.write(template.read().replace('%NODES', content[0]).replace('%EDGES', content[1]))

View File

@ -18,6 +18,8 @@ def processContent(content):
result = Hasse.processFileContent(content, template.read())
elif contentType == 'FSA':
result = FSA.processFileContent(content, template.read())
elif contentType == 'Graph':
result = Graph.processFileContent(content, template.read())
elif contentType == 'Truthtable':
result = Truthtable.processFileContent(content, template.read())
else: