【问题标题】:Generalise Bresenham algorithm height octant推广 Bresenham 算法高度八分圆
【发布时间】:2014-03-13 13:05:29
【问题描述】:

我正在尝试实现 Bresenham 算法以获得类似的东西:

因此,我主要测试了很多算法,例如:

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;
    }

}

或者

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
    }
  }

两个八分圆。

我也尝试了我们可以在维基百科中找到的内容,但没什么特别的。

我尝试实现的最后一个功能:

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 ;
    }
}

那么,有人知道任何解决方案吗?

【问题讨论】:

  • 具体问题是什么?你是写了这些函数,还是只是在某个地方找到了它们?参考资料会比复制粘贴更有用,但我们想知道尝试了什么。
  • 我尝试并实现了所有这些功能。我找到了它们并对其进行了改编。
  • 嗯,样本之间的书写风格差异很大。无论如何,那么,具体问题是什么?您特别想解决什么问题?这里完全没有提到问题。
  • 问题是我想画这个圆,但我做不到。这就是我写它们的方式:for(int angle=0; angle &lt; 360; angle +=5) { int x = cos(convertToRadian(angle))*rayon; int y = sin(convertToRadian(angle))*rayon; ptrColor-&gt;line(250, 250, x, y, Color(0,0, 220)); } 250 是中心
  • 嗯,如果有sincos,那不是布雷森汉姆的圆算法。我很困惑,因为您正在讨论八分圆,但代码仅适用于 Bresenham 的线算法。 “我不能”仍然完全不明确;如果你不明白自己在做什么,那就不是真正的编程。

标签: c++ algorithm bresenham generalization


【解决方案1】:

八分圆可以通过一些额外的变量来概括dx0, dy0, dx1, dy1

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

根据八分圆,dx0dy0 之一为零; “增量变量”也可以有负值。

【讨论】:

  • 嗯,按照我的学习方式,您计算单个 delta 值并在每一步绘制 8 个点。如果八分圆在笛卡尔平面上的角度为零,则在每一步增加 y 并在必要时减少 x。反映关于 X 轴和 Y 轴以及线 X = ±Y 的点的结果序列。
  • 我可能对这个问题有不同的理解。绝对可以将一个点镜像到 8 个像素。不过,我不会称之为概括。恰恰相反。
  • 哦,现在我明白你的意思了。是的,这个想法会将单八分圆算法推广到任何单八分圆。然而,现在他说他只是用 sin 和 cos 来得到圈子……现在还不清楚问题是什么。
【解决方案2】:

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 ;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多