ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el17cd
Date:
Fri Feb 22 19:05:28 2019 +0000
Parent:
3:2e31dfcb712a
Child:
5:54d3b36fec2c
Commit message:
Cube Class implemented, when instantiated it created 12 vertices and 6 faces

Changed in this revision

Cube/Cube.cpp Show annotated file Show diff for this revision Revisions of this file
Cube/Cube.h Show annotated file Show diff for this revision Revisions of this file
Face/Face.cpp Show annotated file Show diff for this revision Revisions of this file
Face/Face.h Show annotated file Show diff for this revision Revisions of this file
Rasturizer/Rasturizer.cpp Show annotated file Show diff for this revision Revisions of this file
Rasturizer/Rasturizer.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Cube/Cube.cpp	Fri Feb 22 19:05:28 2019 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "Cube.h"
+
+Cube::Cube(float x, float y, float z, float size){
+    float v[8][3] = {{x-size, y+size, z-size},
+    {x-size, y+size, z+size},
+    {x+size, y+size, z+size},
+    {x+size, y+size, z-size},
+    {x-size, y-size, z-size},
+    {x-size, y-size, z+size},
+    {x+size, y-size, z+size},
+    {x+size, y-size, z-size}};
+    
+    for(int vertex = 0; vertex < 8; vertex++){
+        for(int axis = 0; axis < 3; axis++){
+            verticies[vertex][axis] = v[vertex][axis];
+        }
+    }
+    float f0Points[4][3] = {{v[0][0], v[0][1], v[0][2]}, {v[1][0], v[1][1], v[1][2]}, {v[2][0], v[2][1], v[2][2]}, {v[3][0], v[3][1], v[3][2]}};
+    float f1Points[4][3] = {{v[0][0], v[0][1], v[0][2]}, {v[1][0], v[1][1], v[1][2]}, {v[5][0], v[5][1], v[5][2]}, {v[4][0], v[4][1], v[4][2]}};
+    float f2Points[4][3] = {{v[1][0], v[1][1], v[1][2]}, {v[2][0], v[2][1], v[2][2]}, {v[6][0], v[6][1], v[6][2]}, {v[5][0], v[5][1], v[5][2]}};
+    float f3Points[4][3] = {{v[3][0], v[3][1], v[3][2]}, {v[2][0], v[2][1], v[2][2]}, {v[6][0], v[6][1], v[6][2]}, {v[7][0], v[7][1], v[7][2]}};
+    float f4Points[4][3] = {{v[0][0], v[0][1], v[0][2]}, {v[3][0], v[3][1], v[3][2]}, {v[7][0], v[7][1], v[7][2]}, {v[4][0], v[4][1], v[4][2]}};
+    float f5Points[4][3] = {{v[4][0], v[4][1], v[4][2]}, {v[5][0], v[5][1], v[5][2]}, {v[6][0], v[6][1], v[6][2]}, {v[7][0], v[7][1], v[7][2]}};
+    /*
+    float f0Points[4][3] = {{x-size, y+size, z-size}, {x-size, y+size, z+size}, {x+size, y+size, z+size}, {x+size, y+size, z-size}};
+    float f1Points[4][3] = {{x-size, y+size, z-size}, {x-size, y+size, z+size},{x-size, y-size, z+size}, {x-size, y-size, z-size}};
+    float f2Points[4][3] = {{x-size, y+size, z+size}, {x+size, y+size, z+size}, {x+size, y-size, z+size}, {x-size, y-size, z+size}};
+    float f3Points[4][3] = {{x+size, y+size, z-size}, {x+size, y+size, z+size}, {x+size, y-size, z+size}, {x+size, y-size, z-size}};
+    float f4Points[4][3] = {{x-size, y+size, z-size}, {x+size, y+size, z-size}, {x+size, y-size, z-size}, {x-size, y-size, z-size}};
+    float f5Points[4][3] = {{x-size, y-size, z-size}, {x-size, y-size, z+size}, {x+size, y-size, z+size}, {x+size, y-size, z-size}};
+    */
+    faces[0].setVerticies(f0Points);
+    faces[1].setVerticies(f1Points);
+    faces[2].setVerticies(f2Points);
+    faces[3].setVerticies(f3Points);
+    faces[4].setVerticies(f4Points);
+    faces[5].setVerticies(f5Points);
+}
+
+Face Cube::getFace(int index){
+    return faces[index];
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Cube/Cube.h	Fri Feb 22 19:05:28 2019 +0000
@@ -0,0 +1,14 @@
+#include "mbed.h"
+#ifndef FACE_H
+#define FACE_H
+#include "Face.h"
+#endif
+
+class Cube {
+  private:
+    float verticies[8][3];
+    Face faces[6];
+  public:
+   Cube(float x, float y, float z, float size);
+   Face getFace(int index);
+};
\ No newline at end of file
--- a/Face/Face.cpp	Fri Feb 22 14:01:20 2019 +0000
+++ b/Face/Face.cpp	Fri Feb 22 19:05:28 2019 +0000
@@ -1,13 +1,25 @@
 #include "mbed.h"
+#ifndef FACE_H
+#define FACE_H
 #include "Face.h"
+#endif
 
-Face::Face(float (&PointArray)[4][3]){
+Face::Face(){//float (&PointArray)[4][3]){
+    /*
+    for(int vertex = 0; vertex < 4; vertex++){
+        for(int axis = 0; axis < 3; axis++){
+            verticies[vertex][axis] = PointArray[vertex][axis];
+        }
+    }*/
+}
+void Face::setVerticies(float (&PointArray)[4][3]){
     for(int vertex = 0; vertex < 4; vertex++){
         for(int axis = 0; axis < 3; axis++){
             verticies[vertex][axis] = PointArray[vertex][axis];
         }
     }
 }
-float Face::GetVertexValue(int vertex, int axis){
+
+float Face::getVertexValue(int vertex, int axis){
     return verticies[vertex][axis];
 }
\ No newline at end of file
--- a/Face/Face.h	Fri Feb 22 14:01:20 2019 +0000
+++ b/Face/Face.h	Fri Feb 22 19:05:28 2019 +0000
@@ -4,6 +4,7 @@
   private:
     float verticies[4][3]; 
   public:
-    Face(float (&PointArray)[4][3]);
-    float GetVertexValue(int vertex, int axis);
+    Face(); //float (&PointArray)[4][3]
+    float getVertexValue(int vertex, int axis);
+    void setVerticies(float (&PointArray)[4][3]);
 };
\ No newline at end of file
--- a/Rasturizer/Rasturizer.cpp	Fri Feb 22 14:01:20 2019 +0000
+++ b/Rasturizer/Rasturizer.cpp	Fri Feb 22 19:05:28 2019 +0000
@@ -1,13 +1,20 @@
 #include "mbed.h"
 #include "Rasturizer.h"
 
-
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
 
-Rasturizer::Rasturizer(Face face, N5110 lcd){
+Rasturizer::Rasturizer(){
     lcd.init();
-    float points[4][2] = {{rand()%20,rand()%20},{20+rand()%20, rand()%20}, {20+rand()%20,20+rand()%20}, {rand()%20,20+rand()%20}};
+}
 
+void Rasturizer::drawFace(Face face){
+    float points[4][3];
+    for(int vertex = 0; vertex < 4; vertex++){
+        for(int axis = 0; axis < 3; axis++){
+            points[vertex][axis] = face.getVertexValue(vertex, axis);
+        }
+    }
     int diffX1 = points[0][0]-points[1][0];
     int diffY1 = points[0][1]-points[1][1];
     int diffX2 = points[2][0]-points[3][0];
@@ -23,7 +30,6 @@
             lcd.drawLine(points[0][0]+s, rint(points[0][1]+step*s), rint(points[3][0]-stepSmall1*s), rint(points[3][1]-stepSmall2*s), 0); 
             lcd.drawLine(points[0][0]+s+1, rint(points[0][1]+step*s), rint(points[3][0]-stepSmall1*s-1), rint(points[3][1]-stepSmall2*s), 0); 
         }
-        lcd.printString("a",10,0);
     }
     else{
         lcd.printString("bottom",0,0);
@@ -48,5 +54,11 @@
                  points[3][0],
                  points[3][1],
                  1);
-    
+}
+void Rasturizer::clear(){
+    lcd.clear();
+}
+
+void Rasturizer::refresh(){
+    lcd.refresh();
 }
\ No newline at end of file
--- a/Rasturizer/Rasturizer.h	Fri Feb 22 14:01:20 2019 +0000
+++ b/Rasturizer/Rasturizer.h	Fri Feb 22 19:05:28 2019 +0000
@@ -1,10 +1,18 @@
 #include "mbed.h"
+#include "N5110.h"
+
+#ifndef FACE_H
+#define FACE_H
 #include "Face.h"
-#include "N5110.h"
+#endif
+
 
 
 class Rasturizer {
   public:
-    Rasturizer(Face face, N5110 lcd);
+    Rasturizer();
+    void drawFace(Face face);
+    void clear();
+    void refresh();
 };
 
--- a/main.cpp	Fri Feb 22 14:01:20 2019 +0000
+++ b/main.cpp	Fri Feb 22 19:05:28 2019 +0000
@@ -10,72 +10,27 @@
 
 #include "mbed.h"
 #include <vector>
+#ifndef FACE_H
+#define FACE_H
 #include "Face.h"
+#endif
+#include "Rasturizer.h"
+#include "Cube.h"
 
-#include "N5110.h"
-N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
  
 int main()
 {
-    float p[4][3] = {{1,0,0},{0,0,0},{0,0,0},{0,0,0}};
-    char buffer[50];
-    Face f(p);
-    int n=sprintf (buffer, "%f", f.GetVertexValue(0,0));
-    
-
-    lcd.init();
-
+    Rasturizer renderer;
+    Cube cube(30,30, 1,10);
+    int i = 0;
     while(1) {
-        lcd.clear();
-        for(int i = 0; i<5; i++){
-            float points[4][2] = {{rand()%20,rand()%20},{20+rand()%20, rand()%20}, {20+rand()%20,20+rand()%20}, {rand()%20,20+rand()%20}};
-            
-            float length = sqrt(pow((double)(points[0][0]-points[1][0]),2.0) + pow((double)(points[0][1]-points[1][1]),2));
-            float length1 = sqrt(pow((double)(points[2][0]-points[3][0]),2.0) + pow((double)(points[2][1]-points[3][1]),2));
-    
-            int diffX1 = points[0][0]-points[1][0];
-            int diffY1 = points[0][1]-points[1][1];
-            int diffX2 = points[2][0]-points[3][0];
-            int diffY2 = points[2][1]-points[3][1];
-        
-            if (abs(diffX1) > abs(diffX2)){
-                
-                float step = (float)diffY1/(float)diffX1;
-                float stepSmall1 = (float)diffX2/(float)diffX1;
-                float stepSmall2 = (float)diffY2/(float)diffX1;
-                lcd.printString(buffer,0,0);
-                for(int s = 0; s<= abs(diffX1)-1; s++){
-                    lcd.drawLine(points[0][0]+s, rint(points[0][1]+step*s), rint(points[3][0]-stepSmall1*s), rint(points[3][1]-stepSmall2*s), 0); 
-                    lcd.drawLine(points[0][0]+s+1, rint(points[0][1]+step*s), rint(points[3][0]-stepSmall1*s-1), rint(points[3][1]-stepSmall2*s), 0); 
-                }
-                lcd.printString("a",10,0);
-            }
-            else{
-                lcd.printString(buffer,0,0);
-                float step = (float)diffY2/(float)diffX2;
-                float stepSmall1 = (float)diffX1/(float)diffX2;
-                float stepSmall2 = (float)diffY1/(float)diffX2;
-                
-                for(int s = 0; s<= abs(diffX2)-1; s++){
-                    lcd.drawLine(points[0][0]-stepSmall1*s, rint(points[0][1]-stepSmall2*s),rint(points[3][0]+s), rint(points[3][1]+step*s), 0); 
-                    lcd.drawLine(points[0][0]-stepSmall1*s-1, rint(points[0][1]-stepSmall2*s),rint(points[3][0]+s+1), rint(points[3][1]+step*s), 0); 
-                }
-            }
-            for (int i = 0; i < 3; i++){
-            lcd.drawLine(points[i][0],
-                         points[i][1],
-                         points[i+1][0],
-                         points[i+1][1],
-                         1);
-            }
-            lcd.drawLine(points[0][0],
-                         points[0][1],
-                         points[3][0],
-                         points[3][1],
-                         1);
-        } 
-        lcd.refresh(); // refresh the LCD so the pixels appear
+        renderer.clear();
+        if (i == 6)
+            i = 0;
+        renderer.drawFace(cube.getFace(i));
+        renderer.refresh(); // refresh the LCD so the pixels appear
         wait_ms(1000/1);  // this gives a refresh rate of 10 frames per second
+        i++;
     }
 }
\ No newline at end of file