{Software} Structures




main.cpp

#include "ofMain.h"
#include "ofMain.h"
#include "ofApp.h"

int main( ){
    ofSetupOpenGL(1920, 1080, OF_FULLSCREEN);
    //ofSetupOpenGL(1920, 1080, OF_WINDOW);
    ofRunApp(new ofApp());
}


ofApp.cpp

#include "ofApp.h"
#include "circle.h"

Circle *circles[numCircle];

void ofApp::setup() {
    ofSetBackgroundAuto(false);
    ofBackground(126);
    ofHideCursor();
    for (int i = 0; i < numCircle; i++) {
        circles[i] = new Circle(ofRandom(0, ofGetWidth()), ofRandom(0, ofGetHeight()),
                                ofRandom(20, 126),
                                ofRandom(-0.25, 0.25), ofRandom(-0.25, 0.25), i, circles,
                                ofGetWidth(), ofGetHeight());
    }
}

void ofApp::update() {

}

void ofApp::draw() {
    for (int i = 0; i < numCircle; i++) {
        circles[i]->update();
        circles[i]->move();
    }
}


ofApp.h

#pragma once

#include "ofMain.h"

static int width;
static int height;

class ofApp : public ofBaseApp{
    public:
        void setup();
        void update();
        void draw();
};


Circle.cpp

#include "Circle.h"

Circle::Circle(float px, float py, float pr, float psp, float pysp, int pid, Circle *circles[numCircle], int pw, int ph) {
    x = px;
    y = py;
    r = pr;
    r2 = r * r;
    xSpeed = psp;
    ySpeed = pysp;
    id = pid;
    others = circles;
    width = pw;
    height = ph;
}

void Circle::update() {
    for (int i = id+1; i < numCircle; i++) {
        intersect( others[id], others[i] );
    }
}

void Circle::move() {
    x += xSpeed;
    y += ySpeed;

    if(xSpeed > 0) {
        if (x > width+r) {
            x = -r;
        }
    } else {
        if (x < -r) {
            x = width+r;
        }
    }
    
    if(ySpeed < 0) {
        if (y < -r) {
            y = height+r;
        }
    } else {
        if (y > height+r) {
            y = -r;
        }
    }
}

void Circle::intersect(Circle *cA, Circle *cB) {
    float dx = cA->x - cB->x;
    float dy = cA->y - cB->y;
    float d2 = dx*dx + dy*dy;
    float d = sqrt( d2 );
    
    if ( d > (cA->r + cB->r) || d < (fabs(cA->r - cB->r)) ) {
        return;  // No solution
    }

    float a = (cA->r2 - cB->r2 + d2) / (2*d);
    float h = sqrt( cA->r2 - a*a );
    float x2 = cA->x + a*(cB->x - cA->x)/d;
    float y2 = cA->y + a*(cB->y - cA->y)/d;
    
    float paX = x2 + h*(cB->y - cA->y)/d;
    float paY = y2 - h*(cB->x - cA->x)/d;
    float pbX = x2 - h*(cB->y - cA->y)/d;
    float pbY = y2 + h*(cB->x - cA->x)/d;
    
    float dist = distance(paX, paY, pbX, pbY);
    float gray = 255 - dist;
    ofSetColor(gray, gray, gray, 20);
    ofDrawLine(paX, paY, pbX, pbY);
} 

float Circle::distance(float x1, float y1, float x2, float y2) { 
    return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); 
}
 

Circle.h

#include "ofApp.h"

#ifndef _Circle_H_
#define _Circle_H_

#define numCircle 200

class Circle
{
public:
    
    float x, y, r, r2, xSpeed, ySpeed;
    int id;
    
    Circle **others;
    
    Circle();
    Circle(float, float, float, float, float, int, Circle *[numCircle], int, int);
    void update();
    void move();
    void makePoint();
    void intersect(Circle *, Circle *);
    float distance(float, float, float, float);
    
}; 

#endif