funcplot.py revision 00b0f109e4e04891be218ee0bfbddaabbe2b85ae
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterCopyright (C) 2006 Johan Engelen, johan@shouraizou.nl
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterCopyright (C) 2005 Aaron Spike, aaron@ekips.org
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterThis program is free software; you can redistribute it and/or modify
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterit under the terms of the GNU General Public License as published by
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterthe Free Software Foundation; either version 2 of the License, or
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster(at your option) any later version.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterThis program is distributed in the hope that it will be useful,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterbut WITHOUT ANY WARRANTY; without even the implied warranty of
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterGNU General Public License for more details.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterYou should have received a copy of the GNU General Public License
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosteralong with this program; if not, write to the Free Software
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan FosterThis program is a modified version of wavy.py by Aaron Spike.
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Fosterfrom math import *
449854c2a07b50ea64d9d6a8b03d18d4afeeee43Ken Stubbingsdef drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top,
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # step is the distance between nodes on x
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # coords and scales based on the source rect
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster coordx = lambda x: (x - xstart) * scalex + xoff #convert x-value to coordinate
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster coordy = lambda y: (ytop-y) * scaley + yoff #convert y-value to coordinate
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # functions specified by the user
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # initialize function and derivative for 0;
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # they are carried over from one iteration to the next, to avoid extra function calculations
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster if fponum == True: # numerical derivative, using 0.001*step as the small differential
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster d0 = (f(xstart + 0.001*step) - y0)/(0.001*step)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster else: # derivative given by the user
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster a = [] # path array
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster a.append(['M',[coordx(xstart), coordy(y0)]]) # initial moveto
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster else: # derivative given by the user
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # create curve
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster a.append(['C',[coordx(x - step + third), coordy(y0 + (d0 * third)),
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster y0 = y1 # next segment's y0 is this segment's y1
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster d0 = d1 # we assume the function is smooth everywhere, so carry over the derivative too
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster self.OptionParser.add_option("-s", "--samples",
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster help="Calculate the first derivative numerically")
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster help="The selected UI-tab when OK was pressed")
449854c2a07b50ea64d9d6a8b03d18d4afeeee43Ken Stubbings self.OptionParser.add_option("--pythonfunctions",
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # create new path with basic dimensions of selected rectangle
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster newpath = self.document.createElement('svg:path')
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster x = float(node.attributes.getNamedItem('x').value)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster y = float(node.attributes.getNamedItem('y').value)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster w = float(node.attributes.getNamedItem('width').value)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster h = float(node.attributes.getNamedItem('height').value)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster #copy attributes of rect
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster s = node.attributes.getNamedItem('style').value
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster t = node.attributes.getNamedItem('transform').value
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster newpath.setAttribute('d', simplepath.formatPath(
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster newpath.setAttribute('title', self.options.fofx)
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';'
a688bcbb4bcff5398fdd29b86f83450257dc0df4Allan Foster # + self.options.fpofx + ';'
e = FuncPlot()
e.affect()