Alexandra Posta / Mbed OS ELEC2645_Race_Collision

Dependencies:   ELEC2645_JoystickLCD_LPC1768_2021

Files at this revision

API Documentation at this revision

Comitter:
alex_20
Date:
Thu Apr 22 16:33:42 2021 +0000
Parent:
6:40ef2030334c
Child:
8:1fc5e14b0db6
Commit message:
first ellipse;

Changed in this revision

lib/Ball.h Show annotated file Show diff for this revision Revisions of this file
lib/N5110.cpp Show annotated file Show diff for this revision Revisions of this file
lib/N5110.h Show annotated file Show diff for this revision Revisions of this file
lib/Utils.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/lib/Ball.h	Thu Apr 22 10:52:37 2021 +0000
+++ b/lib/Ball.h	Thu Apr 22 16:33:42 2021 +0000
@@ -5,3 +5,18 @@
 #include "Utils.h"
 #include "N5110.h"
 #include "Vector.h"
+
+class Ball
+{
+public:
+
+    Ball();
+    void init();
+    std::vector<Vector2Df> set_path;
+    void draw(N5110 &lcd, Vector2Df path_points, int type);
+    
+private:
+
+    int _direction;
+};
+#endif 
--- a/lib/N5110.cpp	Thu Apr 22 10:52:37 2021 +0000
+++ b/lib/N5110.cpp	Thu Apr 22 16:33:42 2021 +0000
@@ -402,7 +402,7 @@
         }
 
         y++;
-        if (radiusError<0) {
+        if (radiusError < 0) {
             radiusError += 2 * y + 1;
         } else {
             x--;
@@ -412,7 +412,87 @@
 
 }
 
-// *****************************************************************************
+// function to draw ellipse
+void N5110:: drawEllipse(unsigned int const xc,
+                         unsigned int const yc,
+                         unsigned int const rx,
+                         unsigned int const ry)
+{
+    float dx, dy, d1, d2, x, y;
+    x = 0;
+    y = ry;
+    int radiusError = 1-x;
+  
+    // Initial decision parameter of region 1
+    d1 = (ry * ry) - (rx * rx * ry) + (0.25 * rx * rx);
+    dx = 2 * ry * ry * x;
+    dy = 2 * rx * rx * y;
+  
+    // For region 1
+    while (dx < dy) 
+    {
+        setPixel (xc + x, yc + y,true);
+        setPixel (xc - x, yc + y,true);
+        setPixel (xc + x, yc - y,true);
+        setPixel (xc - x, yc - y,true);
+        
+        if (radiusError < 0) {
+            radiusError += 2 * x + 1;
+        } else {
+            x++;
+            radiusError += 2 * (x - y) + 1;
+        }
+        
+        // Checking and updating value of
+        // decision parameter based on algorithm
+        if (d1 < 0)
+        {
+            dx = dx + (2 * ry * ry);
+            d1 = d1 + dx + (ry * ry);
+        }
+        else 
+        {
+            y--;
+            dx = dx + (2 * ry * ry);
+            dy = dy - (2 * rx * rx);
+            d1 = d1 + dx - dy + (ry * ry);
+        }
+    }
+    
+    // Region 2
+    // Decision parameter of region 2
+    d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + 
+         ((rx * rx) * ((y - 1) * (y - 1))) -
+          (rx * rx * ry * ry);
+  
+    // Plotting points of region 2
+    while (y >= 0)
+    {
+        setPixel (xc + x, yc + y,true);
+        setPixel (xc - x, yc + y,true);
+        setPixel (xc + x, yc - y,true);
+        setPixel (xc - x, yc - y,true);
+        
+        y--;
+        
+        // Checking and updating parameter
+        // value based on algorithm
+        if (d2 > 0) 
+        {
+            dy = dy - (2 * rx * rx);
+            d2 = d2 + (rx * rx) - dy;
+        }
+        else 
+        {
+            x++;
+            dx = dx + (2 * ry * ry);
+            dy = dy - (2 * rx * rx);
+            d2 = d2 + dx - dy + (rx * rx);
+        }
+    }
+}
+
+
 // take points on a curve, decide whether to draw them or not
 void N5110::drawCurve(std::vector<Vector2Df> curve_points, float offset, int dash_len, int type)
 {
--- a/lib/N5110.h	Thu Apr 22 10:52:37 2021 +0000
+++ b/lib/N5110.h	Thu Apr 22 16:33:42 2021 +0000
@@ -384,8 +384,23 @@
                     unsigned int const radius,
                     FillType const     fill);
         
-                    
-    // Draw Curve
+    /** Draw Ellipse
+    *   @param  xc      - x-coordinate of centre
+    *   @param  yc      - y-coordinate of centre
+    *   @param  rx      - radius of the minor axis
+    *   @param  ry      - radius of the major axis
+    */
+    void drawEllipse(unsigned int const xc,
+                     unsigned int const yc,
+                     unsigned int const ry,
+                     unsigned int const rx);
+              
+    /** Draw Curve
+    *   @param  curve_points    - all points that compose the curve
+    *   @param  offset          - parameter for movememt illusion
+    *   @param  dash_line       - lenght of the lines that compose the curve
+    *   @param  type            - 0 white, 1 black
+    */
     void drawCurve(std::vector<Vector2Df> curve_points, 
                    float offset, 
                    int dash_len, 
--- a/lib/Utils.h	Thu Apr 22 10:52:37 2021 +0000
+++ b/lib/Utils.h	Thu Apr 22 16:33:42 2021 +0000
@@ -37,11 +37,6 @@
                                    float const y1,
                                    float const x2,
                                    float const y2);
-
-                        
-    private:
-    
-    int _help;
    
 };
 
--- a/main.cpp	Thu Apr 22 10:52:37 2021 +0000
+++ b/main.cpp	Thu Apr 22 16:33:42 2021 +0000
@@ -105,8 +105,7 @@
         
         car.draw(lcd,start_pos,y);
         
-        printf("m= %d\n\n", magnitude);
-        printf("d= %f\n\n", direction);
+        lcd.drawEllipse(42,20,15,10);
         
         lcd.refresh(); 
     }