[work 95] Swap front and back tiles
![[work 95] Swap front and back tiles](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/09/outFrameImg0030.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 正方形を並べたタイルを前後に配置する
- 初期設定の正方形の枠をはみ出さないように正方形を回転させる
- 正方形が45度回転すると前後のタイルが重なり合わずに表示される
- 重なり合わずに表示されたタイミングで前後を入れ替える
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 "Polygon.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::array<std::vector<std::shared_ptr<MyPolygon>>, 2> polygons;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(0);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
std::array<ofRectangle, 2> rect;
std::array<ofColor, 2> color = {ofColor(255), ofColor(128)};
float width = 60;
for (int k = 0; k < rect.size(); k++) {
rect.at(k).setFromCenter(ofGetWidth() / 2, ofGetHeight() / 2, 600 + (width * k), 600 + (width * k));
int col = rect.at(k).width / width;
int raw = rect.at(k).height / width;
for (int i = 0; i < col; i++) {
for (int j = 0; j < raw; j++) {
float x = rect.at(k).getLeft() + (width / 2) + (width * i);
float y = rect.at(k).getTop() + (width / 2) + (width * j);
polygons.at(k).push_back(make_shared<MyPolygon>(ofVec2f(x, y), width, color.at(k)));
polygons.at(k).back()->setup();
}
}
}
}
//--------------------------------------------------------------
void ofApp::update(){
bool swap = false;
for (std::vector<std::shared_ptr<MyPolygon>> poly : polygons) {
for (std::shared_ptr<MyPolygon> p : poly) {
if ((p->getAngle() == 30) || (p->getAngle() == 150)) {
swap = true;
}
p->update();
}
}
if (swap) {
std::vector<std::shared_ptr<MyPolygon>> tmp;
tmp = polygons.at(0);
polygons.at(0) = polygons.at(1);
polygons.at(1) = tmp;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
for (std::vector<std::shared_ptr<MyPolygon>> poly : polygons) {
for (std::shared_ptr<MyPolygon> p : poly) {
p->display();
}
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 's') {
ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
img.save("screenshot.png");
}
}
#ifndef Polygon_hpp
#define Polygon_hpp
#include <stdio.h>
#include "ofMain.h"
class MyPolygon {
public:
MyPolygon(ofVec2f c, float w, ofColor col);
~MyPolygon();
void setup();
void update();
void display();
int getAngle();
private:
ofVec2f center;
float width;
std::vector<ofVec2f> corner;
std::vector<ofVec2f> orgCorner;
std::vector<ofVec2f> sideDir;
ofColor color;
int angle;
};
#endif /* Polygon_hpp */
#include "Polygon.hpp"
MyPolygon::MyPolygon(ofVec2f c, float w, ofColor col)
{
center = c;
width = w;
color = col;
}
MyPolygon::~MyPolygon()
{
}
void MyPolygon::setup()
{
orgCorner.push_back(ofVec2f(width / 2, width / 2));
orgCorner.push_back(ofVec2f(width / 2, -width / 2));
orgCorner.push_back(ofVec2f(-width / 2, -width / 2));
orgCorner.push_back(ofVec2f(-width / 2, width / 2));
corner = orgCorner;
for (int i = 0; i < corner.size(); i++) {
int index = (i + 1) % corner.size();
sideDir.push_back(orgCorner.at(index) - orgCorner.at(i));
}
angle = 0;
}
void MyPolygon::update()
{
std::vector<ofVec2f> org = corner;
for (int i = 0; i < corner.size(); i++) {
corner.at(i) = orgCorner.at(i) + sideDir.at(i) * std::sin(ofDegToRad(angle));
}
angle = (angle + 1) % 180;
}
void MyPolygon::display()
{
ofPushMatrix();
ofSetColor(color);
ofTranslate(center);
ofFill();
ofBeginShape();
ofVertices(corner);
ofEndShape();
ofPopMatrix();
}
int MyPolygon::getAngle()
{
return angle;
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofRectangle |
| c++ | std::array |
| c++ | std::vector |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode