从Path创建Area

时间:2015-02-10 16:47:19

标签: java geometry processing

我想从路径创建一个区域。 如果我将new Rectangle(10, 10, 300, 300)传递给区域构造函数,那么一切正常。如果我通过一条路径,那么区域似乎是空的。

我该如何解决这个问题?

import java.awt.*;
import java.awt.geom.*;

PGraphicsJava2D pg;
Graphics2D g2d;    

Area area;
float ang = 0;

void setup() {
  size(400, 400, JAVA2D);
  pg = (PGraphicsJava2D)g;
  g2d = pg.g2;
  background(255);
  //                             left  top  width height
  //area = new Area(new Rectangle(10, 10, 300, 300));
 // area.subtract(new Area(new Rectangle(0, 0, 50, 50)));
 // area.subtract(new Area(new Rectangle(250, 0, 50, 50)));

 float[][] path = new float[][] {{0,0},{200,0},{200,200},{0,200},{0,0}};

 Path2D.Float p = arrayToPath(path, true);


 println(p == null ? "null" : "not null"); // not null

 // why does this not appear?
 area = new Area(p);

 //area = new Area(new Rectangle(10, 10, 300, 300));

  noLoop();
}

void draw() {
  background(255);
  //translate(width/2, height/2);
  //rotate(ang);
  ang += 0.01f;
  g2d.setColor(Color.cyan);
  g2d.fill(area);
  g2d.setColor(Color.black);
  g2d.draw(area);

  PathIterator pi = area.getPathIterator(null);

  while (!pi.isDone ()) {
    describeCurrentSegment(pi);
    pi.next();
  }
}

public void describeCurrentSegment(PathIterator pi) {
  double[] coordinates = new double[6];
  int type = pi.currentSegment(coordinates);
  switch (type) {
  case PathIterator.SEG_MOVETO:
    System.out.println("move to " + coordinates[0] + ", " + coordinates[1]);
    break;
  case PathIterator.SEG_LINETO:
    System.out.println("line to " + coordinates[0] + ", " + coordinates[1]);
    break;
  case PathIterator.SEG_QUADTO:
    System.out.println("quadratic to " + coordinates[0] + ", " + coordinates[1] + ", "
      + coordinates[2] + ", " + coordinates[3]);
    break;
  case PathIterator.SEG_CUBICTO:
    System.out.println("cubic to " + coordinates[0] + ", " + coordinates[1] + ", "
      + coordinates[2] + ", " + coordinates[3] + ", " + coordinates[4] + ", " + coordinates[5]);
    break;
  case PathIterator.SEG_CLOSE:
    System.out.println("close");
    break;
  default:
    break;
  }
}



 Path2D.Float arrayToPath(float[][] input, boolean includeLastOne) {

      Path2D.Float path = new Path2D.Float();

      path.moveTo(input[0][0], input[0][0]);

      int length = includeLastOne ? input.length : input.length-1;

      for (int i = 1; i < length; i++) {
          path.lineTo(input[i][0], input[i][0]);
      }

      path.closePath();

      return path;

  }

1 个答案:

答案 0 :(得分:0)

此代码适用于我:

import java.awt.*;
import java.awt.geom.*;

PGraphicsJava2D pg;
Graphics2D g2d;    

Area area;
float ang = 0;

void setup() {
  size(400, 400, JAVA2D);
  pg = (PGraphicsJava2D)g;
  g2d = pg.g2;
  background(255);
  //                             left  top  width height
  //area = new Area(new Rectangle(10, 10, 300, 300));
 // area.subtract(new Area(new Rectangle(0, 0, 50, 50)));
 // area.subtract(new Area(new Rectangle(250, 0, 50, 50)));

 float[][] path = new float[][] {{0,0},{200,0},{200,200},{0,200},{0,0}};

 Path2D.Float p = new Path2D.Float();
 p.moveTo(0, 0);
 p.lineTo(100, 0);
 p.lineTo(100, 100);
 p.lineTo(0, 100);
 p.lineTo(0, 0);


 println(p == null ? "null" : "not null"); // not null

 // why does this not appear?
 area = new Area(p);

 //area = new Area(new Rectangle(10, 10, 300, 300));

  noLoop();
}

void draw() {
  background(255);
  //translate(width/2, height/2);
  //rotate(ang);
  ang += 0.01f;
  g2d.setColor(Color.cyan);
  g2d.fill(area);
  g2d.setColor(Color.black);
  g2d.draw(area);

  PathIterator pi = area.getPathIterator(null);

  while (!pi.isDone ()) {
    describeCurrentSegment(pi);
    pi.next();
  }
}

public void describeCurrentSegment(PathIterator pi) {
  double[] coordinates = new double[6];
  int type = pi.currentSegment(coordinates);
  switch (type) {
  case PathIterator.SEG_MOVETO:
    System.out.println("move to " + coordinates[0] + ", " + coordinates[1]);
    break;
  case PathIterator.SEG_LINETO:
    System.out.println("line to " + coordinates[0] + ", " + coordinates[1]);
    break;
  case PathIterator.SEG_QUADTO:
    System.out.println("quadratic to " + coordinates[0] + ", " + coordinates[1] + ", "
      + coordinates[2] + ", " + coordinates[3]);
    break;
  case PathIterator.SEG_CUBICTO:
    System.out.println("cubic to " + coordinates[0] + ", " + coordinates[1] + ", "
      + coordinates[2] + ", " + coordinates[3] + ", " + coordinates[4] + ", " + coordinates[5]);
    break;
  case PathIterator.SEG_CLOSE:
    System.out.println("close");
    break;
  default:
    break;
  }
}

因此,您的arrayToPath()函数存在问题。仔细看看你要添加到路径中的内容:为什么要从(输入[0] [0],输入[0] [0])开始?那将是什么价值?为什么然后lineTo(输入[i] [0],输入[i] [0])?想想这些索引在数组中的位置以及它们的值。