处理/线图 - 帮助

发布于 2024-09-24 04:56:16 字数 3647 浏览 6 评论 0原文

我昨天发了类似的帖子,但什么也没得到。我今天花了几个小时解决问题,但没有取得任何进展。

我正在使用处理(语言)并尝试实现一种在两点之间画一条线的方法。 (我不想使用库的 line() 方法。)

我的 lineCreate 方法对于正斜率非常有效,但对于负斜率则失败。你能帮忙找出原因吗?

这是 lineCreate() 代码:

void createLine(int x0, int y0, int x1, int y1){
   //...
  // Handle slanted lines...
  double tempDX = x1 - x0;
  double tempDY = y1 - y0;            // Had to create dx and dy as doubles because typecasting dy/dx to a double data type wasn't working.
  double m = (-tempDY / tempDX);      // m = line slope. (Note - The dy value is negative 
  int deltaN = (2 * -dx);        // deltaX is the amount to increment d after choosing the next pixel on the line.
  int deltaNE = (2 * (-dy - dx));      // ...where X is the direction moved for that next pixel. 
  int deltaE = (2 * -dy);            // deltaX variables are used below to plot line.
  int deltaSE = (2 * (dy + dx));
  int deltaS = (2 * dx);
  int x = x0; 
  int y = y0;
  int d = 0;                            // d = Amount d-value changes from pixel to pixel. Depends on slope.
  int region = 0;                  // region = Variable to store slope region. Different regions require different formulas.
 if(m > 1){                            // if-statement: Initializes d, depending on the slope of the line.
      d = -dy - (2 * dx);                  // If slope is 1-Infiniti. -> Use NE/N initialization for d.
      region = 1;
  }
  else if(m == 1)
    region = 2;
  else if(m > 0 && m < 1){
      d = (2 * -dy) - dx;                  // If slope is 0-1 -> Use NE/E initialization for d.
      region = 3;
  }
  else if(m < 0 && m > -1){          
      d = (2 * dy) + dx;                  // If slope is 0-(-1) -> Use E/SE initliazation for d.
      region = 4;
  }
  else if(m == -1)
    region = 5;
  else if(m < -1){
      d = dy + (2 * dx);                  // If slope is (-1)-(-Infiniti) -> Use SE/S initialization for d.
      region = 6;
  }
  while(x < x1){                    // Until points are connected...
        if(region == 1){          // If in region one...
              if(d <= 0){                // and d<=0...
              d += deltaNE;            // Add deltaNE to d, and increment x and y.
              x = x + 1; 
              y = y - 1;
            }
            else{              
              d += deltaN;        // If d > 0 -> Add deltaN, and increment y.
              y = y - 1;
            }
        }
        else if(region == 2){
             x = x + 1;
             y = y - 1; 
        }
        else if(region == 3){      // If region two...
                if(d <= 0){              
              d += deltaE;
              x = x + 1; 
            }
            else{
              d += deltaNE;
              x = x + 1;
              y = y - 1;
            }
        }
        else if(region == 4){        // If region three...
              if(d <= 0){              
              d += deltaSE;
              x = x + 1; 
              y = y + 1;
            }
            else{
              d += deltaE;
              x = x + 1;
            }
        }
        else if(region == 5){
             x = x + 1;
             y = y + 1; 
        }
        else if(region == 6){        // If region four...
              if(d <= 0){              
              d += deltaSE;
              x = x + 1; 
              y = y + 1;
            }
            else{
              d += deltaS;
              y = y + 1;
            }
          }
        point(x, y);          // Paints new pixel on line going towards (x1,y1).
  }
  return;
}

I posted something similar yesterday, but got nothing. I spent a few hours today problem-solving, but didn't progress any.

I'm using Processing (the language) and trying to implement a method that draws a line between two points. (I don't want to use the library's line() method.)

My lineCreate method works great for positive slopes, but fails with negative slopes. Can you help figure out why?

Here's the lineCreate() code:

void createLine(int x0, int y0, int x1, int y1){
   //...
  // Handle slanted lines...
  double tempDX = x1 - x0;
  double tempDY = y1 - y0;            // Had to create dx and dy as doubles because typecasting dy/dx to a double data type wasn't working.
  double m = (-tempDY / tempDX);      // m = line slope. (Note - The dy value is negative 
  int deltaN = (2 * -dx);        // deltaX is the amount to increment d after choosing the next pixel on the line.
  int deltaNE = (2 * (-dy - dx));      // ...where X is the direction moved for that next pixel. 
  int deltaE = (2 * -dy);            // deltaX variables are used below to plot line.
  int deltaSE = (2 * (dy + dx));
  int deltaS = (2 * dx);
  int x = x0; 
  int y = y0;
  int d = 0;                            // d = Amount d-value changes from pixel to pixel. Depends on slope.
  int region = 0;                  // region = Variable to store slope region. Different regions require different formulas.
 if(m > 1){                            // if-statement: Initializes d, depending on the slope of the line.
      d = -dy - (2 * dx);                  // If slope is 1-Infiniti. -> Use NE/N initialization for d.
      region = 1;
  }
  else if(m == 1)
    region = 2;
  else if(m > 0 && m < 1){
      d = (2 * -dy) - dx;                  // If slope is 0-1 -> Use NE/E initialization for d.
      region = 3;
  }
  else if(m < 0 && m > -1){          
      d = (2 * dy) + dx;                  // If slope is 0-(-1) -> Use E/SE initliazation for d.
      region = 4;
  }
  else if(m == -1)
    region = 5;
  else if(m < -1){
      d = dy + (2 * dx);                  // If slope is (-1)-(-Infiniti) -> Use SE/S initialization for d.
      region = 6;
  }
  while(x < x1){                    // Until points are connected...
        if(region == 1){          // If in region one...
              if(d <= 0){                // and d<=0...
              d += deltaNE;            // Add deltaNE to d, and increment x and y.
              x = x + 1; 
              y = y - 1;
            }
            else{              
              d += deltaN;        // If d > 0 -> Add deltaN, and increment y.
              y = y - 1;
            }
        }
        else if(region == 2){
             x = x + 1;
             y = y - 1; 
        }
        else if(region == 3){      // If region two...
                if(d <= 0){              
              d += deltaE;
              x = x + 1; 
            }
            else{
              d += deltaNE;
              x = x + 1;
              y = y - 1;
            }
        }
        else if(region == 4){        // If region three...
              if(d <= 0){              
              d += deltaSE;
              x = x + 1; 
              y = y + 1;
            }
            else{
              d += deltaE;
              x = x + 1;
            }
        }
        else if(region == 5){
             x = x + 1;
             y = y + 1; 
        }
        else if(region == 6){        // If region four...
              if(d <= 0){              
              d += deltaSE;
              x = x + 1; 
              y = y + 1;
            }
            else{
              d += deltaS;
              y = y + 1;
            }
          }
        point(x, y);          // Paints new pixel on line going towards (x1,y1).
  }
  return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不弃不离 2024-10-01 04:56:16

看看这个页面。它通过代码示例解释了线条绘制背后的整个理论。

有许多已知的画线算法。请此处了解它们。

Have a look at this page. It explains the whole theory behind line drawing with code examples.

There are a number of known algorithm for line drawing. Read about them here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文