N5110 Library for "Racing Cars" Game

Dependents:   RacingCarsGame

Fork of N5110 by Craig Evans

Revision:
20:a6fc42d720c2
Parent:
19:cf23186a762f
Child:
21:ae05483c1767
--- a/N5110.cpp	Fri May 01 01:29:49 2015 +0000
+++ b/N5110.cpp	Sat May 02 17:31:53 2015 +0000
@@ -351,10 +351,10 @@
 
             int type = (fill==1) ? 1:0;  // black or white fill
 
-            drawLine(x+x0,y+y0,-x+x0,y+y0,type);
-            drawLine(y+x0,x+y0,-y+x0,x+y0,type);
-            drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
-            drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
+            clearLine(x+x0,y+y0,-x+x0,y+y0,type);
+            clearLine(y+x0,x+y0,-y+x0,x+y0,type);
+            clearLine(y+x0,-x+y0,-y+x0,-x+y0,type);
+            clearLine(x+x0,-y+y0,-x+x0,-y+y0,type);
         }
 
 
@@ -424,6 +424,57 @@
 }
 
 
+void N5110::clearLine(int x0,int y0,int x1,int y1,int type)
+{
+    int y_range = y1-y0;  // calc range of y and x
+    int x_range = x1-x0;
+    int start,stop,step;
+    
+    // if dotted line, set step to 2, else step is 1
+    step = (type==2) ? 2:1;
+
+    // make sure we loop over the largest range to get the most pixels on the display
+    // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
+    // or else we'll only end up with 1 pixel in the x column
+    if ( abs(x_range) > abs(y_range) ) {
+
+        // ensure we loop from smallest to largest or else for-loop won't run as expected
+        start = x1>x0 ? x0:x1;
+        stop =  x1>x0 ? x1:x0;
+
+        // loop between x pixels
+        for (int x = start; x<= stop ; x+=step) {
+            // do linear interpolation
+            int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
+
+            if (type == 0)   // if 'white' line, turn off pixel
+                clearPixel(x,y);
+            else
+                clearPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
+
+        }
+    } else {
+
+        // ensure we loop from smallest to largest or else for-loop won't run as expected
+        start = y1>y0 ? y0:y1;
+        stop =  y1>y0 ? y1:y0;
+
+        for (int y = start; y<= stop ; y+=step) {
+            // do linear interpolation
+            int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
+
+            if (type == 0)   // if 'white' line, turn off pixel
+                clearPixel(x,y);
+            else
+                clearPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
+
+        }
+    }
+
+    refresh();
+}
+
+
 void N5110::gameInitial(){
     
     //road lanes