Prographics 365

openFrameworks/Processing

#Everydays #day1/365

f:id:kzhrknt:20170412153932p:plain

1000個の円のアニメーション。

いま円の個数を全部ベタで書いてるけど、
#define NUM 1000
みたいに定義したほうがスマートだったな。

具体的な数値は、一旦変数にいれられないか考えるようにしよう。

#include "ofApp.h"

float loc_x[1000];[f:id:kzhrknt:20170412153932p:plain]
float loc_y[1000];
float radius[1000];

float speed_x[1000];
float speed_y[1000];

int width;
int height;


//--------------------------------------------------------------
void ofApp::setup(){
    
    ofBackground(0, 0, 0);
    ofSetFrameRate(60);
    ofEnableAlphaBlending();
    ofSetCircleResolution(64);
    
    width = ofGetWidth();
    height = ofGetHeight();
    
    int i;
    
    for (i=0; i < 1000; i++) {
        
        loc_x[i]   = ofRandom(0, width);
        loc_y[i]   = ofRandom(0, height);
        radius[i]  = ofRandom(10, 30);
        
        speed_x[i] = ofRandom(-10, 10);
        speed_y[i] = ofRandom(-10, 10);
    }
    
}

//--------------------------------------------------------------
void ofApp::update(){
    
    for (int i=0; i < 1000; i++) {
    loc_x[i] += speed_x[i];
    loc_y[i] += speed_y[i];
    
    if(loc_x[i] < 0){
        speed_x[i] = -1*speed_x[i];
    }
    
    if(loc_y[i] < 0){
        speed_y[i] = -1*speed_y[i];
    }
    
    if (loc_x[i] > width) {
        speed_x[i] = -1*speed_x[i];
    }
    
    if (loc_y[i] > height) {
        speed_y[i] = -1*speed_y[i];
    }
    }
    
}

//--------------------------------------------------------------
void ofApp::draw(){
    
    ofSetColor(100, 100, 200, 100);
    for (int i = 0; i < 1000; i++) {
        ofDrawCircle(loc_x[i], loc_y[i], radius[i]);
    }
}