Martin Deng / Mbed 2 deprecated mbed_shapedriver

Dependencies:   mbed

Revision:
7:2db895370298
Parent:
6:baece3338bfe
--- a/shape_drawer.cpp	Fri May 01 06:46:08 2015 +0000
+++ b/shape_drawer.cpp	Fri May 01 16:17:52 2015 +0000
@@ -5,44 +5,6 @@
 static char work_buffer[SLICES][WIDTH];
 static int (*height)(float d) = NULL;
 
-static void erase(){
-    for(int i = 0; i < SLICES; i++){
-        for(int j = 0; j < WIDTH; j++){
-            work_buffer[i][j] = 0x00;    
-        }    
-    }    
-}
-
-void draw_point(float x1, float y1, float z1, float x2, float y2, float z2, float r){
-    
-}
-
-void draw_circle(float r, float _h){    
-    for(int i = 0; i < SLICES; i++){
-        float h;
-        
-        if(height != NULL)
-            h = height(1.0 * i / SLICES);
-        else
-            h = _h;
-            
-        work_buffer[i][(int)rint(r)] |= (0x01 << (int)rint(h));    
-    }   
-}
-
-#define VERT 1
-#define HORZ 0
-
-static void draw_zvert_line(float x, float y, float z1, float z2);
-static void draw_orig_line(float x1, float x2, float z1, float z2, int ori);
-static void draw_perp_line(float a1, float a2, float b, float z1, float z2, int ori);
-static void draw_arb_line(float x1, float x2, float y1, float y2, float z1, float z2);
-
-static void norm_angle(float &p){
-    p = p < 0 ? p + 2 * M_PI : p;  
-    p = p > 2 * M_PI ? p - 2 * M_PI : p;     
-}
-
 static float deg2rad(float p){
     return 2 * M_PI * p / SLICES;    
 }
@@ -66,6 +28,90 @@
     height = h;    
 }
 
+static void erase(){
+    for(int i = 0; i < SLICES; i++){
+        for(int j = 0; j < WIDTH; j++){
+            work_buffer[i][j] = 0x00;    
+        }    
+    }    
+}
+
+static void draw_circle(float x, float y, float r, float _h){  
+    if( x == 0 && y == 0){  
+        for(int i = 0; i < SLICES; i++){
+            float h;
+            
+            if(height != NULL)
+                h = height(1.0 * i / SLICES);
+            else
+                h = _h;
+                
+            work_buffer[i][(int)rint(r)] |= (0x01 << (int)rint(h));    
+        }  
+    } else {
+        //r = 2 x cos p + 2 y sin p, p from 0 to 180
+        for(int i = 0; i < SLICES / 2; i++){
+            float p = deg2rad(i);
+            float r = 2 * x * cos(p) + 2 * y * sin(p);
+            float h;
+            
+            if(height != NULL)
+                h = height(2.0 * i / SLICES);
+            else
+                h = _h;  
+                
+                    
+            if(r >= 0){
+                work_buffer[i][(int)rint(r)] |= (0x01 << (int)rint(h));    
+            } else {
+                work_buffer[(i + SLICES / 2) % SLICES][(int)rint(r)] |= (0x01 << (int)rint(h));    
+            }  
+        }  
+    } 
+}
+
+void draw_circle(float x, float y, float r, float _h, bool fill){
+    if(!fill){
+        draw_circle(x, y, r, _h);
+    } else {
+        int a = rint(r);
+        for(int i = a; i > 0; i--){
+            draw_circle(x, y, i, _h);    
+        }    
+    }
+}
+
+void draw_point(float x, float y, float z, float r, bool fill){
+    height = NULL;
+    
+    int h1 = (int)rint(z - r);
+    int h2 = (int)rint(z + r);
+    
+    h1 = h1 < 0 ? 0 : h1;
+    h1 = h1 >= HEIGHT ? HEIGHT - 1 : h1;
+    
+    h2 = h2 < 0 ? 0 : h2;
+    h2 = h2 >= HEIGHT ? HEIGHT - 1 : h2;
+    
+    for(int i = h1; i < h2; i++){
+        float r_b = sqrt(r*r - i*i);
+        draw_circle(x, y, i, r_b, fill); 
+    }  
+}
+
+#define VERT 1
+#define HORZ 0
+
+static void draw_zvert_line(float x, float y, float z1, float z2);
+static void draw_orig_line(float x1, float x2, float z1, float z2, int ori);
+static void draw_perp_line(float a1, float a2, float b, float z1, float z2, int ori);
+static void draw_arb_line(float x1, float x2, float y1, float y2, float z1, float z2);
+
+static void norm_angle(float &p){
+    p = p < 0 ? p + 2 * M_PI : p;  
+    p = p > 2 * M_PI ? p - 2 * M_PI : p;     
+}
+
 void draw_line(float x1, float y1, float z1, float x2, float y2, float z2){
     
     //Perfectly vertical depth lines