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()
class DrawArea(wx.Window):
def __init__(self, parent, title):
wx.Window.__init__(self, parent)
self.data = [(1, 0, 0, 0, 0)]
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):
if type_ == 1:
(x1, y1), (x2, y2) = arg1, arg2
self.data.append((type_, x1, y1, x2, y2))
else:
self.data.append((type_, arg1, arg2))
dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)
self.DrawGraph(dc)
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)]
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(self.GetBackgroundColour()))
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)
dc.SetPen(wx.Pen("black", 2))
print self.GetData()
for n in self.GetData():
type_ = n[0]
if type_ == 1:
print n
s, x1, y1, x2, y2 = n
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))
#dc.SetPen(wx.Pen("black", 2))
if type_ == 21:
s, NoP, d = n
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 = n
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))
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title = "Graphing Program")
self.graph = DrawArea(self, "The Ultimate Graph!")
self.vMenu = wx.Menu()
self.vMenu.AppendCheckItem(ID_VG, "View Grid")
self.vMenu.Check(ID_VG, 1)
mb = wx.MenuBar()
mb.Append(self.vMenu, "&View")
wx.EVT_MENU(self, ID_VG, self.OnViewGrid)
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) ) )
except:
self.txtc1.SetValue("x1 | y1")
self.txtc2.SetValue("x2 | y2")
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)
if self.txtXY.GetValue().lower() == 'y':
self.graph.SetData(22, n, x)
except:
print "Nonono..."
def OnClear(self, event):
self.graph.ClearData()
def OnViewGrid(self, event):
if self.vMenu.IsChecked(ID_VG):
self.graph.SetGrid(1)
self.vMenu.Check(ID_VG, 1)
else:
self.graph.SetGrid(0)
self.vMenu.Check(ID_VG, 0)
app = wx.PySimpleApp()
frame = MainFrame()
app.MainLoop()