Skip to content

Instantly share code, notes, and snippets.

@megamaz
Last active June 24, 2019 22:47
Show Gist options
  • Save megamaz/8f27b94b31b6609be8a0f826ac93fd8e to your computer and use it in GitHub Desktop.
Save megamaz/8f27b94b31b6609be8a0f826ac93fd8e to your computer and use it in GitHub Desktop.
Graph library can be used to makeit easier to use graphs. Put this file into whatever python code you want to make, type import graph, and use graph.(command) to use graph.
import ctypes
import sys
"""
Graphing made simpler
INTS
----
Used for points (X, Y) or size of graph
"""
graphs = []
# 'graphs' list is using nested tuples like this:
# (int, (tuples))
# the int is for the size of the graph. The X length will equal Y length. tuples are the point position.
# so a full item would look like this:
# (30, ((4, 23), (3,5)))
# ^ ^ ^
# | | |
# | | | First point located at (X, Y) == (4, 23).
# | |
# | | Allows us to store tuples inside tuples.
# |
# |Size of graph. X length = Y length. X length will also equal -X length, meaning if X length is 10 then X length will be 10, not 5.
# |Five could be because length of -X added to X would equal 10, making a 10 length line.
item = 0
user32 = ctypes.windll.user32
GRAPHSIZEMAX = user32.GetSystemMetrics(0)
GRAPHSIZEMAX = int(int(GRAPHSIZEMAX/100) / 0.7037037037037037)
connection = []
graph_sizes = [None]
def create_graph(size, points, pointsconnect):
"""
Will create a new graph compared on the parameter you enter.
size: Size of graph. X length will equal Y legnth. Size max is 27 for VS code, normal zoom, fullscreen.
points: if single point on graph, points will look like (X, Y)
if multiple points, it will look like ((X1, Y1), (X2, Y2))
(do not type 1/2. Example to seperate the variables.)
pointsconnect: Either True or False. if it's true, the points (o) will be connected with [.] in between. ALL POINTS WILL BE CONNECTED.
"""
if size >= GRAPHSIZEMAX:
return None
else:
graphs.append((size, (points)))
graph_sizes.append(size)
if pointsconnect != False: #Is true?
if pointsconnect != True: #is neither true or false?
pointsconnect = False
elif pointsconnect == True: #Is true!
if len(points) == 2: #Is it possible to connect points?
pointsconnect = False
else:
connections = points[0] - points[len(points-1)]
connection.append(connections)
def show_graph(graphnum):
"""
Will show a graph that you created. You can see the graphs you made by using the graphlist command (graph.graphlist). If you haven't made a graph yet, use the create_graph command to make a new graph.
graphnum: use the graphlist command to know which one you are printing.
"""
if len(graphs) == 0:
return None
if graphnum > len(graphs):
return None
if graphnum == 0:
return None
else:
item = graph_sizes[graphnum:graphnum+1]
item = str(item)
item = item[1:len(item)-1]
item = int(item)*2
for y in range(-1*item, item):
line = ""
for x in range(-1*item, item):
if str((x, -y)) in str(graphs[graphnum-1]):
line += "o"
if x < 0:
continue
if x == 0:
line += "|"
if y == 0:
line += "_"
if (x, y) in connection:
line += "."
else:
if y == 0:
continue
else:
line += " "
print(line)
def graphlist():
"""
Shows the graphs you made. can be used as a recall. The number is the number you'd enter in the show_graph command.
"""
graphnum = 0
for graphnum in range(len(graphs)):
print(f"{graphnum}.{graphs[graphnum:graphnum+1]}")
def remove_graph(graphNum):
"""
Will destroy whatever graph you have already created.
use graph.graphlist to know which one you are removing.
"""
graphs.remove(graphs[graphNum-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment