Last active
December 23, 2015 07:49
-
-
Save tado/6603347 to your computer and use it in GitHub Desktop.
Langton's ant for openFrameworks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
#include "testApp.h" | |
//======================================================================== | |
int main( ){ | |
ofSetupOpenGL(1280,720,OF_WINDOW); | |
ofRunApp(new testApp()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "testApp.h" | |
//-------------------------------------------------------------- | |
void testApp::setup(){ | |
ofBackground(0, 0, 0); | |
ofSetFrameRate(60); | |
myImage.allocate(WIDTH, HEIGHT, OF_IMAGE_GRAYSCALE); | |
pixels = myImage.getPixels(); | |
for (int i = 0; i < WIDTH * HEIGHT; i++) { | |
pixels[i] = 0; | |
} | |
myImage.update(); | |
dirs[0].set(-1, 0); // left | |
dirs[1].set(0, 1); // top | |
dirs[2].set(1, 0); // right | |
dirs[3].set(0, -1); // bottom | |
for (int i = 0; i < ANTS_NUM; i++) { | |
ants[i].row = int(ofRandom(WIDTH)); | |
ants[i].col =int(ofRandom(HEIGHT)); | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::update(){ | |
for (int i = 0; i < ANTS_NUM; i++) { | |
for (int j = 0; j < ANTS_SPEED; j++) { | |
if(field[ants[i].row][ants[i].col] == 1){ | |
ants[i].dir--; | |
} else { | |
ants[i].dir++; | |
} | |
if(field[ants[i].row][ants[i].col] == 1){ | |
field[ants[i].row][ants[i].col] = 0; | |
pixels[ants[i].col * WIDTH + ants[i].row] = 0; | |
} else { | |
field[ants[i].row][ants[i].col] = 1; | |
pixels[ants[i].col * WIDTH + ants[i].row] = 255; | |
} | |
ants[i].dir = (ants[i].dir + 4) % 4; | |
ants[i].row += dirs[ants[i].dir].row; | |
ants[i].col += dirs[ants[i].dir].col; | |
ants[i].row = (ants[i].row + WIDTH) % WIDTH; | |
ants[i].col = (ants[i].col + HEIGHT) % HEIGHT; | |
} | |
} | |
myImage.update(); | |
} | |
//-------------------------------------------------------------- | |
void testApp::draw(){ | |
ofSetColor(0, 0, 255); | |
ofSetRectMode(OF_RECTMODE_CORNER); | |
myImage.draw(0, 0); | |
ofSetColor(255, 0, 0); | |
ofSetRectMode(OF_RECTMODE_CENTER); | |
for (int i = 0; i < ANTS_NUM; i++) { | |
ofRect(ants[i].row, ants[i].col, 4, 4); | |
} | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseMoved(int x, int y ){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void testApp::dragEvent(ofDragInfo dragInfo){ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
class Dir { | |
public: | |
int row; | |
int col; | |
void set(int _row, int _col){ | |
row = _row; | |
col = _col; | |
} | |
}; | |
class Ant { | |
public: | |
int row; | |
int col; | |
int dir; | |
Ant(){ | |
dir = int(ofRandom(4)); | |
} | |
}; | |
class testApp : public ofBaseApp{ | |
public: | |
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 windowResized(int w, int h); | |
void dragEvent(ofDragInfo dragInfo); | |
void gotMessage(ofMessage msg); | |
static const int WIDTH = 1280; | |
static const int HEIGHT = 720; | |
int field[WIDTH][HEIGHT] = { [0 ... WIDTH-1] = { [ 0 ... HEIGHT-1] = 0 } };; | |
static const int ANTS_SPEED = 20; | |
static const int ANTS_NUM = 50; | |
Ant ants[ANTS_NUM]; | |
Dir dirs[4]; | |
ofImage myImage; | |
unsigned char * pixels; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment