diff options
| author | Johan B. C. Engelen <jbc.engelen@swissonline.ch> | 2006-12-28 14:27:30 +0000 |
|---|---|---|
| committer | johanengelen <johanengelen@users.sourceforge.net> | 2006-12-28 14:27:30 +0000 |
| commit | 822e53804bd058e6830ef9b5513b43b66a574eb2 (patch) | |
| tree | 5cfd483b9d683e3a502021c3c97774eff228659c /share/extensions/funcplot.py | |
| parent | replace relative jumps with labels to prevent wrong target address (diff) | |
| download | inkscape-822e53804bd058e6830ef9b5513b43b66a574eb2.tar.gz inkscape-822e53804bd058e6830ef9b5513b43b66a574eb2.zip | |
Committed [ 1620672 ] Funcplot: New features and bug fixes. With some adjustments.
(bzr r2106)
Diffstat (limited to 'share/extensions/funcplot.py')
| -rw-r--r-- | share/extensions/funcplot.py | 88 |
1 files changed, 70 insertions, 18 deletions
diff --git a/share/extensions/funcplot.py b/share/extensions/funcplot.py index d4154dfe7..9948478a1 100644 --- a/share/extensions/funcplot.py +++ b/share/extensions/funcplot.py @@ -1,5 +1,6 @@ #!/usr/bin/env python
'''
+Copyright (C) 2006 Georg Wiora, xorx@quarkbox.de
Copyright (C) 2006 Johan Engelen, johan@shouraizou.nl
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
@@ -17,31 +18,48 @@ 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
-This program is a modified version of wavy.py by Aaron Spike.
+Changes:
+ * This program is a modified version of wavy.py by Aaron Spike.
+ * 22-Dec-2006: Wiora : Added axis and isotropic scaling
'''
import inkex, simplepath, simplestyle
from math import *
from random import *
-def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top,
- fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False):
+def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, bottom,
+ fx = "sin(x)", fpx = "cos(x)", fponum = True, times2pi = False, isoscale = True, drawaxis = True):
if times2pi == True:
xstart = 2 * pi * xstart
xend = 2 * pi * xend
-
- # step is the distance between nodes on x
- step = (xend - xstart) / (samples-1)
- third = step / 3.0
-
+
# 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
scaley = height / (ytop - ybottom)
- yoff = top
- coordy = lambda y: (ytop-y) * scaley + yoff #convert y-value to coordinate
+ 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:
+ 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 != "":
@@ -49,6 +67,24 @@ def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top, if fpx != "":
fp = eval('lambda x: ' + fpx)
+ # step is the distance between nodes on x
+ step = (xend - xstart) / (samples-1)
+ third = step / 3.0
+
+ 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
y0 = f(xstart)
@@ -57,8 +93,8 @@ def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top, else: # derivative given by the user
d0 = fp(xstart)
- a = [] # path array
- a.append(['M',[coordx(xstart), coordy(y0)]]) # initial moveto
+ # Start curve
+ a.append([' M ',[coordx(xstart), coordy(y0)]]) # initial moveto
for i in range(int(samples-1)):
x = (i+1) * step + xstart
@@ -68,7 +104,7 @@ def drawfunction(xstart, xend, ybottom, ytop, samples, width, height, left, top, else: # derivative given by the user
d1 = fp(x)
# create curve
- a.append(['C',[coordx(x - step + third), coordy(y0 + (d0 * third)),
+ a.append([' C ',[coordx(x - step + third), coordy(y0 + (d0 * third)),
coordx(x - third), coordy(y1 - (d1 * third)),
coordx(x), coordy(y1)]])
y0 = y1 # next segment's y0 is this segment's y1
@@ -115,6 +151,18 @@ class FuncPlot(inkex.Effect): 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",
@@ -143,17 +191,20 @@ class FuncPlot(inkex.Effect): except AttributeError:
pass
+ # top and bottom where exchanhged
newpath.setAttribute('d', simplepath.formatPath(
drawfunction(self.options.xstart,
self.options.xend,
self.options.ybottom,
self.options.ytop,
self.options.samples,
- w,h,x,y,
+ w,h,x,y+h,
self.options.fofx,
self.options.fpofx,
self.options.fponum,
- self.options.times2pi)))
+ self.options.times2pi,
+ self.options.isoscale,
+ self.options.drawaxis)))
newpath.setAttribute('title', self.options.fofx)
#newpath.setAttribute('desc', '!func;' + self.options.fofx + ';'
@@ -165,8 +216,9 @@ class FuncPlot(inkex.Effect): # add path into SVG structure
node.parentNode.appendChild(newpath)
- # TODO: make an option wether to remove the rectangle or not.
- node.parentNode.removeChild(node)
+ # option wether to remove the rectangle or not.
+ if self.options.remove:
+ node.parentNode.removeChild(node)
e = FuncPlot()
-e.affect()
\ No newline at end of file +e.affect()
|
