[genius] Thu Sep 22 17:05:13 2016 Jiri (George) Lebl <jirka 5z com>



commit 43f19890f5c12248b572e7e17b7a01df1066b768
Author: Jiri (George) Lebl <jiri lebl gmail com>
Date:   Thu Sep 22 17:05:17 2016 -0500

    Thu Sep 22 17:05:13 2016  Jiri (George) Lebl <jirka 5z com>
    
        * src/graphing.c: add "filled" to lines
    
        * examples/riemann-integral.gel: Riemann sums example
    
        * help/C/genius.xml: document "filled"

 ChangeLog                     |    8 +++
 examples/Makefile.am          |    3 +-
 examples/riemann-integral.gel |  108 +++++++++++++++++++++++++++++++++++++++++
 help/C/genius.xml             |    5 ++
 src/graphing.c                |   31 +++++++++--
 5 files changed, 148 insertions(+), 7 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f462d35..03ba2e2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Thu Sep 22 17:05:13 2016  Jiri (George) Lebl <jirka 5z com>
+
+       * src/graphing.c: add "filled" to lines
+
+       * examples/riemann-integral.gel: Riemann sums example
+
+       * help/C/genius.xml: document "filled"
+
 Wed Sep 21 23:47:46 2016  Jiri (George) Lebl <jirka 5z com>
 
        * src/gnome-genius.c, src/examples.[ch]: split up examples into
diff --git a/examples/Makefile.am b/examples/Makefile.am
index d417ce3..5a8d60b 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -26,7 +26,8 @@ example_DATA = \
        complex-analysis-mandelbrot-set.gel \
        complex-analysis-mandelbrot-define.gel \
        complex-analysis-wandering-ball.gel \
-       complex-analysis-mesh.gel
+       complex-analysis-mesh.gel \
+       riemann-integral.gel
 
 EXTRA_DIST = \
        $(example_DATA)
diff --git a/examples/riemann-integral.gel b/examples/riemann-integral.gel
new file mode 100644
index 0000000..9bdc08e
--- /dev/null
+++ b/examples/riemann-integral.gel
@@ -0,0 +1,108 @@
+# Category: Analysis
+# Name: Riemann integral via Riemann sums
+#
+# Plot a function and then by clicking add points to the partition.  Genius
+# will pick random control points in the partition and plot and compute
+# the Riemann sum.  Alternatively it can be put into a mode where new partition points
+# are picked automatically.
+#
+
+function f(x) = x^2+sin(5*x);
+#function f(x) = x^2;
+#function f(x) = 2*x*(1-x);
+#function f(x) = sqrt(x)*sin(27*x);
+#function f(x) = x;
+#function f(x) = if x < 0.5 then -1.0 else 1.0;
+
+limits = [0.0,1.0];
+
+# Use midpoint rather than a random control point
+usemidpoints = false;
+
+# pick points randomly rather than by clicking
+pickpointsrandomly = false;
+
+# wait for a moment after every iteration when picking points
+# randomly
+waitafteriteration = true;
+
+
+
+
+PlotWindowPresent(); # Make sure the window is raised
+
+partition = limits;
+c = [rand()*(limits@(2)-limits@(1))+limits@(1)];
+
+# thousand points seems to get good results for moderately
+# complicated functions
+theintegral = CompositeSimpsonsRule(f, limits@(1), limits@(2), 1000);
+
+fgraph = null;
+for x = limits@(1) to limits@(2) by (limits@(2)-limits@(1))/500 do
+  fgraph = [fgraph;[x,f(x)]];
+
+
+while true do (
+  PlotCanvasFreeze ();
+  LinePlotClear ();
+  
+  thesum = 0.0;
+  largestdeltax = 0.0;
+  for n=1 to elements(partition)-1 do (
+    x0 = partition@(n);
+    x1 = partition@(n+1);
+    deltax = x1-x0;
+    if deltax > largestdeltax then largestdeltax = deltax;
+    
+    if usemidpoints then
+      c@(n) = x0 + deltax/2.0;
+      
+    if elements(c) < n or c@(n) > x1 then (
+      #need to pick a random control point
+      c = SortVector([c,rand()*deltax+x0])
+    );
+    
+    fc = f(c@(n));
+    thesum = thesum + fc*deltax;
+    
+    LinePlotDrawLine([x0,0;x0,fc;x1,fc;x1,0],
+                     "color", "lightgreen",
+                     "filled");
+    LinePlotDrawLine([x0,0;x0,fc;x1,fc;x1,0],
+                     "color", "green",
+                     "thickness", 2);
+    LinePlotDrawLine([c@(n),0;c@(n),fc],
+                     "color", "red",
+                     "thickness", 1)
+  );
+  
+  LinePlotDrawLine(fgraph,
+                   "window", "fit",
+                   "color", "blue",
+                   "thickness", 2,
+                   "legend", "f(x)");
+    
+  PlotCanvasThaw();
+  
+  print ("The riemann sum = " + thesum +
+         "\nNumber of subintervals = " + elements(c) + 
+         "\nLargest delta x = " + largestdeltax +
+         "\nThe actual integral (approximately using Simpsons's rule) = " + theintegral + 
+         "\nError = " + |thesum - theintegral| + 
+         "\n");
+
+  if pickpointsrandomly then (
+    x = rand()*(limits@(2)-limits@(1)) + limits@(1);
+    # Need to wait otherwise it's too fast
+    if waitafteriteration then wait(0.3)
+  ) else (
+    p = LinePlotWaitForClick ();
+    if IsNull(p) then break;
+    x = p@(1)
+  );
+  
+  if not IsIn(x,partition) and limits@(1) < x < limits@(2) then (
+    partition = SortVector([partition,x])
+  )
+);
diff --git a/help/C/genius.xml b/help/C/genius.xml
index 4a1dddf..b05f91b 100644
--- a/help/C/genius.xml
+++ b/help/C/genius.xml
@@ -8833,6 +8833,10 @@ optionally the limits as <userinput>x1,x2,y1,y2</userinput>.
            the color, the thickness, the window
            as 4-vector, type of arrow, or the legend.  (Arrow and window are from version 1.0.6 onwards.)
          </para>
+         <para>
+           If the line is to be treated as a filled polygon, filled with the given color, you
+           can specify the argument <userinput>"filled"</userinput>.  Since version 1.0.22 onwards.
+         </para>
          <para>
            The color should be either a string indicating the common English word for the color
            that GTK will recognize such as 
@@ -8869,6 +8873,7 @@ optionally the limits as <userinput>x1,x2,y1,y2</userinput>.
 <prompt>genius></prompt> <userinput>LinePlotDrawLine([0,0;1,1],"arrow","end")</userinput>
 <prompt>genius></prompt> 
<userinput>LinePlotDrawLine(RungeKuttaFull(`(x,y)=y,0,3,100),"color","blue","legend","The 
Solution")</userinput>
 <prompt>genius></prompt> <userinput>for r=0.0 to 1.0 by 0.1 do 
LinePlotDrawLine([0,0;1,r],"color",[r,(1-r),0.5],"window",[0,1,0,1])</userinput>
+<prompt>genius></prompt> 
<userinput>LinePlotDrawLine([0,0;10,0;10,10;0,10],"filled","color","green")</userinput>
 </screen>
           </para>
          <para>
diff --git a/src/graphing.c b/src/graphing.c
index face762..ff79551 100644
--- a/src/graphing.c
+++ b/src/graphing.c
@@ -3810,7 +3810,7 @@ parametric_get_value (double *x, double *y, double t)
 
 static GtkPlotData *
 draw_line (double *x, double *y, int len, int thickness, GdkColor *color,
-          char *legend)
+          char *legend, gboolean filled)
 {
        GtkPlotData *data;
 
@@ -3835,8 +3835,11 @@ draw_line (double *x, double *y, int len, int thickness, GdkColor *color,
                                           GDK_JOIN_ROUND,
                                           thickness, color);
 
+       gtk_plot_data_fill_area (data, filled);
+
        gtk_widget_show (GTK_WIDGET (data));
 
+
        gtk_plot_canvas_paint (GTK_PLOT_CANVAS (plot_canvas));
        gtk_plot_canvas_refresh (GTK_PLOT_CANVAS (plot_canvas));
 
@@ -4156,7 +4159,11 @@ slopefield_draw_solution (double x, double y, double dx, gboolean is_gui)
        /* Adjust ends */
        /*clip_line_ends (xx, yy, len);*/
 
-       data = draw_line (xx, yy, len, 2 /* thickness */, &color, NULL /*legend*/);
+       data = draw_line (xx, yy, len,
+                         2 /* thickness */,
+                         &color,
+                         NULL /* legend */,
+                         FALSE /* filled */);
        solutions_list = g_slist_prepend (solutions_list,
                                          data);
        g_signal_connect (G_OBJECT (data), "destroy",
@@ -4252,7 +4259,11 @@ vectorfield_draw_solution (double x, double y, double dt, double tlen, gboolean
 
        len = i;
 
-       data = draw_line (xx, yy, len, 2 /* thickness */, &color, NULL /*legend*/);
+       data = draw_line (xx, yy, len,
+                         2 /* thickness */,
+                         &color,
+                         NULL /* legend */,
+                         FALSE /* filled */);
        solutions_list = g_slist_prepend (solutions_list,
                                          data);
        g_signal_connect (G_OBJECT (data), "destroy",
@@ -8767,7 +8778,8 @@ draw_arrowhead (double xx1, double yy1, double xx2, double yy2,
                            ym - cos(angle)* /*aw*/5* thickness / 2.0,
                            & (ax[2]), & (ay[2]));
 
-       draw_line (ax, ay, 3, thickness, color, NULL /*legend*/);
+       draw_line (ax, ay, 3, thickness, color, NULL /* legend */,
+                  FALSE /* filled */);
 } 
 
 static gboolean
@@ -8850,6 +8862,7 @@ LinePlotDrawLine_op (GelCtx *ctx, GelETree * * a, int *exception)
        int thickness;
        gboolean arrow_origin = FALSE;
        gboolean arrow_end = FALSE;
+       gboolean filled = FALSE;
        int i;
        gboolean update = FALSE;
        char *legend = NULL;
@@ -8914,6 +8927,7 @@ LinePlotDrawLine_op (GelCtx *ctx, GelETree * * a, int *exception)
                        static GelToken *bothid = NULL;
                        static GelToken *noneid = NULL;
                        static GelToken *legendid = NULL;
+                       static GelToken *filledid = NULL;
 
                        if (colorid == NULL) {
                                colorid = d_intern ("color");
@@ -8926,6 +8940,7 @@ LinePlotDrawLine_op (GelCtx *ctx, GelETree * * a, int *exception)
                                bothid = d_intern ("both");
                                noneid = d_intern ("none");
                                legendid = d_intern ("legend");
+                               filledid = d_intern ("filled");
                        }
 
                        if (a[i]->type == GEL_STRING_NODE)
@@ -9070,8 +9085,12 @@ LinePlotDrawLine_op (GelCtx *ctx, GelETree * * a, int *exception)
                                        return NULL;
                                }
                                i++;
+                       } else if (id == filledid) {
+                               filled = TRUE;
                        } else {
-                               gel_errorout (_("%s: Unknown style"), "LinePlotDrawLine");
+                               gel_errorout (_("%s: Unknown style: %s"),
+                                             "LinePlotDrawLine",
+                                             id->token);
                                g_free (legend);
                                g_free (x);
                                g_free (y);
@@ -9106,7 +9125,7 @@ LinePlotDrawLine_op (GelCtx *ctx, GelETree * * a, int *exception)
                plot_axis ();
        }
 
-       draw_line (x, y, len, thickness, &color, legend);
+       draw_line (x, y, len, thickness, &color, legend, filled);
 
        if (arrow_end && len > 1)
                draw_arrowhead (x[len-2], y[len-2],


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]