Skip to content

Instantly share code, notes, and snippets.

@ofZach
Created April 28, 2016 00:10
Show Gist options
  • Save ofZach/4c74628eedac9282c4ddf2b6f3726fc6 to your computer and use it in GitHub Desktop.
Save ofZach/4c74628eedac9282c4ddf2b6f3726fc6 to your computer and use it in GitHub Desktop.
line packing example
// ofApp.h :
#pragma once
#include "ofMain.h"
class line {
public:
ofPoint a;
ofPoint b;
};
class ofApp : 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 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);
vector < line > lines;
};
ofApp.cpp:
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
}
//--------------------------------------------------------------
void ofApp::update(){
for (int i = 0; i < 100; i++){
ofPoint ptA(ofRandom(0,ofGetWidth()), ofRandom(0,ofGetHeight()));
float randomAngle = ofRandom(0,PI);
ofPoint ptB = ptA + 20* ofPoint(cos(randomAngle), sin(randomAngle));
bool bDoIntersectWithAnyone = false;
for (auto line : lines){
ofPoint intersection;
if (ofLineSegmentIntersection(ptA, ptB, line.a, line.b, intersection)){
bDoIntersectWithAnyone = true;
break;
}
}
if (!bDoIntersectWithAnyone){
line tempLine;
tempLine.a = ptA;
tempLine.b = ptB;
lines.push_back(tempLine);
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(0);
for (auto line : lines){
ofDrawLine(line.a, line.b);
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment