From 45e413eb0b82e91d48e648a97f300059e85d227b Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 10 May 2021 22:12:22 +0200 Subject: [PATCH] Add graphs --- exam_template/python/Graph.py | 67 +++++++++++++++++++++++++++++++++++ exam_template/python/run.py | 2 ++ 2 files changed, 69 insertions(+) create mode 100644 exam_template/python/Graph.py diff --git a/exam_template/python/Graph.py b/exam_template/python/Graph.py new file mode 100644 index 0000000..bd6eb43 --- /dev/null +++ b/exam_template/python/Graph.py @@ -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])) \ No newline at end of file diff --git a/exam_template/python/run.py b/exam_template/python/run.py index cb8aeb3..2f47647 100644 --- a/exam_template/python/run.py +++ b/exam_template/python/run.py @@ -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: