[gimp-perl] Simplify saw functions.



commit 9ad4f9825e4e70edafddb2a46ae1e56e2dc7947e
Author: Ed J <edj src gnome org>
Date:   Tue Jun 10 04:24:04 2014 +0100

    Simplify saw functions.

 examples/billboard |   23 +++++++----------------
 examples/sethspin  |   13 +++----------
 2 files changed, 10 insertions(+), 26 deletions(-)
---
diff --git a/examples/billboard b/examples/billboard
index 78b6354..9099b77 100755
--- a/examples/billboard
+++ b/examples/billboard
@@ -7,29 +7,20 @@ use strict;
 use warnings;
 use constant PI => 3.14159;
 
-# Uncomment below to spew forth much data about whats going on.
-
+use POSIX qw(fmod);
 sub saw {  # a sawtooth function on PI
-  my $val = @_;
-  if ($val < PI * 0.5) {
-    return $val/PI ;
-  } elsif ($val < PI) {
-    return -1 + $val/PI;
-  } elsif ($val < PI * 1.5) {
-    return $val/PI ;
-  } else {
-    return -1 + $val/PI;
-  }
+  my $val = shift() / PI;
+  return $val - (fmod($val, 1.0) < 0.5 ? 0 : 1);
 }
 
 sub spin_layer { # the function for actually spinning the layer
   my ($img, $spin, $destination, $numframes, $prp, $blinds) = @_;
   # Now lets spin it!
-  $stepsize = 3.14159/$numframes; # in radians
-  for ($i=0; $i<=3.14159; $i+=$stepsize) {
-    Gimp->progress_update ($i/3.14159);
+  $stepsize = PI/$numframes; # in radians
+  for ($i=0; $i<=PI; $i+=$stepsize) {
+    Gimp->progress_update ($i/PI);
     # create a new layer for spinning
-    $framelay = ($i < 3.14159/2.0) ?  $spin->copy(1) : $destination->copy(1);
+    $framelay = ($i < PI/2.0) ?  $spin->copy(1) : $destination->copy(1);
     $img->insert_layer($framelay, 0, 0);
     # spin it a step
     # Here I need to make the proper selection, repeatedly if necessary
diff --git a/examples/sethspin b/examples/sethspin
index 2f627a6..3138799 100755
--- a/examples/sethspin
+++ b/examples/sethspin
@@ -9,17 +9,10 @@ use strict;
 use warnings;
 #$Gimp::verbose = 3;
 
+use POSIX qw(fmod);
 sub saw {  # a sawtooth function on PI
-  my ($val) = @_;
-  if ($val < PI/2.0) {
-    return $val/PI;
-  } elsif ($val < PI) {
-    return $val/PI - 1;
-  } elsif ($val < PI*1.5) {
-    return $val/PI;
-  } else {
-    return $val/PI - 1;
-  }
+  my $val = shift() / PI;
+  return $val - (fmod($val, 1.0) < 0.5 ? 0 : 1);
 }
 
 sub spin_layer { # the function for actually spinning the layer


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