Processing 在加工过程中如何使椭圆尺寸自动变小变大

Processing 在加工过程中如何使椭圆尺寸自动变小变大,processing,Processing,我正在使用类来创建一些东西,以适应它们,我正在尝试获得我所绘制的椭圆的大小,以使其自身变得越来越小,从而使其看起来更具交互性,但我不知道要实现什么代码 我已经在下面展示了我正在使用的代码 void setup() { 尺寸(640480); } 作废提款() { 背景(255); 对于(int i=height-50;i>0;i-=20) { 填充(随机(255)、随机(255)、随机(255)); 椭圆(宽/2,高/2,i,i); } } 您只需将圆的宽度和高度存储在一个变量中,在draw

我正在使用类来创建一些东西,以适应它们,我正在尝试获得我所绘制的椭圆的大小,以使其自身变得越来越小,从而使其看起来更具交互性,但我不知道要实现什么代码

我已经在下面展示了我正在使用的代码

void setup()
{
尺寸(640480);
}
作废提款()
{  
背景(255);
对于(int i=height-50;i>0;i-=20)
{
填充(随机(255)、随机(255)、随机(255));
椭圆(宽/2,高/2,i,i);
}
}

您只需将圆的宽度和高度存储在一个变量中,在
draw()
函数中使用该变量,然后随时更改圆的大小即可

下面是一个在单击鼠标时更改圆大小的示例程序:

浮动半径=50;
无效设置(){
大小(500500);
椭圆模型(半径);
}
void mouseClicked(){
半径=250.0*mouseX/宽度;
}
作废提款(){
背景(0);
椭圆(宽度/2,高度/2,半径,半径);
}
下面是一个自动增大和缩小圆的示例程序:

浮动半径=0;
布尔增长=真;
无效设置(){
大小(500500);
椭圆模型(半径);
}
作废提款(){
如果(成长){
radius++;
如果(半径==250){
增长=错误;
}
}否则{
半径--;
如果(半径==0){
成长=真实;
}
}
背景(0);
椭圆(宽度/2,高度/2,半径,半径);
}

但这两个例子的想法都是一样的:只需将圆半径存储在一个变量中,更改该变量以更改大小,然后使用该变量绘制圆。

您只需将圆的宽度和高度存储在一个变量中,然后在
draw()
函数中使用该变量,然后,只要您想更改圆的大小,就可以随时更改它

下面是一个在单击鼠标时更改圆大小的示例程序:

浮动半径=50;
无效设置(){
大小(500500);
椭圆模型(半径);
}
void mouseClicked(){
半径=250.0*mouseX/宽度;
}
作废提款(){
背景(0);
椭圆(宽度/2,高度/2,半径,半径);
}
下面是一个自动增大和缩小圆的示例程序:

浮动半径=0;
布尔增长=真;
无效设置(){
大小(500500);
椭圆模型(半径);
}
作废提款(){
如果(成长){
radius++;
如果(半径==250){
增长=错误;
}
}否则{
半径--;
如果(半径==0){
成长=真实;
}
}
背景(0);
椭圆(宽度/2,高度/2,半径,半径);
}

但这两个例子的想法都是一样的:只需将圆半径存储在一个变量中,更改该变量以更改大小,然后使用该变量绘制圆。

为了补充Kevin的伟大答案,这里有两个相同想法的变体(跟踪大小是否增加)

第一种方法涉及使用增长速度变量,当达到极限时,该变量只需翻转符号。如果大小太小(下限)或太大(上限),翻转符号(增长速度乘以-1)将变为增长,反之亦然:

//current size - continuously updated
float size = 10;
//minimum size
float minSize = 10;
//maximum size
float maxSize = 240;
//change speed for size (how much will the size increase/decrease each frame)
float sizeSpeed = 1.5;

void setup()
{
  size(640, 480);
}

void draw()
{  
  //if the size is either too small, or too big, flip the size speed sign (if it was positive (growing) - make it negative (shrink) - and vice versa)
  if(size < minSize || size > maxSize) {
    sizeSpeed *= -1;
  }
  //increment the size with the size speed (be it positive or negative)
  size += sizeSpeed;

  background(255); 
  fill(random(255), random(255), random(255));
  ellipse(width/2, height/2, size,size);

}
更新就类而言,您可以开始将用于绘制椭圆的变量封装到类中。语法没有那么复杂:

  • 使用
    class
    关键字,然后是类的名称(按惯例为大写),并在
    {}
    -类范围内添加类成员
  • 使用
    new
    关键字创建类的实例,并使用
    符号访问实例成员
  • 下面是一个使用上述方法的基本示例:

    Ellipse e = new Ellipse();
    
    void setup()
    {
      size(640, 480);
    }
    
    void draw()
    {  
      e.size = map(sin(frameCount * e.sizeSpeed),-1.0,1.0,e.minSize,e.maxSize);
    
      background(255); 
      fill(random(255), random(255), random(255));
      ellipse(width/2, height/2, e.size,e.size);
    
    }
    
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
    }
    
    这是一个很好且简单的开始,但绘图部分没有封装。 与在类中声明和使用变量的方式相同,您可以声明和使用函数。下面是一个简单地将椭圆绘图功能移动到函数中的基本示例:

    Ellipse e = new Ellipse();
    
    void setup()
    {
      size(640, 480);
    }
    
    void draw()
    {  
    
      background(255); 
      e.render();
    
    }
    
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
      void render(){
        size = map(sin(frameCount * sizeSpeed),-1.0,1.0,minSize,maxSize);
        fill(random(255), random(255), random(255));
        ellipse(width/2, height/2, size,size);
      }
    }
    
    正如您所注意到的,现在需要两条线来初始化和渲染主草图中的椭圆。还有一些地方可以改进。如果创建第二个椭圆,则x和y是相同的,因此它们会相互遮挡。向类中添加x,y属性将意味着每个实例将具有独立的坐标。 类似地,
    frameCount
    是全局的,这意味着每个椭圆使用相同的值 角度/尺寸。我们也可以让它独立

    Ellipse e1 = new Ellipse(320,240);
    Ellipse e2 = new Ellipse(20,20);
    
    void setup()
    {
      size(640, 480);
    }
    
    void draw()
    {  
    
      background(255);
      e1.render(); 
      e2.render();
    
    }
    
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
      //position
      float x,y;
    
      //internal frameCount replacement
      int tick;
    
      //constructor
      Ellipse(float x,float y){
        this.x = x;//copy x argument value to the instance (this) x property 
        this.y = y;//copy x argument value to the instance (this) x property
      }
    
      void render(){
        tick++;
        size = map(sin(tick * sizeSpeed),-1.0,1.0,minSize,maxSize);
        fill(random(255), random(255), random(255));
        ellipse(x,y, size,size);
      }
    }
    
    请注意,我们还添加了一个构造函数。构造函数是非常特殊的:它就像一个新实例的入口点。每当您创建一个类的新实例时,就会调用构造函数,它通常会初始化数据。上一个示例没有显式定义构造函数,但处理提供了一个默认无参数的构造函数。为了进一步了解上述概念,我们创建了一个具有x,y坐标的构造函数:

    Ellipse e1 = new Ellipse(320,240);
    Ellipse e2 = new Ellipse(20,20);
    
    void setup()
    {
      size(640, 480);
    }
    
    void draw()
    {  
    
      background(255);
      e1.render(); 
      e2.render();
    
    }
    
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
      //position
      float x,y;
    
      //internal frameCount replacement
      int tick;
    
      //constructor
      Ellipse(float x,float y){
        this.x = x;//copy x argument value to the instance (this) x property 
        this.y = y;//copy x argument value to the instance (this) x property
      }
    
      void render(){
        tick++;
        size = map(sin(tick * sizeSpeed),-1.0,1.0,minSize,maxSize);
        fill(random(255), random(255), random(255));
        ellipse(x,y, size,size);
      }
    }
    
    这是一个非常快速的概述。有关更多详细信息,请务必查看

    考虑到这一点,您可以在运行时使用创建新椭圆,并对椭圆参数进行一些有趣的随机化:

    ArrayList<Ellipse> ellipses = new ArrayList<Ellipse>();
    
    void setup()
    {
      size(640, 480);
      ellipses.add(new Ellipse(width / 2, height / 2));
    }
    
    void draw()
    {  
    
      background(255);
      for(Ellipse e : ellipses){
        e.render();
      }
    }
    //add an ellipse every 5th frame if mouse is dragged
    void mouseDragged(){
      if(frameCount % 5 == 0) ellipses.add(new Ellipse(mouseX,mouseY));
    }
    //remove all ellipses if SPACE is pressed
    void keyPressed(){
      if(key == ' ') ellipses.clear();
    }
    
    //ellipse class
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
      //position
      float x,y;
    
      //internal frameCount replacement
      int tick;
    
      int fill;
    
    
      //constructor
      Ellipse(float x,float y){
        this.x = x;//copy x argument value to the instance (this) x property 
        this.y = y;//copy x argument value to the instance (this) x property
        fill = color(random(32,192));
        sizeSpeed = random(0.025,0.01);
        maxSize = random(120,240);
      }
    
      void render(){
        tick++;
        size = map(sin(tick * sizeSpeed),-1.0,1.0,minSize,maxSize);
        fill(fill);
        ellipse(x,y, size,size);
      }
    }
    
    ArrayList椭圆=新的ArrayList();
    无效设置()
    {
    尺寸(640480);
    添加(新的椭圆(宽度/2,高度/2));
    }
    作废提款()
    {  
    背景(255);
    对于(椭圆e:椭圆){
    e、 render();
    }
    }
    //如果拖动鼠标,则每隔5帧添加一个椭圆
    void mouseDragged(){
    如果(帧数%5==0)椭圆。添加(新椭圆(mouseX,mouseY));
    }
    //如果按空格键,请删除所有椭圆
    按下void键(){
    if(key='')省略号.clear();
    }
    //椭圆类
    类椭圆{
    //当前大小-不断更新
    浮点数=10;
    //最小尺寸
    float minSize=10;
    //最大尺寸
    浮点最大值=240;
    //更改大小的速度(每帧大小将增加/减少多少)
    浮子尺寸速度=0.025;
    //位置
    浮动x,y;
    //内部帧计数替换
    整数勾号;
    
    ArrayList<Ellipse> ellipses = new ArrayList<Ellipse>();
    
    void setup()
    {
      size(640, 480);
      ellipses.add(new Ellipse(width / 2, height / 2));
    }
    
    void draw()
    {  
    
      background(255);
      for(Ellipse e : ellipses){
        e.render();
      }
    }
    //add an ellipse every 5th frame if mouse is dragged
    void mouseDragged(){
      if(frameCount % 5 == 0) ellipses.add(new Ellipse(mouseX,mouseY));
    }
    //remove all ellipses if SPACE is pressed
    void keyPressed(){
      if(key == ' ') ellipses.clear();
    }
    
    //ellipse class
    class Ellipse{
      //current size - continuously updated
      float size = 10;
      //minimum size
      float minSize = 10;
      //maximum size
      float maxSize = 240;
      //change speed for size (how much will the size increase/decrease each frame)
      float sizeSpeed = 0.025;
    
      //position
      float x,y;
    
      //internal frameCount replacement
      int tick;
    
      int fill;
    
    
      //constructor
      Ellipse(float x,float y){
        this.x = x;//copy x argument value to the instance (this) x property 
        this.y = y;//copy x argument value to the instance (this) x property
        fill = color(random(32,192));
        sizeSpeed = random(0.025,0.01);
        maxSize = random(120,240);
      }
    
      void render(){
        tick++;
        size = map(sin(tick * sizeSpeed),-1.0,1.0,minSize,maxSize);
        fill(fill);
        ellipse(x,y, size,size);
      }
    }