Processing (处理)组PShape getWidth()或宽度为0.0

Processing (处理)组PShape getWidth()或宽度为0.0,processing,Processing,我想得到一组PShape的宽度和高度,但我只得到0.0 例如: // Example code from "https://processing.org/reference/PShape_addChild_.html" PShape house; void setup() { size(200, 200); // Make a group PShape house = createShape(GROUP); // Make three shapes

我想得到一组PShape的宽度和高度,但我只得到0.0

例如:

// Example code from "https://processing.org/reference/PShape_addChild_.html"

PShape house;

void setup() {
  size(200, 200);

  // Make a group PShape
  house = createShape(GROUP);
  
  // Make three shapes
  PShape path = createShape();
  path.beginShape();
  path.vertex(-20, -20);
  path.vertex(0, -40);
  path.vertex(20, -20);
  path.endShape();
  PShape rectangle = createShape(RECT, -20, -20, 40, 40);
  PShape circle = createShape(ELLIPSE, 0, 0, 20, 20);
  
  // Add all three as children
  house.addChild(path);
  house.addChild(rectangle);
  house.addChild(circle);
  
  println(house.width, house.getWidth());
  println(house.height, house.getHeight());
}

void draw() {
  background(52);
  translate(mouseX, mouseY);
  shape(house);
}
在这段代码中,
println(house.width,house.getWidth())
println(house.height,house.getHeight())显示
0.0 0.0

那么,我怎样才能得到任何形状的宽度和高度呢


编辑

根据@rabbi76的建议,我创建了一个getBoundingBoxLimits()函数:

float[][] getBoundingBoxLimits(PShape s){
  float[][] coords = {{1000000.0, -1000000.0}, {1000000.0, -1000000.0}}; 
  // coords represents {{min_x, max_x}, {min_y, max_y}}
  float ii, jj;
  
  for(int i=0; i<1000; i++){
    for(int j=0; i<1000; j++){
      ii = i*1.0;
      jj = j*1.0;
      if(s.contains(ii,jj)){ // contains() expects floats, not ints
        if(ii < coords[0][0]) { coords[0][0] = ii; }
        if(ii > coords[0][1]) { coords[0][1] = ii; }
        if(jj < coords[1][0]) { coords[1][0] = jj; }
        if(jj > coords[1][1]) { coords[1][1] = jj; }         
       }
    }
  }

  return coords;
}
float[][]getBoundingBoxLimits(PShape s){
float[]coords={{1000000.0,-1000000.0},{1000000.0,-1000000.0};
//coords表示{{min_x,max_x},{min_y,max_y}
floatⅡ,jj;
对于(inti=0;i坐标[1][1]){coords[1][1]=jj;}
}
}
}
返回坐标;
}
但是它返回一个
IllegalArgumentException:contains()方法只对路径实现。

所以,乍一看,我似乎无法确定(x,y)点是否位于路径形状以外的任何形状中

无论如何,我希望有一种更直接的方法来获取任何PShape的宽度和高度(因为我确信这些信息存储在这些对象中)。

相关: