简体   繁体   中英

function to draw a straight line in c

I'm trying to create a function to draw a straight line by reading two points from the user, (x1,y1) where the line begins and (x2,y2) where it ends. here's my function:

    void line(struct pixels* screen)
    {
       float X, Y;
       int i, j, x1, y1, x2, y2, mX, mY;
       scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

       if ((x1 >= 0 && x1 <= screen->width) && (y1 >= 0 && y1 <= screen->height) && (x2 >= 0 && x2 <= screen->width) && (y2 >= 0 && y2 <= screen->height))
      {
         X = (x2 - x1);
         Y = (y2 - y1);
        if (X < 0)
            mX = X*(-1);
        else
            mX = X;

        if (Y < 0)
            mY = Y*(-1);
        else
            mY = Y;

        if( mX>mY )
        {
           if (X > 0)
           {
              for (i = 0; i < X; i++)
              {
                j = (int)(((i*Y) / X) + 0.5);
                screen->pixel[x1 + i][y1 + j] = '*';
              }

            }
            else
           {
             for (i = 0; i > X; i--)
             {
                j = (int)(((i*Y) / X) + 0.5);
                screen->pixel[x1 + i][y1 + j] = '*';
             }
            }

         }
        else
       {
          if (Y > 0)
          {
             for (j = 0; j < Y; j++)
             {
                i = (int)(((j*X) / Y) + 0.5);
                screen->pixel[x1 + i][y1 + j] = '*';
              }
           }
          else
          {
              for (j = 0; j > Y; j--)
              {
                 i = (int)(((j*X) / Y) + 0.5);
                 screen->pixel[x1 + i][y1 + j] = '*';
               }
           }
       }
   }
   else
       printf("ERROR: coordinates exceed the screen limits\n");
  }

the problem is : when the user enters for example line from (1,1) to (10,10) the code works perfectly, but when it's from (10,10) to (1,1) it doesn't work!

Google Bresenham's Line Drawing Algorithm. There is a fantastic tutorial/explanation of how do to this kind of thing at How OpenGL works: software renderer in 500 lines of code . Your specific question is brought up in the article. Highly recommended.

Here is his C++ implementation:

void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { 
    bool steep = false; 
    if (std::abs(x0-x1)<std::abs(y0-y1)) { 
        std::swap(x0, y0); 
        std::swap(x1, y1); 
        steep = true; 
    } 
    if (x0>x1) { 
        std::swap(x0, x1); 
        std::swap(y0, y1); 
    } 
    int dx = x1-x0; 
    int dy = y1-y0; 
    float derror = std::abs(dy/float(dx)); 
    float error = 0; 
    int y = y0; 
    for (int x=x0; x<=x1; x++) { 
        if (steep) { 
            image.set(y, x, color); 
        } else { 
            image.set(x, y, color); 
        } 
        error += derror; 
        if (error>.5) { 
            y += (y1>y0?1:-1); 
            error -= 1.; 
        } 
    } 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM