Processing 处理:使用for循环创建多个椭圆
我不熟悉处理,想知道如何在指定的行上创建for循环来创建另外两个椭圆?我想在不影响椭圆轨迹的情况下创建这些椭圆Processing 处理:使用for循环创建多个椭圆,processing,Processing,我不熟悉处理,想知道如何在指定的行上创建for循环来创建另外两个椭圆?我想在不影响椭圆轨迹的情况下创建这些椭圆 int xv = 200; int yv = 20; int xsp = 2; int ysp = 2; void setup() { size(700, 500); } void draw() { background(250); int xcoord = xv; { // x
int xv = 200;
int yv = 20;
int xsp = 2;
int ysp = 2;
void setup() {
size(700, 500);
}
void draw() {
background(250);
int xcoord = xv; { // x position.
**how do i create 2 more ellipses with a for
loop?
int ycoord = yv;
if (xcoord > width || xcoord < 0) { // left, right
walls
xsp = -xsp; // move other direction
}
if (yv > height || yv < 0) { // top, bottom walls
ysp = -ysp; // move in other direction
}
ellipse(xcoord, yv, 20, 20);
xv= xv + xsp; // moves the ellipses
yv = yv + ysp;
}
int xv=200;
int-yv=20;
int xsp=2;
int-ysp=2;
无效设置(){
大小(700500);
}
作废提款(){
背景(250);
int xcoord=xv;{//x位置。
**如何使用for创建另外两个椭圆
环
int ycoord=yv;
如果(xcoord>width | | xcoord<0){//左,右
墙
xsp=-xsp;//向其他方向移动
}
如果(yv>高度| | yv<0){//顶、底墙
ysp=-ysp;//向其他方向移动
}
椭圆(xcoord,yv,20,20);
xv=xv+xsp;//移动椭圆
yv=yv+ysp;
}
在这里,我为您制作了一个数组样式示例:)
int多少个=100;//100?
//这里是空数组
浮动[]xv=新浮动[多少];
浮动[]yv=新浮动[多少];
float[]xsp=新浮动[多少];
浮动[]ysp=新浮动[多少];
无效设置(){
大小(700500);
对于(int i=0;i宽度| xv[i]<0){
xsp[i]=-xsp[i];
}
if(yv[i]>高度| | yv[i]<0){
ysp[i]=-ysp[i];
}
椭圆(xv[i],yv[i],20,20);
xv[i]=xv[i]+xsp[i];
yv[i]=yv[i]+ysp[i];
}
}
以及一个类(对象)示例:
int howMany = 100;// 100?
//here you make a empty array of type Sample
//One array to rule them all :)
Sample[] samples = new Sample[howMany];
void setup() {
size(700, 500);
for (int i = 0; i < samples.length; i++) {
// call the constructor to create each sample with random parameters
samples[i] = new Sample(random(width), random(height), random(-4, 4), random(-4, 4));
}
fill(0,40);
}
void draw() {
background(250);
for (int i = 0; i < samples.length; i++) {
//call methods...
samples[i].display();
samples[i].update();
}
}
class Sample {
//class vars
float xv, yv, xsp, ysp;
// a constructor
Sample(float _xv, float _yv, float _xsp, float _ysp) {
xv = _xv;
yv = _yv;
xsp = _xsp;
ysp = _ysp;
}
void update() {
if (xv > width || xv < 0) {
xsp = -xsp;
}
if (yv > height || yv < 0) {
ysp = -ysp;
}
xv+=xsp;
yv+=ysp;
}
void display() {
ellipse(xv, yv, 20, 20);
}
}
int多少个=100;//100?
//这里创建一个Sample类型的空数组
//一个数组来管理它们:)
样本[]样本=新样本[数量];
无效设置(){
大小(700500);
对于(int i=0;i宽度| xv<0){
xsp=-xsp;
}
如果(yv>高度| | yv<0){
ysp=-ysp;
}
xv+=xsp;
yv+=ysp;
}
无效显示(){
椭圆(xv,yv,20,20);
}
}
好吧,这是一种面向对象的方法……但可以通过添加越来越多的变量来实现,就像您拥有的变量一样。一些事情,比如int xv1/int xv2等等……很无聊……或者制作数组来容纳所有这些,int[]xv=new int[3]……仍然有点无聊,看看这篇文章:还有
int howMany = 100;// 100?
//here you make a empty array of type Sample
//One array to rule them all :)
Sample[] samples = new Sample[howMany];
void setup() {
size(700, 500);
for (int i = 0; i < samples.length; i++) {
// call the constructor to create each sample with random parameters
samples[i] = new Sample(random(width), random(height), random(-4, 4), random(-4, 4));
}
fill(0,40);
}
void draw() {
background(250);
for (int i = 0; i < samples.length; i++) {
//call methods...
samples[i].display();
samples[i].update();
}
}
class Sample {
//class vars
float xv, yv, xsp, ysp;
// a constructor
Sample(float _xv, float _yv, float _xsp, float _ysp) {
xv = _xv;
yv = _yv;
xsp = _xsp;
ysp = _ysp;
}
void update() {
if (xv > width || xv < 0) {
xsp = -xsp;
}
if (yv > height || yv < 0) {
ysp = -ysp;
}
xv+=xsp;
yv+=ysp;
}
void display() {
ellipse(xv, yv, 20, 20);
}
}