代码本色示例学习

随机

在这里插入图片描述

float increment = 0.01;
float zoff = 0.0;  
float zincrement = 0.02; 

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

void draw() {
  background(0);
  
  loadPixels();

  float xoff = 0.0; 
  
  for (int x = 0; x < width; x++) {
    xoff += increment;   
    float yoff = 0.0;   
    for (int y = 0; y < height; y++) {
      yoff += increment; 
      
      float bright = noise(xoff,yoff,zoff)*255;

      pixels[x+y*width] = color(bright,bright,bright);
    }
  }
  updatePixels();
  
  zoff += zincrement; // Increment zoff
}

向量

在这里插入图片描述

Mover mover;

void setup() {
  size(200,200);
  
  mover = new Mover();
}

void draw() {
  background(255);
  
  mover.update();
  mover.checkEdges();
  mover.display();
}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  
  Mover() {
    location = new PVector(random(width),random(height));
    velocity = new PVector(0,0);
    acceleration = new PVector(-0.001,0.01);
    topspeed = 10;
  }
  
  void update() {
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity); 

    println(velocity.mag());
  }
  
  void display() {
    stroke(0);
    fill(175);
    ellipse(location.x,location.y,16,16);
  }
  
  void checkEdges() {
    if(location.x > width ) {
      location.x = 0; 
    } else if(location.x < 0) {
      location.x = width; 
    }
    
    if(location.y > height) {
      location.y = 0; 
    } else if(location.y < 0) {
      location.y = height; 
    }
  }
}

在这里插入图片描述

Mover[] movers = new Mover[20];

void setup() {
  size(383,200);
  randomSeed(1);
  for(int i = 0; i < movers.length;i++) {
    movers[i] = new Mover(random(1,4),0,0); 
  }
}

void draw() {
  background(255);
  
  for(int i = 0; i < movers.length;i++) {
    PVector wind = new PVector(0.01,0);
    PVector gravity = new PVector(0,0.1 * movers[i].mass);
    
    // 摩擦戏数
    float c = 0.05;
    // 正向力
    float normal = 1;
    
    PVector friction = movers[i].velocity.get();
    friction.mult(-1);  // 乘以 -1
    friction.normalize(); // 归一化    
    friction.mult(c);  // 乘以摩擦戏数
    friction.mult(normal);  // 乘以正向力
       
    
    if(movers[i].location.x > width / 2) {
      movers[i].applyForce(friction);  
    }
    
    movers[i].applyForce(wind);
    movers[i].applyForce(gravity);
  
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }
  

}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
      
      println(mag());
    }
  }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;
  
  color c;
  
  Mover(float m,float x,float y) {
    location = new PVector(x,y);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
    mass = m;
    c = color(random(255),random(255),random(255));
  }
  
  void applyForce(PVector force) {
    PVector newVector = force.get();
    newVector.div(mass);
    acceleration.add(newVector);
  }
  
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
  }
  
  void display() {
    stroke(0);
    strokeWeight(2);
    fill(c);
    //fill(random(255),random(255),random(255));
    ellipse(location.x,location.y,mass * 16,mass * 16);
  }
  
  void checkEdges() {
    if(location.x > width) {
      location.x = width;
      velocity.x *= -1;
    } else if(location.x < 0) {
      velocity.x *= -1;
      location.x = 0;
    }
    
    if(location.y > height) {
      velocity.y *= -1;
      location.y = height;
    }
  }
}

震荡

在这里插入图片描述

Oscillator[] oscillators = new Oscillator[200];

void setup() {
  size(800,200);
  background(255);
  for(int i = 0; i < oscillators.length; i++) {
    oscillators[i] = new Oscillator(); 
  }  
}

void draw() {
  background(255);
  for(int i = 0; i < oscillators.length; i++) {
    oscillators[i].oscillate();
    oscillators[i].display();
  }
}

class Oscillator {
  PVector angle;
  PVector velocity;
  PVector amplitude;
  
  color c;
  
  Oscillator() {
    angle = new PVector();
    velocity = new PVector(random(-0.05,0.05),random(-0.05,0.05));
    amplitude = new PVector(random(width / 2),random(height / 2));
    
    c = color(random(255),random(255),random(255));
  }
  
  void oscillate() {
    angle.add(velocity); 
  }
  
  void display() {
    float x = sin(angle.x) * amplitude.x;
    float y = sin(angle.y) * amplitude.y;
    
    pushMatrix();
    translate(width / 2,height / 2);
    stroke(0);
    fill(c);
    line(0,0,x,y);
    ellipse(x,y,16,16);
    popMatrix();
  }
}

class PVector {
  float x;
  float y;
  
  PVector() {
    x = 0;
    y = 0;
  }
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
  
  float heading2D() {
    return atan2(y,x); 
  }
}

粒子系统

在这里插入图片描述

import java.util.Iterator;

ParticleSystem ps;

void setup() {
  size(800,200);
  ps = new ParticleSystem(new PVector(width / 2,50));
}

void draw() {
  background(255);
  ps.addParticle();
  ps.run();
}

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
  
  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }
  
  void addParticle() {
    float r = random(1);
    if(r < 0.5) {
      particles.add(new Particle(origin)); 
    } else {
      particles.add(new Confetti(origin));
    }
  }
  
  void run() {
    Iterator<Particle> it = particles.iterator();
    while(it.hasNext()) {
      Particle p = it.next();
      p.run();
      if(p.isDead()) {
        it.remove(); 
      }
    }
  }
}

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  
  float lifespan;
  
  Particle(PVector l) {
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = l.get();
    lifespan = 255.0;
  }
  
  void run() {
    update();
    display();
  }
  
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display() {
    stroke(0,lifespan);
    strokeWeight(2);
    fill(127,lifespan);
    ellipse(location.x,location.y,12,12);
  }
  
  boolean isDead() {
    if(lifespan < 0.0) {
      return true; 
    } else {
      return false; 
    }
  }
}

class Confetti extends Particle {
  Confetti(PVector l) {
    super(l); 
  }
  
  void display() {
    rectMode(CENTER);
    fill(127,lifespan);
    stroke(0,lifespan);
    strokeWeight(2);
    pushMatrix();
    translate(location.x,location.y);
    float theta = map(location.x,0,width,0,TWO_PI * 2);
    rotate(theta);
    rect(0,0,12,12);
    popMatrix();
  }
}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
  
  float heading2D() {
    return atan2(y,x); 
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要计算出每种颜色T恤的需求量的分布情况。根据题意,红色T恤的需求服从均值为3100,标准差为800的正态分布,而其他三种颜色的T恤需求分别服从均值为300,标准差为200的正态分布。根据正态分布的性质,我们可以计算出每种颜色T恤需求量的概率密度函数和累积分布函数。 1. 没有差异化延迟情况下 在没有差异化延迟的情况下,我们需要制造的T恤数量应该能够满足一定的服务水平CSL。增量分析是一种常用的方法来确定服务水平,它的基本思想是根据历史数据来估计需求分布的参数,然后根据需求分布和服务水平的要求来计算出相应的库存水平。库存水平可以用安全库存量来表示,安全库存量是指在需求不确定的情况下,为了保证服务水平所需要的最小库存量。 假设我们要达到的服务水平是95%,那么我们需要计算出各种颜色T恤的安全库存量。根据库存模型中的公式,T恤的安全库存量等于需求均值加上安全系数乘以需求标准差。安全系数可以根据服务水平和需求分布的特征来确定,常用的安全系数是1.64,它对应着95%的服务水平。 根据上述公式和题意,我们可以计算出各种颜色T恤的安全库存量如下: - 红色T恤:3100 + 1.64 * 800 = 4348.8,取4350 - 黄色T恤:300 + 1.64 * 200 = 632.8,取635 - 绿色T恤:300 + 1.64 * 200 = 632.8,取635 - 黑色T恤:300 + 1.64 * 200 = 632.8,取635 然后,我们需要计算出每种颜色T恤的平均需求量和总需求量。根据正态分布的性质,每种颜色T恤的平均需求量等于其均值,而总需求量可以通过将每种颜色T恤的平均需求量相加来得到。具体计算如下: - 红色T恤平均需求量:3100 - 黄色T恤平均需求量:300 - 绿色T恤平均需求量:300 - 黑色T恤平均需求量:300 总需求量 = 红色T恤平均需求量 + 黄色T恤平均需求量 + 绿色T恤平均需求量 + 黑色T恤平均需求量 = 3100 + 300 + 300 + 300 = 3000 因此,在没有差异化延迟的情况下,需要制造的T恤总数为3000。 2. 有差异化延迟情况下 在有差异化延迟的情况下,我们需要考虑不同颜色T恤的生产时间和成本。由于不染色的本色T恤可以直接生产,因此其生产时间和成本要比染色T恤低。在这种情况下,我们需要考虑如何利用差异化延迟来最小化成本,并且保证能够满足一定的服务水平。 假设我们将黑色、绿色和黄色T恤都生产成本色T恤,并且只对红色T恤进行染色。为了达到95%的服务水平,我们需要计算出每种颜色T恤的安全库存量,然后根据安全库存量和差异化延迟来计算出各种颜色T恤的生产数量。具体步骤如下: 1. 计算安全库存量 根据库存模型中的公式,每种颜色T恤的安全库存量等于需求均值加上安全系数乘以需求标准差。安全系数可以根据服务水平和需求分布的特征来确定。假设我们要达到的服务水平是95%,那么安全系数可以取1.64。 根据上述公式和题意,我们可以计算出各种颜色T恤的安全库存量如下: - 红色T恤:3100 + 1.64 * 800 = 4348.8,取4350 - 黄色T恤:300 + 1.64 * 200 = 632.8,取635 - 绿色T恤:300 + 1.64 * 200 = 632.8,取635 - 黑色T恤:300 + 1.64 * 200 = 632.8,取635 2. 计算生产数量 根据差异化延迟的原则,我们应该先生产成本色T恤,然后根据服务水平和安全库存量来决定生产染色T恤的数量。具体步骤如下: - 首先,我们需要计算出每种颜色T恤的生产时间和成本。假设生产一件染色T恤需要2个单位的时间和30元的成本,而生产一件成本色T恤只需要1个单位的时间和20元的成本。 - 然后,我们计算出对于每件染色T恤,需要生产多少件成本色T恤才能满足服务水平。根据库存模型中的公式,我们可以计算出每件染色T恤对应的生产数量: 生产数量 = 安全库存量 - 已有库存量 - 未来在途量 未来在途量 = 染色T恤的生产时间 / 成本色T恤的生产时间 对于红色T恤,已有库存量和未来在途量都为0,因此生产数量为4350。 对于黄色、绿色和黑色T恤,已有库存量为0,未来在途量为1,因此生产数量为634。 - 最后,我们计算出每种颜色T恤的总生产数量。对于染色T恤,生产数量就是上面计算出来的生产数量;对于成本色T恤,总生产数量等于需求量减去已有库存量和未来在途量。具体计算如下: - 红色T恤:4350(染色T恤) - 黄色T恤:3000 - 0 - 634 = 2366(成本色T恤) - 绿色T恤:3000 - 0 - 634 = 2366(成本色T恤) - 黑色T恤:3000 - 0 - 634 = 2366(成本色T恤) 因此,在有差异化延迟的情况下,需要准备的T恤数量为: - 染色T恤:4350 - 成本色T恤:2366 * 3 = 7098 总共需要制造的T恤数量为4350 + 7098 = 11448。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值