[work 16] Grid
![[work 16] Grid](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/03/outFrameImg0115.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 四角形の中を動く点
- その点を通る縦と横の辺と平行な線で四角形を4分割する
- 4分割された四角形を同様の方法で再度4分割する
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"
//=================================
int main( ){
// 4K:4096x2160
// 2K:2048x1080
// FullHD:1920x1080
// HD:1440x1080
// HD720p:1280x720
// DVD:720x480
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"
#define WAVE_MAX (4)
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::array<Grid *,5> g;
};
#include "ofApp.h"
ofApp::ofApp(){
for (int i = 0; i < g.max_size(); i++) {
g[i] = new Grid();
}
}
ofApp::~ofApp(){
for (int i = 0; i < g.max_size(); i++) {
delete g[i];
}
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(255);
ofSetBackgroundAuto(true);
}
//--------------------------------------------------------------
void ofApp::update(){
std::array<ofVec2f,4> v;
ofVec2f p0 = ofVec2f(20, 20);
ofVec2f p1 = ofVec2f(ofGetWidth() - 20, 20);
ofVec2f p2 = ofVec2f(ofGetWidth() - 20, ofGetHeight() - 20);
ofVec2f p3 = ofVec2f(20, ofGetHeight() - 20);
v[0] = p0;
v[1] = p1;
v[2] = p2;
v[3] = p3;
g[0]->setVertex(v);
g[0]->update();
ofVec2f p = g[0]->getPoint();
// Upper Left
v[0] = p0;
v[1].x = p.x;
v[1].y = p0.y;
v[2] = p;
v[3].x = p0.x;
v[3].y = p.y;
g[1]->setVertex(v);
g[1]->update();
// Upper Right
v[0].x = p.x;
v[0].y = p0.y;
v[1] = p1;
v[2].x = p1.x;
v[2].y = p.y;
v[3] = p;
g[2]->setVertex(v);
g[2]->update();
// Lower Right
v[0] = p;
v[1].x = p1.x;
v[1].y = p.y;
v[2] = p2;
v[3].x = p.x;
v[3].y = p3.y;
g[3]->setVertex(v);
g[3]->update();
// Lower Left
v[0].x = p0.x;
v[0].y = p.y;
v[1] = p;
v[2].x = p.x;
v[2].y = p3.y;
v[3] = p3;
g[4]->setVertex(v);
g[4]->update();
}
//--------------------------------------------------------------
void ofApp::draw(){
for (int i = 0; i < g.max_size(); i++) {
g[i]->display();
}
}
#ifndef Grid_hpp
#define Grid_hpp
#include <stdio.h>
#include "ofMain.h"
#include "myColorLib.hpp"
class Grid {
public:
Grid();
~Grid();
void setVertex(std::array<ofVec2f,4> v);
void update();
void display();
ofVec2f getPoint();
private:
std::array<ofVec2f,4> vertex;
ofVec2f point;
ofVec2f dir;
float angleVelocity;
float time;
myColorLib *color;
std::array<ofColor,4> c;
};
#endif /* Grid_hpp */
#include "Grid.hpp"
Grid::Grid()
{
dir = ofVec2f(1, 0);
time = ofRandom(10000);
color = new myColorLib(MYCOL_ALL);
for (int i = 0; i < c.max_size(); i++) {
c[i] = color->getColor(ofRandom(1));
}
}
Grid::~Grid()
{
delete color;
}
void Grid::setVertex(std::array<ofVec2f,4> v)
{
vertex = v;
}
void Grid::update()
{
ofVec2f c;
float w = vertex[1].x - vertex[0].x;
float h = vertex[3].y - vertex[0].y;
c.x = vertex[0].x + w * 0.5;
c.y = vertex[0].y + h * 0.5;
angleVelocity = ofMap(ofSignedNoise(time), -1, 1, -10, 10);
time += 0.01;
dir.rotate(angleVelocity);
point.x = c.x + dir.x * w * 0.3;
point.y = c.y + dir.y * h * 0.3;
}
void Grid::display()
{
ofSetColor(c[0]);
ofFill();
ofDrawRectangle(vertex[0].x, vertex[0].y, point.x - vertex[0].x, point.y - vertex[0].y);
ofSetColor(c[1]);
ofDrawRectangle(point.x, vertex[0].y, vertex[1].x - point.x, point.y - vertex[0].y);
ofSetColor(c[2]);
ofDrawRectangle(point.x, point.y, vertex[1].x - point.x, vertex[3].y - point.y);
ofSetColor(c[3]);
ofDrawRectangle(vertex[0].x, point.y, point.x - vertex[0].x, vertex[3].y - point.y);
ofSetColor(0);
ofNoFill();
ofSetLineWidth(5);
ofDrawRectangle(vertex[0].x, vertex[0].y, vertex[1].x - vertex[0].x, vertex[3].y - vertex[0].y);
ofSetColor(0);
ofNoFill();
ofSetLineWidth(5);
ofDrawLine(point.x, vertex[0].y, point.x, vertex[3].y);
ofDrawLine(vertex[0].x, point.y, vertex[1].x, point.y);
ofSetColor(0);
ofFill();
ofDrawCircle(point.x, point.y, 10);
}
ofVec2f Grid::getPoint()
{
return point;
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofVec2f |
| c++ | std::array |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode