Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Rasturizer/Rasturizer.cpp@13:f4de03202477, 2019-03-24 (annotated)
- Committer:
- el17cd
- Date:
- Sun Mar 24 17:53:25 2019 +0000
- Revision:
- 13:f4de03202477
- Parent:
- 12:b69657862610
- Child:
- 14:885915260e25
Fill algorithm in working condition, massive impact on performance
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17cd | 1:044238f7bdda | 1 | #include "mbed.h" |
| el17cd | 1:044238f7bdda | 2 | #include "Rasturizer.h" |
| el17cd | 2:a5bc7b3779f7 | 3 | |
| el17cd | 4:759a5c34e239 | 4 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17cd | 3:2e31dfcb712a | 5 | |
| el17cd | 12:b69657862610 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
| el17cd | 3:2e31dfcb712a | 7 | |
| el17cd | 4:759a5c34e239 | 8 | Rasturizer::Rasturizer(){ |
| el17cd | 3:2e31dfcb712a | 9 | lcd.init(); |
| el17cd | 4:759a5c34e239 | 10 | } |
| el17cd | 1:044238f7bdda | 11 | |
| el17cd | 11:2cd6341136ca | 12 | void Rasturizer::drawHorizon(double angle){ |
| el17cd | 12:b69657862610 | 13 | |
| el17cd | 11:2cd6341136ca | 14 | lcd.drawLine(0, |
| el17cd | 12:b69657862610 | 15 | 21-rint(angle*50), |
| el17cd | 11:2cd6341136ca | 16 | 84, |
| el17cd | 12:b69657862610 | 17 | 21+rint(angle*50), |
| el17cd | 11:2cd6341136ca | 18 | 1);/* |
| el17cd | 11:2cd6341136ca | 19 | 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}}; |
| el17cd | 11:2cd6341136ca | 20 | for(int i = 0; i < 14; i += 2){ |
| el17cd | 11:2cd6341136ca | 21 | lcd.drawLine(rint(floorPoints[i][0]*(40/floorPoints[i][2]))+42, |
| el17cd | 11:2cd6341136ca | 22 | rint(floorPoints[i][1]*(40/floorPoints[i][2]))+21, |
| el17cd | 11:2cd6341136ca | 23 | rint(floorPoints[i+1][0]*(40/floorPoints[i+1][2]))+42, |
| el17cd | 11:2cd6341136ca | 24 | rint(floorPoints[i+1][1]*(40/floorPoints[i+1][2]))+21, |
| el17cd | 11:2cd6341136ca | 25 | 1); |
| el17cd | 11:2cd6341136ca | 26 | }*/ |
| el17cd | 11:2cd6341136ca | 27 | } |
| el17cd | 11:2cd6341136ca | 28 | |
| el17cd | 11:2cd6341136ca | 29 | void Rasturizer::drawFace(Face face, double angle){ |
| el17cd | 7:15543cb10a14 | 30 | double points[4][3]; |
| el17cd | 4:759a5c34e239 | 31 | for(int vertex = 0; vertex < 4; vertex++){ |
| el17cd | 4:759a5c34e239 | 32 | for(int axis = 0; axis < 3; axis++){ |
| el17cd | 4:759a5c34e239 | 33 | points[vertex][axis] = face.getVertexValue(vertex, axis); |
| el17cd | 4:759a5c34e239 | 34 | } |
| el17cd | 11:2cd6341136ca | 35 | double y = points[vertex][1]; |
| el17cd | 11:2cd6341136ca | 36 | double x = points[vertex][0]; |
| el17cd | 11:2cd6341136ca | 37 | |
| el17cd | 11:2cd6341136ca | 38 | points[vertex][0] = x*cos(angle)-y*sin(angle); |
| el17cd | 11:2cd6341136ca | 39 | points[vertex][1] = y*cos(angle)+x*sin(angle); |
| el17cd | 4:759a5c34e239 | 40 | } |
| el17cd | 6:75031d14fc0d | 41 | |
| el17cd | 11:2cd6341136ca | 42 | |
| el17cd | 13:f4de03202477 | 43 | if ((points[0][2] > 20 || points[1][2] > 20 || points[2][2] > 20|| points[3][2] > 20) && face.getVisible()){ |
| el17cd | 10:07a23afd5088 | 44 | |
| el17cd | 12:b69657862610 | 45 | int diffX1 = points[0][0]*(40/points[0][2])-(points[1][0]*(40/points[1][2])); |
| el17cd | 12:b69657862610 | 46 | int diffY1 = points[0][1]*(40/points[0][2])-(points[1][1]*(40/points[1][2])); |
| el17cd | 12:b69657862610 | 47 | int diffX2 = points[2][0]*(40/points[2][2])-(points[3][0]*(40/points[3][2])); |
| el17cd | 12:b69657862610 | 48 | int diffY2 = points[2][1]*(40/points[2][2])-(points[3][1]*(40/points[3][2])); |
| el17cd | 8:a667bc5050c1 | 49 | |
| el17cd | 9:5915fc800824 | 50 | double step = (double)diffY2/(double)diffX2; |
| el17cd | 9:5915fc800824 | 51 | double stepSmall1 = (double)diffX1/(double)diffX2; |
| el17cd | 9:5915fc800824 | 52 | double stepSmall2 = (double)diffY1/(double)diffX2; |
| el17cd | 12:b69657862610 | 53 | pc.printf("%d\n", diffX2); |
| el17cd | 9:5915fc800824 | 54 | |
| el17cd | 12:b69657862610 | 55 | for(int s = 0; s< abs(diffX2); s++){ |
| el17cd | 12:b69657862610 | 56 | lcd.drawLine(rint((points[0][0])*(40/points[0][2])-stepSmall1*s)+42, |
| el17cd | 12:b69657862610 | 57 | rint((points[0][1])*(40/points[0][2])-stepSmall2*s)+21, |
| el17cd | 12:b69657862610 | 58 | rint((points[3][0])*(40/points[3][2])+s)+42, |
| el17cd | 12:b69657862610 | 59 | rint((points[3][1])*(40/points[3][2])+step*s)+21, 0); |
| el17cd | 7:15543cb10a14 | 60 | } |
| el17cd | 13:f4de03202477 | 61 | } |
| el17cd | 13:f4de03202477 | 62 | if((points[0][2] > 10 || points[1][2] > 10 || points[2][2] > 10|| points[3][2] > 10)){ |
| el17cd | 1:044238f7bdda | 63 | |
| el17cd | 7:15543cb10a14 | 64 | for (int i = 0; i < 3; i++){ |
| el17cd | 6:75031d14fc0d | 65 | |
| el17cd | 7:15543cb10a14 | 66 | lcd.drawLine(rint(points[i][0]*(40/points[i][2]))+42, |
| el17cd | 7:15543cb10a14 | 67 | rint(points[i][1]*(40/points[i][2]))+21, |
| el17cd | 7:15543cb10a14 | 68 | rint(points[i+1][0]*(40/points[i+1][2]))+42, |
| el17cd | 7:15543cb10a14 | 69 | rint(points[i+1][1]*(40/points[i+1][2]))+21, |
| el17cd | 7:15543cb10a14 | 70 | 1); |
| el17cd | 7:15543cb10a14 | 71 | |
| el17cd | 1:044238f7bdda | 72 | } |
| el17cd | 7:15543cb10a14 | 73 | lcd.drawLine(rint(points[0][0]*(40/points[0][2]))+42, |
| el17cd | 7:15543cb10a14 | 74 | rint(points[0][1]*(40/points[0][2]))+21, |
| el17cd | 7:15543cb10a14 | 75 | rint(points[3][0]*(40/points[3][2]))+42, |
| el17cd | 7:15543cb10a14 | 76 | rint(points[3][1]*(40/points[3][2]))+21, |
| el17cd | 7:15543cb10a14 | 77 | 1); |
| el17cd | 1:044238f7bdda | 78 | } |
| el17cd | 4:759a5c34e239 | 79 | } |
| el17cd | 9:5915fc800824 | 80 | |
| el17cd | 11:2cd6341136ca | 81 | void Rasturizer::print(const char *text){ |
| el17cd | 11:2cd6341136ca | 82 | lcd.printString(text, 0, 0); |
| el17cd | 11:2cd6341136ca | 83 | } |
| el17cd | 11:2cd6341136ca | 84 | |
| el17cd | 4:759a5c34e239 | 85 | void Rasturizer::clear(){ |
| el17cd | 4:759a5c34e239 | 86 | lcd.clear(); |
| el17cd | 4:759a5c34e239 | 87 | } |
| el17cd | 4:759a5c34e239 | 88 | |
| el17cd | 4:759a5c34e239 | 89 | void Rasturizer::refresh(){ |
| el17cd | 4:759a5c34e239 | 90 | lcd.refresh(); |
| el17cd | 1:044238f7bdda | 91 | } |