Sha256: 5b239fe888693c3363a105485181a3127c145d7b2db61439599e8421598051c1

Contents?: true

Size: 1.3 KB

Versions: 30

Compression:

Stored size: 1.3 KB

Contents

from java import awt
from math import *
from jarray import array

class Graph(awt.Canvas):
	def __init__(self):
		self.function = None

	def paint(self, g):
		if self.function is None:
			return self.error(g)
			
		sz = self.size
		xs = range(0, sz.width, 2)
		
		xscale = 4*pi/sz.width
		xoffset = -2*pi
		
		yscale = -sz.height/2.
		yoffset = sz.height/2.
		
		ys = []
		for x in xs:
			x = xscale*x + xoffset
			y = int(yscale*self.function(x)+yoffset)
			ys.append(y)
		g.drawPolyline(array(xs, 'i'), array(ys, 'i'), len(xs))

	def error(self, g):
		message = "Invalid Expression"
		g.font = awt.Font('Serif', awt.Font.BOLD, 20)
		width = g.fontMetrics.stringWidth(message)
		
		x = (self.size.width-width)/2
		y = (self.size.height+g.fontMetrics.height)/2
		g.drawString("Invalid Expression", x, y)
	
	def setExpression(self, e):
		try:
			self.function = eval('lambda x: '+e)
		except:
			self.function = None
		self.repaint()
	

if __name__ == '__main__':
	def enter(e):
		graph.setExpression(expression.text)
		expression.caretPosition=0
		expression.selectAll()
		
	p = awt.Panel(layout=awt.BorderLayout())
	graph = Graph()
	p.add(graph, 'Center')
	
	expression = awt.TextField(text='(sin(3*x)+cos(x))/2', actionPerformed=enter)
	p.add(expression, 'South')
	
	import pawt	
	pawt.test(p, size=(300,300))
	
	enter(None)

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
rubyfox-server-2.19.2.0 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.17.3.2 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.17.3.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.17.3.0 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.3.2 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.3.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.0.4 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.0.3 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.0.2 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.0.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.16.0.0 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.7.3 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.6.2 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.6.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.5.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.4.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.13.3.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.12.5.1 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.12.5.0 lib/rubyfox/server/data/lib/Lib/test/Graph.py
rubyfox-server-2.12.4.0 lib/rubyfox/server/data/lib/Lib/test/Graph.py