Hi,

I have the following working Bresenham's algorithm,which finds intermediate points between two vertices. But this is for a pixel(which has the intermediate point as its center) of size 1.


Code:
void Bresenham(int x1,
    int y1,
    int const x2,
    int const y2)
{
    int delta_x(x2 - x1);
    // if x1 == x2, then it does not matter what we set here
    signed char const ix((delta_x > 0) - (delta_x < 0));
    delta_x = std::abs(delta_x) << 1;

    int delta_y(y2 - y1);
    // if y1 == y2, then it does not matter what we set here
    signed char const iy((delta_y > 0) - (delta_y < 0));
    delta_y = std::abs(delta_y) << 1;

    cout << "(" << x1 << "," << y1 << ")\n";
    //plot(x1, y1);

    if (delta_x >= delta_y)
    {
        // error may go below zero
        int error(delta_y - (delta_x >> 1));

        while (x1 != x2)
        {
            // reduce error, while taking into account the corner case of error == 0
            if ((error > 0) || (!error && (ix > 0)))
            {
                error -= delta_x;
                y1 += iy;
            }
            // else do nothing

            error += delta_y;
            x1 += ix;

            cout << "(" << x1 << "," << y1 << ")\n";
            //plot(x1, y1);
        }
    }
    else
    {
        // error may go below zero
        int error(delta_x - (delta_y >> 1));

        while (y1 != y2)
        {
            // reduce error, while taking into account the corner case of error == 0
            if ((error > 0) || (!error && (iy > 0)))
            {
                error -= delta_y;
                x1 += ix;
            }
            // else do nothing

            error += delta_x;
            y1 += iy;

            cout << "(" << x1 << "," << y1 << ")\n";
            //plot(x1, y1);
        }
    }
}


So,since,in Bresenhams algorithm,I cant control the number of intermediate points, I tried using Linear Interpolation in x and y directions.


Code:
void LinearInterpolation(double x0, double x1, double y0, double y1, int Div)
{
  double x [Div+1];
  double y [Div+1];
  for(int i=1; i<=Div; i++)
  {
     x[i] = x0 + (x1-x0) * i / (Div + 1);
     if(x[i] > x1)
        break;

          y[i] = y0 + (y1-y0) * i / (Div + 1);
     if(y[i] > y1)
        break;

     cout << "(" << x[i] << "," << y[i] << ")\n";
  }
}


So,the above code gets the intermediate points between any two vertices.
Now,the length of the pixel,which is to be created at every intermediate point two vertices is calculated by : length(segment) divided by (No. of IntermediatePoints+1).

But,if I use this for a triangle(with 3 vertices), then if distance between vertex1 and vertex2(connecting edge1); vertex2 and vertex 3(connecting edge2) is different, then even the length of Pieces(or pixels like square) drawn between along edge 1 and edge 2 is different right? if thats the case,how would I apply filling algorithm?

Thanks in Advance
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement