Advertisement
MMBC

FillTriangle

Aug 1st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. void stdTriangle(Vertice v1, Vertice v2, Vertice v3) {
  2.   Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
  3.   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);}
  4.   else if (v2.y < v3.y) { t1 = v2; t2 = (v1.y < v3.y ? v1 : v3); t3 = (v1.y < v3.y ? v3 : v1); }
  5.   else { t1 = v3; t2 = (v1.y < v2.y ? v1 : v2); t3 = (v1.y < v2.y ? v2 : v1); }
  6.  
  7.   if (t2.y == t3.y) {
  8.     stdTriangleFlatBottom(t1, t2, t3);
  9.   }
  10.   else if (t1.y == t2.y) {
  11.     stdTriangleFlatTop(t1, t2, t3);
  12.   }
  13.   else {
  14.     Vertice t4 = new Vertice((int)(t1.x + ((float)(t2.y-t1.y) / (float)(t3.y-t1.y)) * (t3.x-t1.x)), t2.y);
  15.     stdTriangleFlatBottom(t1, t2, t4);
  16.     stdTriangleFlatTop(t2, t4, t3);
  17.   }
  18. }
  19.  
  20. void stdTriangleFlatBottom(Vertice v1, Vertice v2, Vertice v3) {
  21.   Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
  22.   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);}}
  23.   else if (v2.y < v3.y) { t1 = v2; t2 = (v1.x < v3.x ? v1 : v3); t3 = (v1.x < v3.x ? v3 : v1); }
  24.   else { t1 = v3; t2 = (v1.x < v2.x ? v1 : v2); t3 = (v1.x < v2.x ? v2 : v1); }
  25.  
  26.   float invslope1 = (float)(t2.x - t1.x) / (float)(t2.y - t1.y);
  27.   float invslope2 = (float)(t3.x - t1.x) / (float)(t3.y - t1.y);
  28.  
  29.   float curx1 = t1.x;
  30.   float curx2 = t1.x;
  31.  
  32.   for (int scanLineY = t1.y; scanLineY <= t2.y; scanLineY++) {
  33.     HLine((int)curx1, (int)curx2, scanLineY);
  34.     curx1 += invslope1;
  35.     curx2 += invslope2;
  36.   }
  37. }
  38.  
  39. void stdTriangleFlatTop(Vertice v1, Vertice v2, Vertice v3) {
  40.   Vertice t1 = new Vertice(0,0), t2 = new Vertice(0,0), t3 = new Vertice(0,0);
  41.   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);}}
  42.   else if (v2.y > v3.y) { t3 = v2; t1 = (v1.x < v3.x ? v1 : v3); t2 = (v1.x < v3.x ? v3 : v1); }
  43.   else { t3 = v3; t1 = (v1.x < v2.x ? v1 : v2); t2 = (v1.x < v2.x ? v2 : v1); }
  44.  
  45.   float invslope1 = (float)(t3.x - t1.x) / (float)(t3.y - t1.y);
  46.   float invslope2 = (float)(t3.x - t2.x) / (float)(t3.y - t2.y);
  47.  
  48.   float curx1 = t3.x;
  49.   float curx2 = t3.x;
  50.  
  51.   for (int scanLineY = t3.y; scanLineY > t1.y; scanLineY--) {
  52.     HLine((int)curx1, (int)curx2, scanLineY);
  53.     curx1 -= invslope1;
  54.     curx2 -= invslope2;
  55.   }
  56. }
  57.  
  58. void HLine(int x0, int x1, int y) {
  59.   int sx = x0 == x1 ? 0 : (x0<x1?1:-1);
  60.   loadPixels();
  61.   if (sx!=0)
  62.   for (int i = x0; i != x1; i+=sx) {
  63.     pixels[y*width+i] = g.fillColor;
  64.   }
  65.   pixels[y*width+x1] = g.fillColor;
  66.   updatePixels();
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement