diff src/paranormal/drawing.c @ 193:4b48e6e9b3cb trunk

[svn] - rewrite line drawing algorithm for speed - use lines instead of dots by default in horizontal and vertical waveforms
author nenolod
date Fri, 03 Nov 2006 02:54:12 -0800
parents a7c823478180
children a1be19f8de1f
line wrap: on
line diff
--- a/src/paranormal/drawing.c	Thu Nov 02 22:19:05 2006 -0800
+++ b/src/paranormal/drawing.c	Fri Nov 03 02:54:12 2006 -0800
@@ -1,3 +1,5 @@
+#include <math.h>
+
 #include "paranormal.h"
 #include "actuators.h"
 #include "pn_utils.h"
@@ -12,63 +14,23 @@
   gint x1 = _x1;
   gint y1 = _y1;
 
+  gint dx = x1 - x0;
   gint dy = y1 - y0;
-  gint dx = x1 - x0;
-  gint stepx, stepy;
-  gint fraction;
 
-  if (dy < 0)
-    {
-      dy = -dy;
-      stepy = -(screen->pitch >> 2);
-    }
-  else
-    {
-      stepy = screen->pitch>>2;
-    }
-  if (dx < 0)
-    {
-      dx = -dx;
-      stepx = -1;
-    }
-  else
-    {
-      stepx = 1;
-    }
-  dy <<= 1;
-  dx <<= 1;
-
-  y0 *= screen->pitch>>2;
-  y1 *= screen->pitch>>2;
   pn_image_data->surface[0][PN_IMG_INDEX(x0, y0)] = value;
-  if (dx > dy)
+
+  if (dx != 0)
     {
-      fraction = dy - (dx >> 1);
+      gfloat m = (gfloat) dy / (gfloat) dx;
+      gfloat b = y0 - m * x0;
+
+      dx = (x1 > x0) ? 1 : - 1;
       while (x0 != x1)
-	{
-	  if (fraction >= 0)
-	    {
-	      y0 += stepy;
-	      fraction -= dx;
-	    }
-	  x0 += stepx;
-	  fraction += dy;
-	  pn_image_data->surface[0][PN_IMG_INDEX(x0, y0)] = value;
-	}
-    }
-  else
-    {
-      fraction = dx - (dy >> 1);
-      while (y0 != y1)
-	{
-	  if (fraction >= 0)
-	    {
-	      x0 += stepx;
-	      fraction -= dy;
-	    }
-	  y0 += stepy;
-	  fraction += dx;
-	  pn_image_data->surface[0][PN_IMG_INDEX(x0, y0)] = value;
-	}
+        {
+          x0 += dx;
+          y0 = round(m * x0 + b);
+
+          pn_image_data->surface[0][PN_IMG_INDEX(x0, y0)] = value;
+        }
     }
 }