加工中绘制形状

Drawing a shape in processing

我正在制作有关处理的动画。然后,我有一个关于循环的问题。通常,我的代码比较长。但是,我制作了一个简单的代码,对初学者也很有用。 我的示例代码:

void setup(){
  println("Line between points " + curr + " and " + (curr+1));
  println("initial X: " + initialX + " initial Y: " + initialY );
  println("final   X: " + finalX + "   final Y: " + finalY );
  counter = 0; // reset counter;
}

void draw() {
  point(initialX, initialY);
  println(initialX, initialY, p);

  }

所以,就像你看到的那样,我使用 "Bresenhams Algorithm" 来画线。但是,当我画线时,它不会在点之间画线。只是画了一点点。通常我的文本文件很长。如何绘制可以从第一个 x 和 y 坐标到最后一个 x 和 y 坐标而不断开连接的线?

我尝试更新方法 draw 以更新 deltaY 并继续绘图直到 deltaY != 0 但结果看起来不太好。您很可能需要检查算法的实现和相关计算。

void draw() 
{

  point(initialX, initialY);
  println(initialX, initialY, p);

  if (finalX > initialX )
    initialX++;
  else
    initialX--;

  if (p < 0) {
    p = p + 2 * deltaY;
  } else {
    if (initialY > finalY)
      initialY--;
    else
      initialY++;

    p = p + 2 * deltaY - 2 * deltaX;
  }
  deltaY = abs(finalY - initialY); // update deltaY

  counter++;
  if (counter > deltaX) {
    if (deltaY > 0) {
      counter--;
    } else {
      curr++;
      if (curr == points.length) {
        noLoop(); // possibly you should break out of the main loop here
      } else {
      fixLines();
      }
    }
  }
}

line(initialX, initialY, finalX, finalY); 的实现看起来好多了。

void draw() 
{
  point(initialX, initialY);
  println(initialX, initialY, p);
  line(initialX, initialY, finalX, finalY);
  curr++;
  if (curr == points.length) {
    noLoop();
  } else {
    fixLines();
  }
}

这是 Bresenham 算法的一个版本的实现,使用 balancing the positive and negative error between the x and y coordinates:

/*
String[] coordinates = {    // Creating an array for my text file.
"117 191",
"96 223",
"85 251",
"77 291",
"78 323",
"84 351",
"97 378",
"116 404",
"141 430"
};
*/
int[][] points;

int deltaX, deltaY;
int initialX, initialY;  // Initial point of first coodinate
int finalX, finalY;      // Final point of first coodinate
int counter = 0;
int curr = 0;
int sx, sy, err;

void setup() {

    size(500, 500);

    strokeWeight(4);
    frameRate(25);

    coordinates = loadStrings("coordinates.txt");
    beginShape();         // It combines the all of vertexes

    points = new int[coordinates.length][2];
    int row = 0;
    for (String line : coordinates) {
        String[] pair = line.split(" ");
        points[row] = new int[] { Integer.parseInt(pair[0]), Integer.parseInt(pair[1])};
        println(points[row][0]); // print x
        println(points[row][1]); // print y
        row++;
    }

    fixLines();

    endShape(CLOSE);
}

void fixLines() {
   int ix = curr % points.length;
   int jx = (curr + 1) % points.length;
   initialX = points[ix][0];
   initialY = points[ix][1];
   finalX = points[jx][0];
   finalY = points[jx][1];

   deltaX = abs(finalX - initialX);
   sx = initialX < finalX ? 1: -1;
   deltaY = -abs(finalY - initialY);
   sy = initialY < finalY ? 1: -1;
   err = deltaX + deltaY;

   println("Line between points " + curr + " and " + (curr+1));
   println("[" + initialX + ", " + initialY + "] - [" + finalX + ", " + finalY + "]");
   println("deltaX=" + deltaX);
}

void draw() {

    point(initialX, initialY);
    if (initialX == finalX && initialY == finalY) {
        curr++;
        if (curr == points.length) {
            noLoop();
        } else {
            fixLines();
        }
    } else {
        int e2 = 2 * err;
        if (e2 >= deltaY) {
            err += deltaY;
            initialX += sx;
        }
        if (e2 <= deltaX) {
            err += deltaX;
            initialY += sy;
        }
    }
}

输出非常接近线性实现: