ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Revision:
21:6b5d2d75e083
Parent:
20:3ca430241df0
Child:
22:236319885874
--- a/Rasturizer/Rasturizer.cpp	Mon Apr 01 17:11:40 2019 +0000
+++ b/Rasturizer/Rasturizer.cpp	Tue Apr 02 17:40:59 2019 +0000
@@ -1,5 +1,6 @@
 #include "Rasturizer.h"
 
+Serial pc(USBTX, USBRX); // tx, rx
 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
@@ -25,53 +26,63 @@
 
 void Rasturizer::drawHorizon(float angle){
     lcd.drawLine(0,
-             21-rint(angle*50),
+             21-rint(angle*40),
              84,
-             21+rint(angle*50),
+             21+rint(angle*40),
              1);
 }
 
-void Rasturizer::drawFace(Face face, float angle){
+void Rasturizer::drawFace(Face *face, float angle){
     float points[4][3];
+    float y;
+    float x;
+    
+    int diffX1;
+    int diffY1;
+    int diffX2;
+    int diffY2;
+    
+    float step;
+    float stepSmall1;
+    float stepSmall2;
     for(int vertex = 0; vertex < 4; vertex++){
         for(int axis = 0; axis < 3; axis++){
-            points[vertex][axis] = face.getVertexValue(vertex, axis);
+            points[vertex][axis] = face->getVertexValue(vertex, axis);
         }
-        float y = points[vertex][1];
-        float x = points[vertex][0];
+        y = points[vertex][1];
+        x = points[vertex][0];
         
         points[vertex][0] = x*cos(angle)-y*sin(angle);
         points[vertex][1] = y*cos(angle)+x*sin(angle);
     }
-    
-    if (checkOnScreen(points) && face.getVisible()){
-        int diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]);
-        int diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]);
-        int diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]);
-        int diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]);
-        
-        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); s++){
-            if(diffX2 > 0){
-                lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
-                    rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s),
-                    rint(xTo2D(points[3][0], points[3][2])+s), 
-                    rint(yTo2D(points[3][1], points[3][2])+step*s),
-                    0);
-            }
-            else{
-                lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
-                    rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s),
-                    rint(xTo2D(points[3][0], points[3][2])-s), 
-                    rint(yTo2D(points[3][1], points[3][2])-step*s),
-                    0);
+
+    if (checkOnScreen(points)){
+        diffX1 = xTo2D(points[0][0], points[0][2])-xTo2D(points[1][0], points[1][2]);
+        diffY1 = yTo2D(points[0][1], points[0][2])-yTo2D(points[1][1], points[1][2]);
+        diffX2 = xTo2D(points[2][0], points[2][2])-xTo2D(points[3][0], points[3][2]);
+        diffY2 = yTo2D(points[2][1], points[2][2])-yTo2D(points[3][1], points[3][2]);
+        if(diffX2 != 0 && face->getVisible()){
+            step = (float)diffY2/(float)diffX2;
+            stepSmall1 = (float)diffX1/(float)diffX2;
+            stepSmall2 = (float)diffY1/(float)diffX2;
+            
+            for(int s = 0; s< abs(diffX2); s++){
+                if(diffX2 > 0){
+                    lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])-stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
+                        rint(yTo2D(points[0][1], points[0][2])-stepSmall2*s),
+                        rint(xTo2D(points[3][0], points[3][2])+s), 
+                        rint(yTo2D(points[3][1], points[3][2])+step*s),
+                        0);
+                }
+                else{
+                    lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])+stepSmall1*s),//rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
+                        rint(yTo2D(points[0][1], points[0][2])+stepSmall2*s),
+                        rint(xTo2D(points[3][0], points[3][2])-s), 
+                        rint(yTo2D(points[3][1], points[3][2])-step*s),
+                        0);
+                }
             }
         }
-    }
-    if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){
         for (int i = 0; i < 3; i++){     
             lcd.drawLine(rint(xTo2D(points[i][0], points[i][2])),
                 rint(yTo2D(points[i][1], points[i][2])),
@@ -84,25 +95,31 @@
             rint(xTo2D(points[3][0], points[3][2])),
             rint(yTo2D(points[3][1], points[3][2])),
             1);
+
     }
+    //if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){
+    //}
+    
+
 }
-void Rasturizer::drawAllFaces(Face faceArray[], int noOfCubes, float angle){
+void Rasturizer::drawAllFaces(Face *faceArray, int noOfCubes, float angle){
+    Face temp;
     for (int f = 0; f< (noOfCubes*6)-1; f++){
         for (int f2 = 0; f2< (noOfCubes*6)-f-1; f2++){
             if(faceArray[f2].getAvgZ() < faceArray[f2+1].getAvgZ()){
-                Face temp = faceArray[f2+1];
+                temp = faceArray[f2+1];
                 faceArray[f2+1] = faceArray[f2];
                 faceArray[f2] = temp;
             }
         }
     }
     for (int f = 0; f< noOfCubes*6 ; f++){
-        drawFace(faceArray[f], angle/15);
+        drawFace(&faceArray[f], angle/15);
     }
 }
 
 bool Rasturizer::checkOnScreen(float (&points)[4][3]){
-    if (points[0][2] < 10 || points[1][2] < 10 || points[2][2] < 10 || points[3][2] < 10){
+    if (points[0][2] < 6 || points[1][2] < 6 || points[2][2] < 6 || points[3][2] < 6){
         return false;
     }
     else if ((xTo2D(points[0][0], points[0][2]) < 0 ||  xTo2D(points[0][0], points[0][2]) > 84)