Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void stdTriangle(Vertice v1, Vertice v2, Vertice v3) {
- Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
- if (v1.y < v2.y && v1.y < v3.y) { t1 = v1; t2 = (v2.y < v3.y ? v2 : v3); t3 = (v2.y < v3.y ? v3 : v2);}
- else if (v2.y < v3.y) { t1 = v2; t2 = (v1.y < v3.y ? v1 : v3); t3 = (v1.y < v3.y ? v3 : v1); }
- else { t1 = v3; t2 = (v1.y < v2.y ? v1 : v2); t3 = (v1.y < v2.y ? v2 : v1); }
- if (t2.y == t3.y) {
- stdTriangleFlatBottom(t1, t2, t3);
- }
- else if (t1.y == t2.y) {
- stdTriangleFlatTop(t1, t2, t3);
- }
- else {
- Vertice t4 = new Vertice((int)(t1.x + ((float)(t2.y-t1.y) / (float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y);
- stdTriangleFlatBottom(t1, t2, t4);
- stdTriangleFlatTop(t2, t4, t3);
- }
- }
- void stdTriangleFlatBottom(Vertice v1, Vertice v2, Vertice v3) {
- Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
- if (v1.y < v2.y) { if (v1.y < v3.y) { t1 = v1; t2 = (v2.x < v3.x ? v2 : v3); t3 = (v2.x < v3.x ? v3 : v2);}}
- else if (v2.y < v3.y) { t1 = v2; t2 = (v1.x < v3.x ? v1 : v3); t3 = (v1.x < v3.x ? v3 : v1); }
- else { t1 = v3; t2 = (v1.x < v2.x ? v1 : v2); t3 = (v1.x < v2.x ? v2 : v1); }
- float invslope1 = (float)(t2.x - t1.x) / (float)(t2.y - t1.y);
- float invslope2 = (float)(t3.x - t1.x) / (float)(t3.y - t1.y);
- float curx1 = t1.x;
- float curx2 = t1.x;
- for (int scanLineY = t1.y; scanLineY <= t2.y; scanLineY++) {
- HLine((int)curx1, (int)curx2, scanLineY);
- curx1 += invslope1;
- curx2 += invslope2;
- }
- }
- void stdTriangleFlatTop(Vertice v1, Vertice v2, Vertice v3) {
- Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
- if (v1.y > v2.y) { if (v1.y > v3.y) { t3 = v1; t1 = (v2.x < v3.x ? v2 : v3); t2 = (v2.x < v3.x ? v3 : v2);}}
- else if (v2.y > v3.y) { t3 = v2; t1 = (v1.x < v3.x ? v1 : v3); t2 = (v1.x < v3.x ? v3 : v1); }
- else { t3 = v3; t1 = (v1.x < v2.x ? v1 : v2); t2 = (v1.x < v2.x ? v2 : v1); }
- float invslope1 = (float)(t3.x - t1.x) / (float)(t3.y - t1.y);
- float invslope2 = (float)(t3.x - t2.x) / (float)(t3.y - t2.y);
- float curx1 = t3.x;
- float curx2 = t3.x;
- for (int scanLineY = t3.y; scanLineY > t1.y; scanLineY--) {
- HLine((int)curx1, (int)curx2, scanLineY);
- curx1 -= invslope1;
- curx2 -= invslope2;
- }
- }
- void HLine(int x0, int x1, int y) {
- int sx = x0 == x1 ? 0 : (x0<x1?1:-1);
- loadPixels();
- if (sx!=0)
- for (int i = x0; i != x1; i+=sx) {
- pixels[y*width+i] = g.fillColor;
- }
- pixels[y*width+x1] = g.fillColor;
- updatePixels();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement