处理 - 线路实施帮助

时间:2010-09-18 09:55:26

标签: java math line processing

我今天跳进Processing(语言)。我一直试图在没有line()函数的情况下实现一行。换句话说,我正在尝试使用自己的代码复制line()函数。我快到了,但不是。 (有一个屏幕,你可以点击,这个功能将这些点击与线连接起来。)

我正在处理四种不同的线斜率(m> 1,0

如果您只是看了下面的代码,并告诉我哪里出错了,我将不胜感激。

int xStart = -1;                // Starting x and y are negative. 
int yStart = -1;                // No lines are drawn when mouseReleased() and x/y are negative.
boolean isReset = false;        // Turns true when 'r' is pressed to reset polygon chain.
int clickCounter = 0;            // Changes background color every 10 clicks (after c is pressed).
color backgroundColor = 0;          // Starting background color. Changed occasionally.
color lineColor = 255;
int weight = 1;

void setup() {
  size(800, 800);            // Initial size is 800x800
  background(backgroundColor);            // ...background is black.
  smooth();                
  stroke(lineColor);               //... lines/points are white.
  strokeWeight(weight);
}

void draw() {
}

void mousePressed(){
  clickCounter++;
}

void mouseReleased(){                // mouseReleased used instead of mousePressed to avoid dragged clicks.
  point(mouseX, mouseY);             // Draws white point at clicked coordinates.
  if((xStart < 0 && yStart < 0) || isReset){      // If x/y negative or if r was pressed, set start points and return. No line drawn.
    xStart = mouseX;
    yStart = mouseY;
    isReset = false;              // Reset isReset to false. 
    return;
  }  
                                                        // Sends starting and ending points to createLine function. 
  createLine(xStart, yStart, mouseX, mouseY);           // createLine(...) - Creates line from start point to end point.  
  xStart = mouseX;                                        // Start point = last click location. End point = Current click location.
  yStart = mouseY;                                   // Sets starting coordinates for next click at current click point.
}

void keyPressed(){
  if(key == 'x')                  // EXTRA CREDIT ADDITION: If x is pressed -> Exit program.
    exit();
  else if(key == 'c'){            // EXTRA CREDIT ADDITTION: If c pressed -> Set background black to clear all lines/points on screen.
    if(clickCounter > 10){
       backgroundColor = color(random(255), random(255), random(255));        // EXTRA CREDIT ADDITION: If cleared and clickCounter is greater 
       clickCounter = 0;                                                        // ...than 10, background changes to random color.
    }
    background(backgroundColor);
    xStart = -1;                // Must set points negative so line is not drawn after next new point is made (since there will only be one point on the screen).
    yStart = -1;
  }
  else if(key == 'r'){          // If r pressed -> Reset: Next click will create new point that isn't connected with line to current points.
    isReset = true;
    lineColor = color(random(255), random(255), random(255));    // EXTRA CREDIT ADDITION: When dot chain is "reset", line changes color.
    weight = (int)random(10);
    strokeWeight(weight);                                    // EXTRA CREDIT ADDITION: ...and line/dot thickness changes.
    stroke(lineColor);
  }                                                                
  else
    return;  
}

// createLine(): Function which draws line from (x0,y0) to (x1,y1).
void createLine(int x0, int y0, int x1, int y1){
  // 1) Line function draws from left to right. (Does not work right to left.) Check and swap points if ending point is left of starting point.
  if(x1 < x0){
    print("LEFT TO RIGHT SWITCH. \n");
    createLine(x1, y1, x0, y0);      // Drawing the line left to right cuts the number of line types we have to deal with to 4 regions.
    return;                              // Regions: slope > 1; 0 < slope < 1; -1 < slope < 0; slope < -1.
  }
  // Declare/Initialize data needed to draw line with midpoint algorithm.
  int dx = x1 - x0;
  int dy = y1 - y0;            //dy = Negative when y0 is lower on screen than y2, because origin is top left.
  print(y0 + "  " + x0 + "  " +y1 + "  " + x1+ "  x    y  \n");
  print(dy + "  " + dx + "  dx    dy\n");
  // Handle vertical & horizontal lines...
  if(dx == 0 || dy == 0){              // If slope is vertical or horizontal, create line with simple function. 
      while(y1 != y0){                    // If vertical -> Paint by incrementing/decrementing y until points connect.
        if(y1 > y0){                          // If new point is above -> Draw upwards.
          y0 = y0 + 1;                
          point(x0, y0);
        }
        else{                                // It new point below -> Draw downwards.
          y0 = y0 - 1;
          point(x0, y0);
        }
      }
      while(x1 != x0){                    // If horizontal -> Paint by incrementing x until points connect (will be left to right line always).
          x0 = x0 + 1;                
          point(x0, y0);
      }
      return;
    }
  // 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 because positive y is downwards on the screen.)
  print("SLOPE CALCULATED: " + m + "\n");
  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(d <= 0){              
              d += deltaE;
              x = x + 1; 
            }
            else{
              d += deltaNE;
              x = x + 1;
              y = y - 1;
            }
        }
        else if(region == 4){    
              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(d <= 0){              
              d += deltaSE;
              x = x + 1; 
              y = y + 1;
            }
            else{
              d += deltaS;
              y = y + 1;
            }
          }
        point(x, y);          
  }
  return;
}

1 个答案:

答案 0 :(得分:0)

当程序暂停时,查找无法正确解析的while()循环。我将以下println语句插入到while循环中以打印出发生的情况。然后我重新创建了有问题的条件并退出程序,并检查控制台是否出现了问题的迹象。

println("top of the while loop to ya...");
println("x: " + x + ", x1: " + x1);
println("region: " + region + ", d: " + d);

看起来区域6导致暂停问题。如果d> 0,它永远不会减少,x永远不会增加,所以无法满足while条件。

使用相同的语句集,您可以解决不准确的行问题。它发生在第4区,但我会把细节留作练习。