Skip to content

Instantly share code, notes, and snippets.

@micromeeeter
Created August 2, 2017 05:06
Show Gist options
  • Save micromeeeter/b72fa07b8c1485fe0a6a47671e05ecfa to your computer and use it in GitHub Desktop.
Save micromeeeter/b72fa07b8c1485fe0a6a47671e05ecfa to your computer and use it in GitHub Desktop.
traffic information (oFでクラスの練習 SFC17春グラフィックスプログラミング課題)
#include "ofMain.h"
#include "ofApp.h"
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(191, 216, 28);
ofSetFrameRate(60);
patchLength = 150;
for(int i = 0; i < 50; i++){
particles[i].patchLength = patchLength;
}
}
//--------------------------------------------------------------
void ofApp::update(){
for(int i = 0; i < 50; i++){
particles[i].update();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofFill();
ofSetColor(180, 180, 180);
for(int i = 0; i < ofGetWidth() / patchLength+1; i++){
ofDrawRectangle(patchLength * i-25, 0, 50, ofGetHeight());
}
for(int i = 0; i < ofGetHeight() / patchLength+1; i++){
ofDrawRectangle(0, patchLength * i-25, ofGetWidth(), 50);
}
ofNoFill();
ofSetColor(255, 255, 255);
ofSetLineWidth(3);
for(int i = 0; i < ofGetWidth() / patchLength+1; i++){
for(int j = 0; j < ofGetHeight() / patchLength; j++){
ofDrawRectangle(i*patchLength+20, j*patchLength+20, 110, 110);
ofPushMatrix();
ofTranslate(i*patchLength, j*patchLength);
ofTranslate(-38, -9);
for(int a = 0; a < 4; a++){
ofDrawLine(0, 7*a, 20, 7*a);
}
ofPopMatrix();
ofPushMatrix();
ofTranslate(i*patchLength, j*patchLength);
ofTranslate(18, -9);
for(int a = 0; a < 4; a++){
ofDrawLine(0, 7*a, 20, 7*a);
}
ofPopMatrix();
}
}
ofFill();
for(int i = 0; i < 50; i++){
particles[i].draw();
}
// ofSaveFrame();
}
#pragma once
#include "ofMain.h"
#include "particle.hpp"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
int patchLength;
Particle particles[50];
};
//
// particle.cpp
// graphicsPrograming_assingment_oF02
//
// Created by Momoha Matsuhashi on 2017/06/15.
//
//
#include "particle.hpp"
//constructor 初期値
Particle::Particle(){
//initializeion
patchLength = 150;
pos_x = int(ofRandom( ofGetWidth() / patchLength)) * patchLength;
pos_y = int(ofRandom(ofGetHeight() / patchLength)) * patchLength;
direction = ofRandom(0, 4);
speed = 2.0;
radius = 6.0;
color = ofColor::fromHsb(ofRandom(0, 255), 180, 255);
}
void Particle::update(){
for(int i = 0; i < ofGetWidth() / patchLength+1; i++){
for(int j = 0; j < ofGetHeight() / patchLength+1; j++){
if(pos_x == patchLength * i && pos_y == patchLength * j){
direction = ofRandom(0, 4);
}
}
}
switch (direction) {
case 0:
pos_x += speed;
break;
case 1:
pos_y += speed;
break;
case 2:
pos_x -= speed;
break;
case 3:
pos_y -= speed;
break;
}
if(pos_x < 0) pos_x = ofGetWidth();
if(pos_x > ofGetWidth()) pos_x = 0;
if(pos_y < 0) pos_x = ofGetHeight();
if(pos_y > ofGetHeight()) pos_y = 0;
}
void Particle::draw(){
ofSetColor(color);
ofDrawCircle(pos_x, pos_y, radius);
}
//
// particle.hpp
// graphicsPrograming_assignment_oF02
//
// Created by Momoha Matsuhashi on 2017/06/15.
//
//
#ifndef particle_hpp
#define particle_hpp
#include <stdio.h>
#include <iostream>
#include <ofMain.h>
class Particle{
public:
//member 属性
float pos_x;
float pos_y;
float speed;
int patchLength;
int direction; //(x,y) 0:(1,0) 1(0,1) 2:(-1,0) 3:(0,-1)
float radius;
ofColor color;
//constructor
Particle(); //do not draw void
//method 行為
void update();
void draw();
};
#endif /* particle_hpp */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment