1

I'm trying to implement Bresenham algorithms to have something like :

So in my main, I tested a lot of algorithm something like :

line(ushort x0, ushort y0, ushort x1, ushort y1, const Color couleur)

int dx = abs(x1-x0);
int dy = abs(y1-y0);
int sx,sy;
sx=sy=0;

if(x0 < x1)  sx = 1;
else  sx = -1;

if(y0 < y1) sy = 1;
else  sy = -1;

int err = dx-dy;

while(1)
{
    pixel(x0, y0) = couleur;
    if(x0 == x1 && y0 == y1) break;
    int e2 = 2* err;

    if(e2 > -dy)
    {
        err = err - dy;
        x0  = x0 + sx;
    }

    if(e2 < dy)
    {
        err = err + dx;
        y0  = y0 + sy;
    }

}

Or

ushort x=x1;
 ushort y=y1;
 int longX=x2-x1;
 int longY=y2-y1;

 if(longY<longX)
  { // 1er Octant
   const int c1=2*(longY-longX);
   const int c2=2*longY;
   int critère=c2-longX;
   while(x<=x2)
    {
     DessinePoint(x,y,couleur);
     if(critère>=0)
      { // changement de ligne horizontale
       y++;
       critère=critère+c1;
      }
     else
      // toujours la même ligne horizontale
      critère=critère+c2;
     x++; // ligne suivante, et recommence
    }
  }
 else
  { // 2eme Octant
   const int c1=2*(longX-longY);
   const int c2=2*longX;
   int critère=c2-longY;
   while(y<=y2)
    {
     DessinePoint(x,y,couleur);
     if(critère>=0)
      { // changement de ligne verticale
       x++;
       critère=critère+c1;
      }
     else
      // toujours la même ligne verticale
      critère=critère+c2;
     y++; // ligne suivante, et recommence
    }
  }

for two octants.

I also tried what we can find in wikipedia, but nothing special.

A last function I tried to implement :

line(ushort xi, ushort yi, ushort xf, ushort yf, const Color couleur)
{
int dx,dy,i,xinc,yinc,cumul,x,y ;
x = xi ;
y = yi ;
dx = xf - xi ;
dy = yf - yi ;
xinc = ( dx > 0 ) ? 1 : -1 ;
yinc = ( dy > 0 ) ? 1 : -1 ;
dx = abs(dx) ;
dy = abs(dy) ;
pixel(x,y)= couleur;

if ( dx > dy )
{
    cumul = dx / 2 ;
    for ( i = 1 ; i <= dx ; i++ )
    {
        x += xinc ;
        cumul += dy ;
        if ( cumul >= dx )
        {
            cumul -= dx ;
            y += yinc ;
        }
        pixel(x,y) = couleur ;
    }
}
else
{
    cumul = dy / 2 ;
    for ( i = 1 ; i <= dy ; i++ )
    {
        y += yinc ;
        cumul += dx ;
        if ( cumul >= dy )
        {
            cumul -= dy ;
            x += xinc ;
        }
        pixel(x,y) = couleur ;
    }
}

So , someone know any solution ?

Pépito
  • 57
  • 4
  • 11
  • What is the specific issue? Did you write these functions, or just find them somewhere? The references would be more useful than copy-paste, but what we want to know is what *you* tried. – Potatoswatter Mar 13 '14 at 13:14
  • I tried and implemented all these functions. I found them in general and adapted them. – Pépito Mar 13 '14 at 13:19
  • Well, the written styles differs greatly between the samples. In any case, then, what is the specific issue? What in particular do you want to solve? There is no problem mentioned here at all. – Potatoswatter Mar 13 '14 at 13:22
  • The problem is that i'm trying to draw this cercle, but I can't. This is how I'm writing them : `for(int angle=0; angle < 360; angle +=5) { int x = cos(convertToRadian(angle))*rayon; int y = sin(convertToRadian(angle))*rayon; ptrColor->line(250, 250, x, y, Color(0,0, 220)); }` 250 is the center – Pépito Mar 13 '14 at 13:43
  • 1
    Um, if it has `sin` and `cos`, it's not Bresenham's circle algorithm. I am quite confused, because you're discussing octants but the code is only for Bresenham's line algorithm. "I can't" is still completely unspecific; if you don't understand what you're doing, it's not really programming. – Potatoswatter Mar 13 '14 at 13:53
  • Typically one draws lines as `line(250,250, 250+x, 250+y)`. – Aki Suihkonen Mar 13 '14 at 13:59
  • Pretty clear to me that OP wants a version of the algorithm that works in all octants, i.e. arbitrary slope, as opposed to only shallow and down to the right, and he tried several methods. The circle arrangement is for testing that it works everywhere. – Julian Mann Jul 12 '22 at 02:24

2 Answers2

1

http://tech-algorithm.com/articles/drawing-line-using-bresenham-algorithm/

void LineBresenham(int x,int y,int x2, int y2, int color) 
{
    int w = x2 - x ;
    int h = y2 - y ;
    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;

    int longest = abs(w) ;
    int shortest = abs(h) ;

    if (!(longest>shortest)) 
    {
        longest = abs(h) ;
        shortest = abs(w) ;
        if (h<0) dy2 = -1 ; 
        else if (h>0) dy2 = 1 ;
        dx2 = 0 ;            
    }
    int numerator = longest >> 1 ;
    for (int i=0;i<=longest;i++) 
    {
        PlotPixel(x,y,color) ;
        numerator += shortest ;
        if (!(numerator<longest)) 
        {
            numerator -= longest ;
            x += dx1 ;
            y += dy1 ;
        } else {
            x += dx2 ;
            y += dy2 ;
        }
    }
}
user366312
  • 16,949
  • 65
  • 235
  • 452
0

The octant can be generalized by some extra variables dx0, dy0, dx1, dy1.

if (error < delta)
{
       x += dx0;
       y += dy0;
} else {
       x += dx1;
       y += dy1;
}

Depending on the octant, one of dx0, dy0 is zero; also the "delta variables" can have negative values.

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
  • Hmm, the way I learned, you calculate a single delta value and plot 8 points at each step. If the octant is starting at an angle of zero on the Cartesian plane, then increase y at each step and decrease x if necessary. Reflect the resulting sequence of points about the X and Y axes and the line X = ±Y. – Potatoswatter Mar 13 '14 at 13:18
  • I might have understood the question differently. It's definitely possible to mirror a point to 8 pixels. I wouldn't call that generalization though. It's quite the opposite. – Aki Suihkonen Mar 13 '14 at 13:31
  • Oh, now I see what you mean. Yes, this idea will generalize the one-octant algorithm to any single octant. However, now he says he's just using sin and cos to get the circle… it's very unclear what the question is. – Potatoswatter Mar 13 '14 at 13:56