2013-09-18 3 views
1

Я новичок в обработке и задавался вопросом, как бы создать цикл for на указанной строке, чтобы создать еще 2 эллипса? Я хочу создать эти эллипсы, не мешая траектории эллипсов.Обработка: Создание нескольких эллипсов с помощью цикла

 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; 
    } 
+0

Ну, это требование для объектно-ориентированного подхода ... Но можно сделать только добавление все большего количества варов, как и у вас. Некоторые вещи, такие как int xv1/int xv2 и т. Д. ... скучно ... Или сделать массивы для хранения всего этого, int [] xv = new int [3] ... все еще немного скучно, посмотрите на эту статью: http://wiki.processing.org/w/From_several_variables_to_arrays, а также http://wiki.processing.org/w/From_several_arrays_to_classes –

ответ

2

Здесь я сделал вам образец стиля массива :)

int howMany = 100;// 100? 
//here you make empty arrays 
float[] xv = new float[howMany]; 
float[] yv = new float[howMany]; 
float[] xsp = new float[howMany]; 
float[] ysp = new float[howMany]; 


    void setup() { 
    size(700, 500); 
     for (int i = 0; i < xv.length; i++){ 
     //they all have same length so... all at once 
     // here you populate them... 
     xv [i] = random(width); 
     yv [i] = random(height); 
     xsp[i] = random(-4,4); 
     ysp[i] = random(-4,4); 
     } 
     fill(0,40); 
    } 

    void draw() { 
    background(250); 

    for (int i =0; i < xv.length; i++){ 

     if(xv[i] > width || xv[i] < 0){ 
     xsp[i] = -xsp[i]; 
     } 

     if(yv[i] > height || yv[i] < 0){ 
     ysp[i] = -ysp[i]; 
     } 


     ellipse(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); 
    } 
} 
+0

Я также обновил ответ с помощью образца класса. :) –

Смежные вопросы