rendered paste body# Graphing lalalala
# CREDITS TO HAMAD... LALALALA
# SHALALALALALALALALALALALALALALALAT!!!
# By PEACEYALL(AT)gmail.com
import wx
import math, cmath
import time
ID_VG = wx.NewId()
ID_CO = wx.NewId()
ID_PS = wx.NewId()
ID_PenS = wx.NewId()
class DrawArea(wx.Window):
def __init__(self, parent, title):
wx.Window.__init__(self, parent)
self.data = [(1, (0, 0), (0, 0), (0, 0, 0), 2, wx.SOLID)]
self.title = title
self.titleFont = wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD)
self.grid = 1
self.InitBuffer()
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
def OnSize(self, event):
self.InitBuffer()
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self, self.buffer)
def GetData(self):
return self.data
def SetData(self, type_, arg1, arg2, arg3, arg4, arg5):
self.data.append((type_, arg1, arg2, arg3, arg4, arg5))
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawGraph(dc)
def OpenFile(self, FileName):
file = open(FileName, 'r').read().replace(' ', '')
split = file.split('\n')
for n in split:
if n == '':
pass;
else:
Esplit = n.split("||")
print Esplit
type_ = Esplit[0]
if type_ == '': type_ = 0;
type_ = int(type_)
colour_Tmp = Esplit[2].replace('(', '').replace(')', '')
c = [int(n) for n in colour_Tmp.split(',')]
ps = int(Esplit[3])
style = Esplit[4]
if type_ == 0:
pass;
if type_ == 1:
a = Esplit[1]
a = a.split('|')
self.SetData(type_, (float(a[0]), float(a[1])), (float(a[2]), float(a[3])), (c[0], c[1], c[2]), ps, eval(style))
else:
a = Esplit[1]
a = a.split('|')
n = [int(n) for n in a[1].split('->')]
x = a[0]
self.SetData(type_, n, x, (c[0], c[1], c[2]), ps, eval(style))
def SaveFile(self, FileName):
file_w = open(FileName, 'w')
s = self.GetData()
for n in s:
type_ = n[0]
if type_ == 1:
x1, y1 = n[1]
x2, y2 = n[2]
colour = n[3]
pointS = n[4]
style = n[5]
file_w.write("\n%s || %s | %s | %s | %s || %s || %s || %s" % (type_, x1, y1, x2, y2, colour, pointS, style) )
else:
NoP, eq = n[1], n[2]
colour = n[3]
pointS = n[4]
style = n[5]
file_w.write("\n%s || %s | %s -> %s || %s || %s || %s" % (type_, eq, NoP[0], NoP[1], colour, pointS, style) )
def SetGrid(self, value):
self.grid = value
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawGraph(dc)
def ClearData(self):
self.data = [(1, (0, 0), (0, 0), (0, 0, 0), 2, wx.SOLID)]
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawGraph(dc)
def InitBuffer(self):
w, h = self.GetClientSize()
self.buffer = wx.EmptyBitmap(w, h)
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawGraph(dc)
def DrawGraph(self, dc):
spacer = 10
scaledmax = 120.0
dc.SetBackground(wx.Brush((255, 255, 255)))
dc.Clear()
w, h = dc.GetSize()
dc.SetFont(self.titleFont)
tw, th = dc.GetTextExtent(self.title)
dc.DrawText(self.title, (w-tw)/2, spacer)
th = th + 2*spacer # <--
cx = w/2 # | Finding the center point
cy = (h-th)/2 + th # <--
print "------"
print "Center Point: ", cy
mindim = min(cx, (h-th)/2) # <-- Calculating the
scale = mindim/scaledmax # <-- scale factor
if self.grid == 1:
dc.SetPen(wx.Pen("grey", 1))
for n in range(10, 120, 10):
dc.DrawLine((cx-n*scale), cy-110*scale, (cx-n*scale), (cy+110*scale)+2)
dc.DrawLine((cx+n*scale)+1, cy-110*scale, (cx+n*scale)+1, (cy+110*scale)+2)
dc.DrawLine(cx-110*scale, cy-n*scale, (cx+110*scale)+2, cy-n*scale)
dc.DrawLine(cx-110*scale, (cy+n*scale)+1, (cx+110*scale)+2, (cy+n*scale)+1)
dc.SetPen(wx.Pen("black", 2))
dc.DrawLine(cx-110*scale, cy, cx+110*scale, cy)
dc.DrawLine(cx, cy-110*scale, cx, cy+110*scale)
print "(%s, %s) (%s, %s)" % (str(cx-110*scale), str(cy), str(cx+110*scale), str(cy))
print "(%s, %s) (%s, %s)" % (str(cx), str(cy-110*scale), str(cx), str(cy+110*scale))
dc.SetPen(wx.Pen("black", 2))
print "App Center x, y: (%s, %s)" % (w/2, h/2)
#x1, y1 = (0, 0)
#x2, y2 = (0, 70)
print self.GetData()
for n in self.GetData():
try:
type_ = n[0]
if type_ == 1:
print n
x1, y1 = n[1]
x2, y2 = n[2]
colour = n[3]
ps = n[4]
style = n[5]
dc.SetPen(wx.Pen(colour, ps, style))
print "x1:", x1, "y1:", y1, "x2:", x2, "y2:", y2
dc.DrawLines([wx.Point(x1*scale, -y1*scale), wx.Point(x2*scale, -y2*scale)], (w/2)+0.5, (h/2)+(spacer+8))
if type_ == 21:
s, NoP, d, c, ps, s = n
dc.SetPen(wx.Pen(c, ps, s))
x = range(int(NoP[0]), int(NoP[-1]))
for y in x:
eq = eval(d)
dc.DrawLines([(eq*scale, -y*scale), (eq*scale, -y*scale)], (w/2)+0.5, (h/2)+(spacer+8))
if type_ == 22:
s, NoP, d, c, ps, s = n
dc.SetPen(wx.Pen(c, ps, s))
y = range(int(NoP[0]), int(NoP[-1]))
for x in y:
eq = eval(d)
dc.DrawLines([(x*scale, -eq*scale), (x*scale, -eq*scale)], (w/2)+0.5, (h/2)+(spacer+8))
except:
self.data.remove(n)
a = "Error in format. Removed from list."
raise a;
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title = "Graphing Program")
self.graph = DrawArea(self, "The Ultimate Graph!")
self.colour = (0, 0, 0)
self.PointSize = 2
self.PenStyle = wx.SOLID
self.Colourdata = None
self.sMenu = wx.Menu()
self.sMenu.AppendCheckItem(ID_VG, "View Grid")
self.sMenu.Check(ID_VG, 1)
self.sMenu.Append(ID_CO, "Set Colour")
self.sMenu.Append(ID_PS, "Set Point Size")
self.sMenu.Append(ID_PenS, "Set Pen Style")
self.fMenu = wx.Menu()
open_f = self.fMenu.Append(-1, "Open Saved GPH File")
save_f = self.fMenu.Append(-1, "Save As GPH File")
mb = wx.MenuBar()
mb.Append(self.fMenu, "&File")
mb.Append(self.sMenu, "S&ettings")
wx.EVT_MENU(self, ID_VG, self.OnViewGrid)
wx.EVT_MENU(self, ID_CO, self.OnColour)
wx.EVT_MENU(self, ID_PS, self.OnPointSize)
wx.EVT_MENU(self, ID_PenS, self.OnPenStyle)
self.Bind(wx.EVT_MENU, self.OnOpen, open_f)
self.Bind(wx.EVT_MENU, self.OnSave, save_f)
self.txtc1 = wx.TextCtrl(self, -1)
self.txtc1.SetValue("x1 | y1")
self.txtc2 = wx.TextCtrl(self, -1)
self.txtc2.SetValue("x2 | y2")
self.txtXY = wx.TextCtrl(self, -1)
self.txtXY.SetValue("X or Y")
self.txtEQ = wx.TextCtrl(self, -1)
self.txtEQ.SetValue("x")
self.txtNP = wx.TextCtrl(self, -1)
self.txtNP.SetValue("x -> y")
graph_btn = wx.Button(self, -1, "Graph!")
clear_btn = wx.Button(self, -1, "Clear")
self.lineRB = wx.RadioButton(self, -1, "(x | y)")
self.lineRB.SetValue(1)
self.eqRB = wx.RadioButton(self, -1, "Equation")
box_v = wx.BoxSizer(wx.VERTICAL)
box_h = wx.BoxSizer(wx.HORIZONTAL)
box_h2 = wx.BoxSizer(wx.HORIZONTAL)
box_h3 = wx.BoxSizer(wx.HORIZONTAL)
box_v.Add(self.graph, 1, wx.EXPAND|wx.ALL, 5)
box_v.AddSizer(box_h, 0, wx.ALIGN_CENTER|wx.ALL)
box_v.AddSizer(box_h2, 0, wx.ALIGN_CENTER|wx.ALL)
box_v.AddSizer(box_h3, 0, wx.ALIGN_CENTER|wx.ALL)
box_h.Add(self.lineRB, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h.Add(self.txtc1, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h.Add(self.txtc2, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h2.Add(self.eqRB, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h2.Add(self.txtXY, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h2.Add(self.txtEQ, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h2.Add(self.txtNP, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h3.Add(graph_btn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
box_h3.Add(clear_btn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
self.Bind(wx.EVT_BUTTON, self.OnGraph, graph_btn)
self.Bind(wx.EVT_BUTTON, self.OnClear, clear_btn)
self.SetSizer(box_v)
self.Layout()
self.SetMenuBar(mb)
self.Maximize(1)
self.Show(1)
def OnGraph(self, event):
if self.lineRB.GetValue():
try:
x1, y1 = self.txtc1.GetValue().replace(' ', '').split('|')
x2, y2 = self.txtc2.GetValue().replace(' ', '').split('|')
self.graph.SetData(1, ( eval(x1), eval(y1) ), ( eval(x2), eval(y2) ), self.colour, self.PointSize, self.PenStyle )
except:
self.NewMessageDialog("Invalid Syntax", "Syntax Error", wx.OK|wx.ICON_ERROR)
if self.eqRB.GetValue():
try:
nTmp = self.txtNP.GetValue().replace(' ', '')
n = nTmp.split('->')
x = self.txtEQ.GetValue()
if self.txtXY.GetValue().lower() == 'x':
self.graph.SetData(21, n, x, self.colour, self.PointSize, self.PenStyle)
if self.txtXY.GetValue().lower() == 'y':
self.graph.SetData(22, n, x, self.colour, self.PointSize, self.PenStyle)
except:
self.NewMessageDialog("Invalid Syntax", "Syntax Error", wx.OK|wx.ICON_ERROR)
def OnClear(self, event):
self.graph.ClearData()
def OnViewGrid(self, event):
if self.sMenu.IsChecked(ID_VG):
self.graph.SetGrid(1)
self.sMenu.Check(ID_VG, 1)
else:
self.graph.SetGrid(0)
self.sMenu.Check(ID_VG, 0)
def OnOpen(self, event):
fd = wx.FileDialog(self, "Choose the GPH File...", '', '', "All Files (*.*)|*.*", wx.OPEN)
if fd.ShowModal() == wx.ID_OK:
try:
self.graph.OpenFile(fd.GetPath())
except:
self.NewMessageDialog("The file you have chosen is corrupt, please ensure that it is a correct file.", "Corrupt File", wx.OK|wx.ICON_ERROR)
fd.Destroy()
def OnSave(self, event):
fd = wx.FileDialog(self, "Select where to save the file...", '', '', "All Files (*.*)|*.*", wx.SAVE|wx.OVERWRITE_PROMPT)
if fd.ShowModal() == wx.ID_OK:
try:
self.graph.SaveFile(fd.GetPath())
except:
self.NewMessageDialog("Could not save to file/folder. Make sure you have permission.", "Saving Failed", wx.OK|wx.ICON_ERROR)
fd.Destroy()
def NewMessageDialog(self, text, title, style):
md = wx.MessageDialog(self, text, title, style = style)
md.ShowModal()
md.Destroy()
def OnColour(self, event):
cd = wx.ColourDialog(self, self.Colourdata)
if cd.ShowModal() == wx.ID_OK:
self.Colourdata = cd.GetColourData()
self.colour = cd.GetColourData().GetColour()
cd.Destroy()
def OnPointSize(self, event):
td = wx.TextEntryDialog(self, "Enter the Point Size you desire in pixels: ", "Point Width", str(self.PointSize))
if td.ShowModal() == wx.ID_OK:
self.PointSize = eval(td.GetValue())
td.Destroy()
def OnPenStyle(self, event):
td = wx.TextEntryDialog(self, "Enter the Pen Style you would like to use. ( Make sure it is a correct value. ) ", "Pen Style", str(self.PenStyle))
if td.ShowModal() == wx.ID_OK:
self.PenStyle = eval(td.GetValue())
td.Destroy()
app = wx.PySimpleApp()
frame = MainFrame()
app.MainLoop()