[work 23] Rotate Grid
![[work 23] Rotate Grid](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/03/outFrameImg0900-4.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 10×10のグリッドを作成
- ランダムに選択したグリッドを回転させる
- ランダムに回転軸(0度、45度、90度)を選択
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"
//================================
int main( ){
// 4K:4096x2160
// 2K:2048x1080
// FullHD:1920x1080
// HD:1440x1080
// HD720p:1280x720
// DVD:720x480
// setup the GL context
ofSetupOpenGL(1280,720, OF_WINDOW);
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
#pragma once
#include "ofMain.h"
#include "Grid.hpp"
class ofApp : public ofBaseApp{
public:
ofApp();
~ofApp();
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);
private:
std::unique_ptr<Grid> grid;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(217,229,240);
ofSetBackgroundAuto(true);
grid = make_unique<Grid>();
}
//--------------------------------------------------------------
void ofApp::update(){
grid->update();
}
//--------------------------------------------------------------
void ofApp::draw(){
ofPushMatrix();
ofTranslate(340,110);
grid->display();
ofPopMatrix();
}
#ifndef Grid_hpp
#define Grid_hpp
#define USE_MYCOLOR (0)
#include <stdio.h>
#include "ofMain.h"
#if USE_MYCOLOR
#include "myColorLib.hpp"
#endif
struct RotateGrid {
ofVec2f idx;
ofVec3f axis;
float angle;
ofColor color;
};
class Grid {
public:
Grid();
~Grid();
void update();
void display();
private:
void delRotateGrid();
int rowSize;
int colSize;
int gridSize;
int angle;
std::list<RotateGrid> rGrid;
#if USE_MYCOLOR
std::unique_ptr<myColorLib> myCol;
#endif
};
#endif /* Grid_hpp */
#include "Grid.hpp"
Grid::Grid()
{
rowSize = 10;
colSize = 10;
gridSize = 50;
angle = 0;
#if USE_MYCOLOR
myCol = make_unique<myColorLib>(MYCOL_SELECT);
#endif
}
Grid::~Grid()
{
}
void Grid::update()
{
bool push = true;
RotateGrid r;
r.idx = ofVec2f((int)ofRandom(0, colSize), (int)ofRandom(0, rowSize));
int x = ofRandom(1) < 0.5 ? 0 : 1;
int y = ofRandom(1) < 0.5 ? 0 : 1;
int z = 0;
r.axis = ofVec3f(x, y, z);
r.angle = 0;
#if USE_MYCOLOR
r.color = myCol->getColor(ofRandom(1));
#else
ofColor c;
c.setHsb(ofRandom(255), ofRandom(255), ofRandom(255));
r.color = c;
#endif
if (!rGrid.empty()) {
auto itr = rGrid.begin();
while(itr != rGrid.end()) {
if (((*itr).idx.x == r.idx.x) && ((*itr).idx.y) == r.idx.y) {
push = false;
break;
}
++itr;
}
}
if ((ofGetFrameNum() % 5 == 0) && push && (rGrid.size() < 30)) {
rGrid.push_back(r);
}
for (RotateGrid &r : rGrid) {
r.angle += 1;
}
delRotateGrid();
}
void Grid::display()
{
for (int y = 0; y < rowSize; y++) {
for (int x = 0; x < colSize; x++) {
ofPushMatrix();
ofTranslate(gridSize/2 + (x * gridSize), gridSize/2 + (y * gridSize), 0);
ofNoFill();
ofSetColor(91, 96, 104);
for (RotateGrid r : rGrid) {
if (((int)r.idx.x == x) && ((int)r.idx.y == y)) {
ofRotateDeg(r.angle, r.axis.x, r.axis.y, r.axis.z);
ofFill();
ofSetColor(r.color);
}
}
ofSetRectMode(OF_RECTMODE_CENTER);
ofDrawRectangle(0, 0, 0, gridSize, gridSize);
ofPopMatrix();
}
}
}
void Grid::delRotateGrid()
{
auto itr = rGrid.begin();
while(itr != rGrid.end()) {
if ((*itr).angle > 180) {
itr = rGrid.erase(itr);
} else {
++itr;
}
if (rGrid.empty()) {
break;
}
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofAppRunner ofGetFrameNum |
| openframeworks | ofGraphics ofRotateDeg |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode