index 3a64e4e95708856ae708bfde846d3e7037f424b7..0b7c77ec90c2ae979cad648412dd05cfd27cbec7 100644 (file)
-#!/usr/bin/env python \r
-'''\r
-Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr\r
-Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de\r
-Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl\r
-Copyright (C) 2005 Aaron Spike, aaron@ekips.org\r
-\r
-This program is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-This program is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with this program; if not, write to the Free Software\r
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
-\r
-Changes:\r
- * This program is a modified version of wavy.py by Aaron Spike.\r
- * 22-Dec-2006: Wiora : Added axis and isotropic scaling\r
- * 21-Jun-2007: Tavmjong: Added polar coordinates\r
-\r
-'''\r
-import inkex, simplepath, simplestyle\r
-from math import *\r
-from random import *\r
-\r
-def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, bottom, \r
- fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False, polar = False, isoscale = True, drawaxis = True):\r
-\r
- if times2pi == True:\r
- xstart = 2 * pi * xstart\r
- xend = 2 * pi * xend \r
- \r
- # coords and scales based on the source rect\r
- scalex = width / (xend - xstart)\r
- xoff = left\r
- coordx = lambda x: (x - xstart) * scalex + xoff #convert x-value to coordinate\r
- if polar : # Set scale so that left side of rectangle is -1, right side is +1.\r
- # (We can't use xscale for both range and scale.)\r
- centerx = left + width/2.0\r
- polar_scalex = width/2.0\r
- coordx = lambda x: x * polar_scalex + centerx #convert x-value to coordinate\r
-\r
- scaley = height / (ytop - ybottom)\r
- yoff = bottom\r
- coordy = lambda y: (ybottom - y) * scaley + yoff #convert y-value to coordinate\r
-\r
- # Check for isotropic scaling and use smaller of the two scales, correct ranges\r
- if isoscale and not polar:\r
- if scaley<scalex:\r
- # compute zero location\r
- xzero = coordx(0)\r
- # set scale\r
- scalex = scaley\r
- # correct x-offset\r
- xstart = (left-xzero)/scalex\r
- xend = (left+width-xzero)/scalex\r
- else :\r
- # compute zero location\r
- yzero = coordy(0)\r
- # set scale\r
- scaley = scalex\r
- # correct x-offset\r
- ybottom = (yzero-bottom)/scaley\r
- ytop = (bottom+height-yzero)/scaley\r
-\r
- # functions specified by the user\r
- if fx != "":\r
- f = eval('lambda x: ' + fx.strip('"'))\r
- if fpx != "":\r
- fp = eval('lambda x: ' + fpx.strip('"'))\r
-\r
- # step is the distance between nodes on x\r
- step = (xend - xstart) / (samples-1)\r
- third = step / 3.0\r
- ds = step * 0.001 # Step used in calculating derivatives\r
-\r
- a = [] # path array \r
- # add axis\r
- if drawaxis :\r
- # check for visibility of x-axis\r
- if ybottom<=0 and ytop>=0:\r
- # xaxis\r
- a.append(['M ',[left, coordy(0)]])\r
- a.append([' l ',[width, 0]])\r
- # check for visibility of y-axis\r
- if xstart<=0 and xend>=0:\r
- # xaxis\r
- a.append([' M ',[coordx(0),bottom]])\r
- a.append([' l ',[0, -height]])\r
-\r
- # initialize function and derivative for 0;\r
- # they are carried over from one iteration to the next, to avoid extra function calculations. \r
- x0 = xstart\r
- y0 = f(xstart)\r
- if polar :\r
- xp0 = y0 * cos( x0 )\r
- yp0 = y0 * sin( x0 )\r
- x0 = xp0\r
- y0 = yp0\r
- if fponum or polar: # numerical derivative, using 0.001*step as the small differential\r
- x1 = xstart + ds # Second point AFTER first point (Good for first point)\r
- y1 = f(x1)\r
- if polar :\r
- xp1 = y1 * cos( x1 )\r
- yp1 = y1 * sin( x1 )\r
- x1 = xp1\r
- y1 = yp1\r
- dx0 = (x1 - x0)/ds \r
- dy0 = (y1 - y0)/ds\r
- else: # derivative given by the user\r
- dx0 = 0 # Only works for rectangular coordinates\r
- dy0 = fp(xstart)\r
-\r
- # Start curve\r
- a.append([' M ',[coordx(x0), coordy(y0)]]) # initial moveto\r
-\r
- for i in range(int(samples-1)):\r
- x1 = (i+1) * step + xstart\r
- x2 = x1 - ds # Second point BEFORE first point (Good for last point)\r
- y1 = f(x1)\r
- y2 = f(x2)\r
- if polar :\r
- xp1 = y1 * cos( x1 )\r
- yp1 = y1 * sin( x1 )\r
- xp2 = y2 * cos( x2 )\r
- yp2 = y2 * sin( x2 )\r
- x1 = xp1\r
- y1 = yp1\r
- x2 = xp2\r
- y2 = yp2\r
- if fponum or polar: # numerical derivative\r
- dx1 = (x1 - x2)/ds\r
- dy1 = (y1 - y2)/ds\r
- else: # derivative given by the user\r
- dx1 = 0 # Only works for rectangular coordinates\r
- dy1 = fp(x1)\r
- # create curve\r
- a.append([' C ',\r
- [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)), \r
- coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)),\r
- coordx(x1), coordy(y1)]\r
- ])\r
- x0 = x1 # Next segment's start is this segments end\r
- y0 = y1\r
- dx0 = dx1 # Assume the function is smooth everywhere, so carry over the derivative too\r
- dy0 = dy1 \r
- return a\r
-\r
-class FuncPlot(inkex.Effect):\r
- def __init__(self):\r
- inkex.Effect.__init__(self)\r
- self.OptionParser.add_option("--xstart",\r
- action="store", type="float", \r
- dest="xstart", default=0.0,\r
- help="Start x-value")\r
- self.OptionParser.add_option("--xend",\r
- action="store", type="float", \r
- dest="xend", default=1.0,\r
- help="End x-value")\r
- self.OptionParser.add_option("--times2pi",\r
- action="store", type="inkbool", \r
- dest="times2pi", default=True,\r
- help="Multiply x-range by 2*pi") \r
- self.OptionParser.add_option("--polar",\r
- action="store", type="inkbool", \r
- dest="polar", default=False,\r
- help="Plot using polar coordinates") \r
- self.OptionParser.add_option("--ybottom",\r
- action="store", type="float", \r
- dest="ybottom", default=-1.0,\r
- help="y-value of rectangle's bottom")\r
- self.OptionParser.add_option("--ytop",\r
- action="store", type="float", \r
- dest="ytop", default=1.0,\r
- help="y-value of rectangle's top")\r
- self.OptionParser.add_option("-s", "--samples",\r
- action="store", type="int", \r
- dest="samples", default=8,\r
- help="Samples") \r
- self.OptionParser.add_option("--fofx",\r
- action="store", type="string", \r
- dest="fofx", default="sin(x)",\r
- help="f(x) for plotting") \r
- self.OptionParser.add_option("--fponum",\r
- action="store", type="inkbool", \r
- dest="fponum", default=True,\r
- help="Calculate the first derivative numerically") \r
- self.OptionParser.add_option("--fpofx",\r
- action="store", type="string", \r
- dest="fpofx", default="cos(x)",\r
- help="f'(x) for plotting") \r
- self.OptionParser.add_option("--remove",\r
- action="store", type="inkbool", \r
- dest="remove", default=True,\r
- help="If True, source rectangle is removed") \r
- self.OptionParser.add_option("--isoscale",\r
- action="store", type="inkbool", \r
- dest="isoscale", default=True,\r
- help="If True, isotropic scaling is used") \r
- self.OptionParser.add_option("--drawaxis",\r
- action="store", type="inkbool", \r
- dest="drawaxis", default=True,\r
- help="If True, axis are drawn") \r
- self.OptionParser.add_option("--tab",\r
- action="store", type="string", \r
- dest="tab", default="sampling",\r
- help="The selected UI-tab when OK was pressed") \r
- self.OptionParser.add_option("--funcplotuse",\r
- action="store", type="string", \r
- dest="funcplotuse", default="",\r
- help="dummy") \r
- self.OptionParser.add_option("--pythonfunctions",\r
- action="store", type="string", \r
- dest="pythonfunctions", default="",\r
- help="dummy") \r
-\r
- def effect(self):\r
- for id, node in self.selected.iteritems():\r
- if node.tag == inkex.addNS('rect','svg'):\r
- # create new path with basic dimensions of selected rectangle\r
- newpath = inkex.etree.Element(inkex.addNS('path','svg'))\r
- x = float(node.get('x'))\r
- y = float(node.get('y'))\r
- w = float(node.get('width'))\r
- h = float(node.get('height'))\r
-\r
- #copy attributes of rect\r
- s = node.get('style')\r
- if s:\r
- newpath.set('style', s)\r
- \r
- t = node.get('transform')\r
- if t:\r
- newpath.set('transform', t)\r
- \r
- # top and bottom were exchanged\r
- newpath.set('d', simplepath.formatPath(\r
- drawfunction(self.options.xstart,\r
- self.options.xend,\r
- self.options.ybottom,\r
- self.options.ytop,\r
- self.options.samples, \r
- w,h,x,y+h,\r
- self.options.fofx, \r
- self.options.fpofx,\r
- self.options.fponum,\r
- self.options.times2pi,\r
- self.options.polar,\r
- self.options.isoscale,\r
- self.options.drawaxis)))\r
- newpath.set('title', self.options.fofx)\r
- \r
- #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';' \r
- # + self.options.fpofx + ';'\r
- # + `self.options.fponum` + ';'\r
- # + `self.options.xstart` + ';'\r
- # + `self.options.xend` + ';'\r
- # + `self.options.samples`)\r
- \r
- # add path into SVG structure\r
- node.getparent().append(newpath)\r
- # option wether to remove the rectangle or not.\r
- if self.options.remove:\r
- node.getparent().remove(node)\r
- \r
-e = FuncPlot()\r
-e.affect()\r
+#!/usr/bin/env python
+'''
+Copyright (C) 2007 Tavmjong Bah, tavmjong@free.fr
+Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de
+Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl
+Copyright (C) 2005 Aaron Spike, aaron@ekips.org
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+Changes:
+ * This program is a modified version of wavy.py by Aaron Spike.
+ * 22-Dec-2006: Wiora : Added axis and isotropic scaling
+ * 21-Jun-2007: Tavmjong: Added polar coordinates
+
+'''
+import inkex, simplepath, simplestyle
+from math import *
+from random import *
+
+def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, bottom,
+ fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False, polar = False, isoscale = True, drawaxis = True):
+
+ if times2pi == True:
+ xstart = 2 * pi * xstart
+ xend = 2 * pi * xend
+
+ # coords and scales based on the source rect
+ scalex = width / (xend - xstart)
+ xoff = left
+ coordx = lambda x: (x - xstart) * scalex + xoff #convert x-value to coordinate
+ if polar : # Set scale so that left side of rectangle is -1, right side is +1.
+ # (We can't use xscale for both range and scale.)
+ centerx = left + width/2.0
+ polar_scalex = width/2.0
+ coordx = lambda x: x * polar_scalex + centerx #convert x-value to coordinate
+
+ scaley = height / (ytop - ybottom)
+ yoff = bottom
+ coordy = lambda y: (ybottom - y) * scaley + yoff #convert y-value to coordinate
+
+ # Check for isotropic scaling and use smaller of the two scales, correct ranges
+ if isoscale and not polar:
+ if scaley<scalex:
+ # compute zero location
+ xzero = coordx(0)
+ # set scale
+ scalex = scaley
+ # correct x-offset
+ xstart = (left-xzero)/scalex
+ xend = (left+width-xzero)/scalex
+ else :
+ # compute zero location
+ yzero = coordy(0)
+ # set scale
+ scaley = scalex
+ # correct x-offset
+ ybottom = (yzero-bottom)/scaley
+ ytop = (bottom+height-yzero)/scaley
+
+ # functions specified by the user
+ if fx != "":
+ f = eval('lambda x: ' + fx.strip('"'))
+ if fpx != "":
+ fp = eval('lambda x: ' + fpx.strip('"'))
+
+ # step is the distance between nodes on x
+ step = (xend - xstart) / (samples-1)
+ third = step / 3.0
+ ds = step * 0.001 # Step used in calculating derivatives
+
+ a = [] # path array
+ # add axis
+ if drawaxis :
+ # check for visibility of x-axis
+ if ybottom<=0 and ytop>=0:
+ # xaxis
+ a.append(['M ',[left, coordy(0)]])
+ a.append([' l ',[width, 0]])
+ # check for visibility of y-axis
+ if xstart<=0 and xend>=0:
+ # xaxis
+ a.append([' M ',[coordx(0),bottom]])
+ a.append([' l ',[0, -height]])
+
+ # initialize function and derivative for 0;
+ # they are carried over from one iteration to the next, to avoid extra function calculations.
+ x0 = xstart
+ y0 = f(xstart)
+ if polar :
+ xp0 = y0 * cos( x0 )
+ yp0 = y0 * sin( x0 )
+ x0 = xp0
+ y0 = yp0
+ if fponum or polar: # numerical derivative, using 0.001*step as the small differential
+ x1 = xstart + ds # Second point AFTER first point (Good for first point)
+ y1 = f(x1)
+ if polar :
+ xp1 = y1 * cos( x1 )
+ yp1 = y1 * sin( x1 )
+ x1 = xp1
+ y1 = yp1
+ dx0 = (x1 - x0)/ds
+ dy0 = (y1 - y0)/ds
+ else: # derivative given by the user
+ dx0 = 0 # Only works for rectangular coordinates
+ dy0 = fp(xstart)
+
+ # Start curve
+ a.append([' M ',[coordx(x0), coordy(y0)]]) # initial moveto
+
+ for i in range(int(samples-1)):
+ x1 = (i+1) * step + xstart
+ x2 = x1 - ds # Second point BEFORE first point (Good for last point)
+ y1 = f(x1)
+ y2 = f(x2)
+ if polar :
+ xp1 = y1 * cos( x1 )
+ yp1 = y1 * sin( x1 )
+ xp2 = y2 * cos( x2 )
+ yp2 = y2 * sin( x2 )
+ x1 = xp1
+ y1 = yp1
+ x2 = xp2
+ y2 = yp2
+ if fponum or polar: # numerical derivative
+ dx1 = (x1 - x2)/ds
+ dy1 = (y1 - y2)/ds
+ else: # derivative given by the user
+ dx1 = 0 # Only works for rectangular coordinates
+ dy1 = fp(x1)
+ # create curve
+ a.append([' C ',
+ [coordx(x0 + (dx0 * third)), coordy(y0 + (dy0 * third)),
+ coordx(x1 - (dx1 * third)), coordy(y1 - (dy1 * third)),
+ coordx(x1), coordy(y1)]
+ ])
+ x0 = x1 # Next segment's start is this segments end
+ y0 = y1
+ dx0 = dx1 # Assume the function is smooth everywhere, so carry over the derivative too
+ dy0 = dy1
+ return a
+
+class FuncPlot(inkex.Effect):
+ def __init__(self):
+ inkex.Effect.__init__(self)
+ self.OptionParser.add_option("--xstart",
+ action="store", type="float",
+ dest="xstart", default=0.0,
+ help="Start x-value")
+ self.OptionParser.add_option("--xend",
+ action="store", type="float",
+ dest="xend", default=1.0,
+ help="End x-value")
+ self.OptionParser.add_option("--times2pi",
+ action="store", type="inkbool",
+ dest="times2pi", default=True,
+ help="Multiply x-range by 2*pi")
+ self.OptionParser.add_option("--polar",
+ action="store", type="inkbool",
+ dest="polar", default=False,
+ help="Plot using polar coordinates")
+ self.OptionParser.add_option("--ybottom",
+ action="store", type="float",
+ dest="ybottom", default=-1.0,
+ help="y-value of rectangle's bottom")
+ self.OptionParser.add_option("--ytop",
+ action="store", type="float",
+ dest="ytop", default=1.0,
+ help="y-value of rectangle's top")
+ self.OptionParser.add_option("-s", "--samples",
+ action="store", type="int",
+ dest="samples", default=8,
+ help="Samples")
+ self.OptionParser.add_option("--fofx",
+ action="store", type="string",
+ dest="fofx", default="sin(x)",
+ help="f(x) for plotting")
+ self.OptionParser.add_option("--fponum",
+ action="store", type="inkbool",
+ dest="fponum", default=True,
+ help="Calculate the first derivative numerically")
+ self.OptionParser.add_option("--fpofx",
+ action="store", type="string",
+ dest="fpofx", default="cos(x)",
+ help="f'(x) for plotting")
+ self.OptionParser.add_option("--remove",
+ action="store", type="inkbool",
+ dest="remove", default=True,
+ help="If True, source rectangle is removed")
+ self.OptionParser.add_option("--isoscale",
+ action="store", type="inkbool",
+ dest="isoscale", default=True,
+ help="If True, isotropic scaling is used")
+ self.OptionParser.add_option("--drawaxis",
+ action="store", type="inkbool",
+ dest="drawaxis", default=True,
+ help="If True, axis are drawn")
+ self.OptionParser.add_option("--tab",
+ action="store", type="string",
+ dest="tab", default="sampling",
+ help="The selected UI-tab when OK was pressed")
+ self.OptionParser.add_option("--funcplotuse",
+ action="store", type="string",
+ dest="funcplotuse", default="",
+ help="dummy")
+ self.OptionParser.add_option("--pythonfunctions",
+ action="store", type="string",
+ dest="pythonfunctions", default="",
+ help="dummy")
+
+ def effect(self):
+ for id, node in self.selected.iteritems():
+ if node.tag == inkex.addNS('rect','svg'):
+ # create new path with basic dimensions of selected rectangle
+ newpath = inkex.etree.Element(inkex.addNS('path','svg'))
+ x = float(node.get('x'))
+ y = float(node.get('y'))
+ w = float(node.get('width'))
+ h = float(node.get('height'))
+
+ #copy attributes of rect
+ s = node.get('style')
+ if s:
+ newpath.set('style', s)
+
+ t = node.get('transform')
+ if t:
+ newpath.set('transform', t)
+
+ # top and bottom were exchanged
+ newpath.set('d', simplepath.formatPath(
+ drawfunction(self.options.xstart,
+ self.options.xend,
+ self.options.ybottom,
+ self.options.ytop,
+ self.options.samples,
+ w,h,x,y+h,
+ self.options.fofx,
+ self.options.fpofx,
+ self.options.fponum,
+ self.options.times2pi,
+ self.options.polar,
+ self.options.isoscale,
+ self.options.drawaxis)))
+ newpath.set('title', self.options.fofx)
+
+ #newpath.setAttribute('desc', '!func;' + self.options.fofx + ';'
+ # + self.options.fpofx + ';'
+ # + `self.options.fponum` + ';'
+ # + `self.options.xstart` + ';'
+ # + `self.options.xend` + ';'
+ # + `self.options.samples`)
+
+ # add path into SVG structure
+ node.getparent().append(newpath)
+ # option wether to remove the rectangle or not.
+ if self.options.remove:
+ node.getparent().remove(node)
+
+e = FuncPlot()
+e.affect()