0

I'm trying to draw(/fill) a triangle, it works.. sometimes, I've got a problem when 2 of the point have y-coordinates that get really close to eachother. the dx/dy gets reaaalllyy big (And could even go infinite..)

Here's the code I use to find the beginning and end of a scanline

//sortedArray contains all 3 of the points, [0] is the point with the highest y-coordinate

double dxDivDy1 = (sortedArray[1].x-sortedArray[0].x)/(sortedArray[1].y-sortedArray[0].y);//[0]-[1] 
double dxDivDy2 = (sortedArray[2].x-sortedArray[0].x)/(sortedArray[2].y-sortedArray[0].y);//[0]-[2] 
ArrayList<Double> startXes = new ArrayList<Double>();                                               
ArrayList<Double> endXes = new ArrayList<Double>();                                                 

startXes.add(new Double(sortedArray[0].x));
endXes.add(new Double(sortedArray[0].x));

double startX1 = transformedTriangle.vertices[0].location.x;//[1]                                   
double startX2  = transformedTriangle.vertices[0].location.x;//[2]                                  
if(sortedArray[1].x > sortedArray[2].x)//[1] > [2]                                                  
{
    for(int y = bY; y < eY; y++)                                                                    
    {

        if(startX1 > sortedArray[1].x){
            dxDivDy1 = (sortedArray[2].x-sortedArray[1].x)/(sortedArray[2].y-sortedArray[1].y);
        }

        if(startX2 < sortedArray[2].x){                                                         
            dxDivDy2 = (sortedArray[1].x-sortedArray[2].x)/(sortedArray[1].y-sortedArray[2].y); 
        }

        startX1 += dxDivDy1;                                                                    
        startX2 += dxDivDy2;                                                                    

        startXes.add(startX2);//[2]                                                             
        endXes.add(startX1);//[1]                                                               
    }
}
else//[2] > [1]                                                                                 
{
    for(int y = bY; y < eY; y++)                                                                    
    {
        if(startX1 < sortedArray[1].x){                                                             
            dxDivDy1 = (sortedArray[1].x-sortedArray[2].x)/(sortedArray[1].y-sortedArray[2].y);
        }

        if(startX2 > sortedArray[2].x){                                                         
            dxDivDy2 = (sortedArray[2].x-sortedArray[1].x)/(sortedArray[2].y-sortedArray[1].y); 
        }

        startX1 += dxDivDy1;                                                                    
        startX2 += dxDivDy2;                                                                    

        startXes.add(startX1);                                                                  
        endXes.add(startX2);                                                                    
    }
}

So, is there a good way to fix this or should I get a different algortim?

Regards.

edit: so basicly, I just traverse the line and keeping adding the dx/dy

edit2: yes, this is java code.

edit3: Anyone knows of a different algorithm, because this seems to have a whole lot of special cases.

user1118321
  • 25,567
  • 4
  • 55
  • 86

0 Answers0