Created
April 28, 2016 00:10
-
-
Save ofZach/4c74628eedac9282c4ddf2b6f3726fc6 to your computer and use it in GitHub Desktop.
line packing example
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
// 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