ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Rasturizer/Rasturizer.cpp

Committer:
el17cd
Date:
2019-02-23
Revision:
7:15543cb10a14
Parent:
6:75031d14fc0d
Child:
8:a667bc5050c1

File content as of revision 7:15543cb10a14:

#include "mbed.h"
#include "Rasturizer.h"

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);


Rasturizer::Rasturizer(){
    lcd.init();
}

void Rasturizer::drawFace(Face face){
    double points[4][3];
    for(int vertex = 0; vertex < 4; vertex++){
        for(int axis = 0; axis < 3; axis++){
            points[vertex][axis] = face.getVertexValue(vertex, axis);
        }
    }
    //double avgZ = points[0][2] + points[1][2] + points[2][2] + points[3][2];//not needed if no rotation
    
    if (points[0][2] > 5 || points[1][2] > 5 || points[2][2] > 5|| points[3][2]>5){
        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)){
            
            double step = (double)diffY1/(double)diffX1;
            double stepSmall1 = (double)diffX2/(double)diffX1;
            double stepSmall2 = (double)diffY2/(double)diffX1;
            lcd.printString("top",0,0);
            for(int s = 0; s<= abs(diffX1)-1; s++){
                lcd.drawLine((rint(points[0][0]+s)/points[0][2])+42, rint((points[0][1]+step*s)/points[0][2]), rint((points[3][0]-stepSmall1*s)/points[3][2])+42, rint((points[3][1]-stepSmall2*s)/points[3][2])+21, 0); 
                lcd.drawLine((rint(points[0][0]+s+1)/points[0][2])+42, rint((points[0][1]+step*s)/points[0][2]), rint((points[3][0]-stepSmall1*s-1)/points[3][2])+42, rint((points[3][1]-stepSmall2*s)/points[3][2])+21, 0); 
                
                //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); 
                
            }
        }
        else{
            lcd.printString("bottom",0,0);
            double step = (double)diffY2/(double)diffX2;
            double stepSmall1 = (double)diffX1/(double)diffX2;
            double stepSmall2 = (double)diffY1/(double)diffX2;
            
            for(int s = 0; s<= abs(diffX2)-1; s++){
                lcd.drawLine(rint((points[0][0]-stepSmall1*s)*(40/points[0][2]))+42, rint((points[0][1]-stepSmall2*s)*(40/points[0][2]))+21,rint((points[3][0]+s)*(40/points[3][2]))+42, rint((points[3][1]+step*s)*(40/points[3][2]))+21, 0); 
                lcd.drawLine(rint((points[0][0]-stepSmall1*s-1)*(40/points[0][2])+42), rint((points[0][1]-stepSmall2*s)*(40/points[0][2])+21),rint((points[3][0]+s+1)*(40/points[3][2]))+42, rint((points[3][1]+step*s)*(40/points[3][2]))+21, 0); 
            }
        }*/
        
        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);
                     
        }
        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);
    }
}
void Rasturizer::clear(){
    lcd.clear();
}

void Rasturizer::refresh(){
    lcd.refresh();
}