[work 118] Cellular Automata
![[work 118] Cellular Automata](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2020/04/outFrameImg0886.png?fit=1280%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(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 "CellularAutomata.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::shared_ptr<CA> cell;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(ofColor::white);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
cell = make_shared<CA>(250, 8);
cell->setup();
}
//--------------------------------------------------------------
void ofApp::update(){
cell->update();
}
//--------------------------------------------------------------
void ofApp::draw(){
cell->display();
}
#ifndef CellularAutomata_hpp
#define CellularAutomata_hpp
#include <stdio.h>
#include "ofMain.h"
class CA {
public:
CA(int n, int m) : num(n), mod(m) {};
~CA() {};
void setup();
void update();
void display();
private:
int num;
int mod;
std::vector<std::vector<int>> state;
int transition(int i, int j);
};
#endif /* CellularAutomata_hpp */
#include "CellularAutomata.hpp"
void CA::setup()
{
state.assign(num, std::vector<int>(num, 0));
int c = num / 2;
state.at(c).at(c) = 1;
}
void CA::update()
{
if (ofGetFrameNum() != 0 && ofGetFrameNum() % 15 == 0) {
std::vector<std::vector<int>> nextState(num, std::vector<int>(num, 0));
for (int i = 0; i < state.size(); i++) {
for (int j = 0; j < state.at(i).size(); j++) {
nextState.at(i).at(j) = transition(i, j);
}
}
state = nextState;
}
}
void CA::display()
{
float w = (float)ofGetWidth() / (float)num;
float h = (float)ofGetHeight() / (float)num;
float y = 0;
float x;
for (int i = 0; i < state.size(); i++) {
x = 0;
for (int j = 0; j < state.at(i).size(); j++) {
ofFill();
ofColor c;
float hue = ofMap(state.at(i).at(j), 0, mod, 0, 255);
float sat = ofMap(state.at(i).at(j), 0, mod, 0, 255);
c.setHsb(hue, sat, 255);
ofSetColor(c);
ofDrawRectangle(x, y, w, h);
x += w;
}
y += h;
}
}
int CA::transition(int i, int j)
{
int next;
next = state.at((i - 1 + num) % num).at(j)
+ state.at(i).at((j - 1 + num) % num)
+ state.at(i).at(j)
+ state.at(i).at((j + 1) % num)
+ state.at((i + 1) % num).at(j);
next = next % mod;
return next;
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofMap |
| c++ | std::vector |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode