[work 45] Euclidean Algorithm
![[work 45] Euclidean Algorithm](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/06/outFrameImg0900.png?fit=720%2C720&ssl=1)
Movie
Source code
about
- ユークリッドの互除法を視覚化する
- 正方形を長方形に分割し、長方形を正方形に分割する。これらをしきい値に達するまで再帰的に実行する。
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(720, 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 "DivRect.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:
shared_ptr<DivRect> rect;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(0);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
rect = make_shared<DivRect>();
rect->setup();
}
//--------------------------------------------------------------
void ofApp::update(){
rect->update();
}
//--------------------------------------------------------------
void ofApp::draw(){
rect->display();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 'r') {
rect->reset();
}
}
#ifndef DivRect_hpp
#define DivRect_hpp
#include <stdio.h>
#include "ofMain.h"
struct myRect {
ofRectangle rect;
ofColor color;
};
class DivRect {
public:
DivRect();
~DivRect();
void setup();
void update();
void display();
void reset();
private:
void DivideRectangle(float xPos, float yPos, float width);
void DivideSquare(float xPos, float yPos, float width);
int num1;
int num2;
float ratio;
float threshold;
std::vector<myRect> rects;
int index;
ofFbo fbo;
};
#endif /* DivRect_hpp */
#include "DivRect.hpp"
DivRect::DivRect()
{
num1 = 10;
num2 = 6;
ratio = (float)num2 / (float)num1;
threshold = 160;
index = 0;
}
DivRect::~DivRect()
{
}
void DivRect::setup()
{
DivideSquare(0, 0, ofGetWidth());
fbo.allocate(ofGetWidth(), ofGetHeight());
fbo.begin();
ofClear(0);
fbo.end();
}
void DivRect::reset()
{
rects.clear();
fbo.begin();
ofClear(0);
fbo.end();
num1 = (int)ofRandom(1, 20);
num2 = (int)ofRandom(1, 20);
while (num1 == num2) {
num2 = (int)ofRandom(1, 20);
}
ratio = (float)num2 / (float)num1;
threshold = (int)ofRandom(10, 300);
index = 0;
setup();
}
void DivRect::update()
{
if (index < rects.size()) {
fbo.begin();
ofSetColor(rects.at(index).color);
ofFill();
ofDrawRectangle(rects.at(index).rect);
ofNoFill();
ofSetColor(255);
ofDrawRectangle(rects.at(index).rect);
fbo.end();
}
index++;
}
void DivRect::display()
{
fbo.draw(0, 0);
}
void DivRect::DivideRectangle(float xPos, float yPos, float width)
{
int idx = 0;
float wd = width;
float xEndPos = xPos + wd;
float yEndPos = yPos + wd / ratio;
myRect r;
r.rect = ofRectangle(xPos, yPos, wd, wd / ratio);
r.color = ofColor(ofRandom(255),ofRandom(255),ofRandom(255));
rects.push_back(r);
while (wd > threshold) {
idx++;
if (idx % 2 == 0) {
while (xPos + wd < xEndPos + 0.1) {
DivideSquare(xPos, yPos, wd);
xPos += wd;
}
wd = xEndPos - xPos;
} else {
while (yPos + wd < yEndPos + 0.1) {
DivideSquare(xPos, yPos, wd);
yPos += wd;
}
wd = yEndPos - yPos;
}
}
}
void DivRect::DivideSquare(float xPos, float yPos, float width){
int idx = 0;
float wd = width;
float xEndPos = xPos + wd;
float yEndPos = yPos + wd;
myRect r;
r.rect = ofRectangle(xPos, yPos, wd, wd);
r.color = ofColor(ofRandom(255),ofRandom(255),ofRandom(255));
rects.push_back(r);
while (wd > threshold) {
idx++;
if (idx % 2 == 1) {
while (xPos + wd * ratio < xEndPos + 0.1) {
DivideRectangle(xPos, yPos, wd * ratio);
xPos += wd * ratio;
}
wd = xEndPos - xPos;
} else {
while (yPos + wd / ratio < yEndPos + 0.1) {
DivideRectangle(xPos, yPos, wd);
yPos += wd / ratio;
}
wd = yEndPos - yPos;
}
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofRectangle |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode