[ Video ]
[ About ]
Hexagonを増殖
Many hexagon.
[ Source ]
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #pragma once #include "ofMain.h" #include "Hexagon.h" class ofApp : public ofBaseApp { public: 	void setup(); 	void update(); 	void draw(); 	void keyPressed(int key) {}; 	void keyReleased(int key) {}; 	void mouseMoved(int x, int y) {}; 	void mouseDragged(int x, int y, int button) {}; 	void mousePressed(int x, int y, int button) {}; 	void mouseReleased(int x, int y, int button) {}; 	void mouseEntered(int x, int y) {}; 	void mouseExited(int x, int y) {}; 	void windowResized(int w, int h) {}; 	void dragEvent(ofDragInfo dragInfo) {}; 	void gotMessage(ofMessage msg) {}; 	vector<Hexagon> hexagons; }; | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofSetWindowTitle("openframeworks"); 	ofBackground(39); 	ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); 	for (int i = 0; i < 128; i++) { 		ofColor color; 		color.setHsb(i * 2, 200, 255); 		Hexagon hexagon = Hexagon(ofPoint(ofGetWidth() * 0.5, ofGetHeight() * 0.5), 10, color); 		this->hexagons.push_back(hexagon); 	} } //-------------------------------------------------------------- void ofApp::update() { 	for (Hexagon& hexagon : this->hexagons) { 		hexagon.Update(); 	} } //-------------------------------------------------------------- void ofApp::draw() { 	for (Hexagon& hexagon : this->hexagons) { 		hexagon.Draw(); 	} } //-------------------------------------------------------------- int main() { 	ofSetupOpenGL(720, 720, OF_WINDOW); 	ofRunApp(new ofApp()); } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #pragma once #include "ofMain.h" class Hexagon { public: 	Hexagon(); 	Hexagon(ofPoint location, float radius, ofColor color); 	void Update(); 	void Draw(); private: 	ofPoint location; 	std::deque<ofPoint> logs; 	float radius; 	float height; 	ofColor color; }; | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include "Hexagon.h" Hexagon::Hexagon() : Hexagon(ofPoint(), 0, ofColor()) {} Hexagon::Hexagon(ofPoint location, float radius, ofColor color) { 	this->location = location; 	this->radius = radius; 	this->height = radius * sqrt(3); 	this->color = color; } void Hexagon::Update() { 	if (ofGetFrameNum() % 5 == 0) { 		int r = ofRandom(6); 		int deg = r * 60 + 30; 		int try_count = 0; 		ofPoint tmp_location; 		while (true) { 			tmp_location = this->location + ofPoint(this->height * cos(deg * DEG_TO_RAD), this->height * sin(deg * DEG_TO_RAD)); 			if (tmp_location.x > 0 && tmp_location.x < ofGetWidth() && tmp_location.y > 0 && tmp_location.y < ofGetHeight()) { break; } 			deg += 60; 			if (++try_count > 6) {  				tmp_location = this->location;  				break; 			} 		} 		this->location = tmp_location; 		this->logs.push_front(this->location); 	} 	while (this->logs.size() > 6) { this->logs.pop_back(); } } void Hexagon::Draw() { 	float tmp_radius = this->radius; 	ofSetColor(this->color); 	for (ofPoint& log : this->logs) { 		ofPushMatrix(); 		ofTranslate(log); 		ofBeginShape(); 		for (int deg = 0; deg < 360; deg += 60) { 			ofVertex(tmp_radius * cos(deg * DEG_TO_RAD), tmp_radius * sin(deg * DEG_TO_RAD)); 		} 		ofEndShape(true); 		ofPopMatrix(); 	} } |