public
Last active

Langton's ant for openFrameworks.

  • Download Gist
Langton's ant - main.cpp
C++
1 2 3 4 5 6 7 8
#include "ofMain.h"
#include "testApp.h"
 
//========================================================================
int main( ){
ofSetupOpenGL(1280,720,OF_WINDOW);
ofRunApp(new testApp());
}
Langton's ant - testApp.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#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){
}
Langton's ant - testApp.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#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;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.