如何在处理中从 pshape 调整子形状的大小

How to resize a children shape from a pshape in Processing

我目前正在使用 Processing 构建应用程序。 我有一个形状,我可以在其中 select 它的子元素并操纵它的颜色和描边......但我无法调整文件的每个元素的大小。 基本上,我想要的是像这样调整整个东西的大小:

shape(shape, mouseX, mouseY, 600, 600);//parameters: shape, x-coor, y-coor, new width, new height;

并且,更改每个元素的颜色(就像上面的行)。 代码:

PShape shape;
int noOfChilds;
/***************************************************************************/
void setup() 
{
  size(1000, 600);
  shape = loadShape("a.svg");
  noOfChilds = shape.getChildCount();
  print(noOfChilds);
  shapeMode(CENTER);
} 
/***************************************************************************/
void draw() 
{
  background(100);
  shape.enableStyle();
  stroke(255);
  shape(shape, mouseX, mouseY, 600, 600);
  //
  shape.disableStyle();
  for(int i = 0; i < noOfChilds; i++)
  {
    pushMatrix();
    translate(300, 300);
    PShape ps = shape.getChild(i);
    fill(random(255),random(255),random(255));
    shape(ps, 0, 0);//shape(ps, 0, 0, anyValue, anyValue); seems to fail :'(
    popMatrix();
  }
}
/***************************************************************************/

您可以使用 PShape#scale() 函数来扩展单个 PShape 个实例。

来自the reference

PShape s;

void setup() {
  s = loadShape("bot.svg");
}

void draw() {
  background(204);
  shape(s);
}

void mousePressed() {
  // Shrink the shape 90% each time the mouse is pressed
  s.scale(0.9);  
}

Increases or decreases the size of a shape by expanding and contracting vertices. Shapes always scale from the relative origin of their bounding box. Scale values are specified as decimal percentages. For example, the method call scale(2.0) increases the dimension of a shape by 200%. Subsequent calls to the method multiply the effect. For example, calling scale(2.0) and then scale(1.5) is the same as scale(3.0). This transformation is applied directly to the shape; it's not refreshed each time draw() is run.

您还可以使用更通用的 scale() 函数。可以在 the reference.

中找到更多信息

请注意,您可能还需要调整绘制形状的位置,可能需要使用 translate() 函数。