0

I'm trying to implement a scan line filling algorithm for random triangles given 3 points as arrays and a color. I'm getting some weird results. Please ignore the optimization of the code, I'm just trying to get it to work and then try to optimize it. I have a method which uses the Bresenham line algorithm to draw lines.  I cannot figure out why I'm getting these results.  http://postimg.org/image/qh1sdizwv/

    var dx1;
    var dx2;
    var dx3;

    var S= new Int32Array([0,0]);
    var E= new Int32Array([0,0]);

    var A=new Int32Array([0,0]);
    var B=new Int32Array([0,0]);
    var C=new Int32Array([0,0]);

    var xmin = Math.min(v0[0],v1[0],v2[0]);
    var xmax = Math.max(v0[0],v1[0],v2[0]);
    var ymin = Math.min(v0[1],v1[1],v2[1]);
    var ymax = Math.max(v0[1],v1[1],v2[1]);

    /*A,B and C are points sorted by y-coordinates
      A[1] is ymin, C[1] is ymax
      S is for start, E is for end*/

    if((ymin==v0[1]) && (ymax==v2[1])){
      A[0]=v0[0];
      A[1]=v0[1];
      B[0]=v1[0];
      B[1]=v1[1];
      C[0]=v2[0];
      C[1]=v2[1];
    }

    if((ymin==v0[1]) && (ymax==v1[1])){
      A[0]=v0[0];
      A[1]=v0[1];
      B[0]=v2[0];
      B[1]=v2[1];
      C[0]=v1[0];
      C[1]=v1[1];
    }

    if((ymin==v1[1]) && (ymax==v2[1])){
      A[0]=v1[0];
      A[1]=v1[1];
      B[0]=v0[0];
      B[1]=v0[1];
      C[0]=v2[0];
      C[1]=v2[1];
    }

    if((ymin==v1[1]) && (ymax==v0[1])){
      A[0]=v1[0];
      A[1]=v1[1];
      B[0]=v2[0];
      B[1]=v2[1];
      C[0]=v0[0];
      C[1]=v0[1];
    }

    if((ymin==v2[1]) && (ymax==v0[1])){
      A[0]=v2[0];
      A[1]=v2[1];
      B[0]=v1[0];
      B[1]=v1[1];
      C[0]=v0[0];
      C[1]=v0[1];
    }

    if((ymin==v2[1]) && (ymax==v1[1])){
      A[0]=v2[0];
      A[1]=v2[1];
      B[0]=v0[0];
      B[1]=v0[1];
      C[0]=v1[0];
      C[1]=v1[1];
    }

    if(B[1]-A[1] >0)
      dx1=((B[0]-A[0])/(B[1]-A[1]));
    else
      dx1=0;

    if(C[1]-A[1] >0)
      dx2=((C[0]-A[0])/(C[1]-A[1]));
    else
      dx2=0;

    if(C[1]-B[1] >0)
      dx3=((C[0]-B[0])/(C[1]-B[1]));
    else
      dx3=0;

    S[0]=A[0];
    S[1]=A[1];
    E[0]=A[0];
    E[1]=A[1];

    if(dx1>dx2){
      for(;S[1]<=B[1]; S[1]++, E[1]++, S[0]=S[0]+dx2, E[0]=E[0]+dx1)
        bresenhamAlg(S,E,color);

      E[0]=B[0];
      E[1]=B[1];
      for(;S[1]<=C[1]; S[1]++, E[1]++, S[0]=S[0]+dx2, E[0]=E[0]+dx3)
        bresenhamAlg(S,E,color);
    }

    else{
      for(;S[1]<=B[1]; S[1]++, E[1]++, S[0]=S[0]+dx1, E[0]=E[0]+dx2)
        bresenhamAlg(S,E,color);

      S[0]=B[0];
      S[1]=B[1];
      for(;S[1]<=C[1]; S[1]++, E[1]++, S[0]=S[0]+dx3, E[0]=E[0]+dx2)
        bresenhamAlg(S,E,color);
    }
  • What's your question? Maybe this belongs over on codereview.stackexchange.com ... – bsa Feb 10 '15 at 06:32
  • I cannot figure out why I'm getting those results. I think you're right, i'll ask my question in codereview! – Yani Mihaylov Feb 10 '15 at 06:36
  • Apparently stack overflow is the website I need because I'm not looking for optimization. I need help with why my code does not work correctly. – Yani Mihaylov Feb 10 '15 at 07:28
  • this http://stackoverflow.com/a/19078088/2521214 might help. anyway first you should visually check if your line rasterization can do the outline correctly prior to filling. so rem the filling code and draw pixles for the outline only if it is OK without holes and in correct order only then move to filling it – Spektre Feb 10 '15 at 08:13
  • I have already checked if the line rasterization works okay and it does. I have noticed the problem only occurs only when there are lines with negative slopes. – Yani Mihaylov Feb 10 '15 at 15:39
  • Here's [another triangle rasterizer](http://webglfundamentals.org/webgl/lessons/resources/fragment-shader-anim.html) and [another](https://github.com/mrdoob/three.js/blob/master/examples/js/renderers/SoftwareRenderer.js) if you're interested – gman Feb 11 '15 at 04:33
  • then just create such case and debug (step/trace) to see what is really happening – Spektre Feb 11 '15 at 08:34

0 Answers0