ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Revision:
14:885915260e25
Parent:
13:f4de03202477
Child:
15:8fbbdefbe720
diff -r f4de03202477 -r 885915260e25 Rasturizer/Rasturizer.cpp
--- a/Rasturizer/Rasturizer.cpp	Sun Mar 24 17:53:25 2019 +0000
+++ b/Rasturizer/Rasturizer.cpp	Sun Mar 31 13:55:51 2019 +0000
@@ -3,27 +3,38 @@
 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
-Serial pc(USBTX, USBRX); // tx, rx
-
 Rasturizer::Rasturizer(){
     lcd.init();
+    fov = 50;
+    /*double tempPoints[4][3] = {{43, 2.5, 30}, {43, 2.5, 200}, {43, -2.5, 200}, {43, -2.5, 30}};
+    for(int vertex = 0; vertex < 8; vertex++){
+        for(int axis = 0; axis < 3; axis++){
+            edgePoints[vertex][axis] = tempPoints[vertex][axis];
+        }
+    }
+    edge.setVerticies(edgePoints);
+    edge.setVisible(true);*/
+}
+
+double Rasturizer::xTo2D(double x, double z){
+    return x * (fov/z) + 42;
+}
+
+double Rasturizer::yTo2D(double y, double z){
+    return y * (fov/z) + 21;
 }
 
 void Rasturizer::drawHorizon(double angle){
-
     lcd.drawLine(0,
              21-rint(angle*50),
              84,
              21+rint(angle*50),
              1);/*
-    double floorPoints[14][3] = {{-120,5, 5}, {-120,0, 1000}, {-80,5, 5}, {-80,0, 1000}, {-40,5, 5}, {-40,0, 1000}, {0,5, 5}, {0,0, 1000}, {40,5, 5}, {40,0, 1000}, {80,5, 5}, {80,0, 1000}, {120,5, 5}, {120,0, 1000}};//{-10,20, 10}, {-10,0, 100}, {0,20, 10}, {0,0, 100}, {10,20, 10}, {10,0, 100}, {20,20, 10}, {20,0, 100},{30,20, 10}, {30,0, 100}};
-    for(int i = 0; i < 14; i += 2){
-        lcd.drawLine(rint(floorPoints[i][0]*(40/floorPoints[i][2]))+42,
-                     rint(floorPoints[i][1]*(40/floorPoints[i][2]))+21,
-                     rint(floorPoints[i+1][0]*(40/floorPoints[i+1][2]))+42,
-                     rint(floorPoints[i+1][1]*(40/floorPoints[i+1][2]))+21,
-                     1);
-    }*/
+    for( int i = 0; i<4; i++){
+        edgePoints[i][0] -= angle*50;
+    }
+    edge.setVerticies(edgePoints);
+    drawFace(edge, angle);*/
 }
 
 void Rasturizer::drawFace(Face face, double angle){
@@ -39,42 +50,46 @@
         points[vertex][1] = y*cos(angle)+x*sin(angle);
     }
     
-    
-    if ((points[0][2] > 20 || points[1][2] > 20 || points[2][2] > 20|| points[3][2] > 20) && face.getVisible()){
-        
-        int diffX1 = points[0][0]*(40/points[0][2])-(points[1][0]*(40/points[1][2]));
-        int diffY1 = points[0][1]*(40/points[0][2])-(points[1][1]*(40/points[1][2]));
-        int diffX2 = points[2][0]*(40/points[2][2])-(points[3][0]*(40/points[3][2]));
-        int diffY2 = points[2][1]*(40/points[2][2])-(points[3][1]*(40/points[3][2]));
+    if ((points[0][2] > 12 || points[1][2] > 12 || points[2][2] > 12|| points[3][2] > 12) && 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]);
         
         double step = (double)diffY2/(double)diffX2;
         double stepSmall1 = (double)diffX1/(double)diffX2;
         double stepSmall2 = (double)diffY1/(double)diffX2;
-        pc.printf("%d\n", diffX2);
         
         for(int s = 0; s< abs(diffX2); s++){
-            lcd.drawLine(rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42,
-             rint((points[0][1])*(40/points[0][2])-stepSmall2*s)+21,
-             rint((points[3][0])*(40/points[3][2])+s)+42, 
-             rint((points[3][1])*(40/points[3][2])+step*s)+21, 0);
+            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(points[i][0]*(40/points[i][2]))+42,
-                     rint(points[i][1]*(40/points[i][2]))+21,
-                     rint(points[i+1][0]*(40/points[i+1][2]))+42,
-                     rint(points[i+1][1]*(40/points[i+1][2]))+21,
-                     1);
-                     
+        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])),
+                rint(xTo2D(points[i+1][0], points[i+1][2])),
+                rint(yTo2D(points[i+1][1], points[i+1][2])),
+                1);
         }
-        lcd.drawLine(rint(points[0][0]*(40/points[0][2]))+42,
-                     rint(points[0][1]*(40/points[0][2]))+21,
-                     rint(points[3][0]*(40/points[3][2]))+42,
-                     rint(points[3][1]*(40/points[3][2]))+21,
-                     1);
+        lcd.drawLine(rint(xTo2D(points[0][0], points[0][2])),
+            rint(yTo2D(points[0][1], points[0][2])),
+            rint(xTo2D(points[3][0], points[3][2])),
+            rint(yTo2D(points[3][1], points[3][2])),
+            1);
     }
 }
 
@@ -88,4 +103,12 @@
 
 void Rasturizer::refresh(){
     lcd.refresh();
+}
+
+void Rasturizer::invertMode(){
+    lcd.inverseMode();
+}
+
+void Rasturizer::normalMode(){
+    lcd.normalMode();
 }
\ No newline at end of file