Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "ofApp.h"
#define EPS 1.0E-10
//--------------------------------------------------------------
void ofApp::setup(){
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
ofPushMatrix();
ofTranslate(mouseX,mouseY);
ofPoint lineStart(0,500,-150);
ofPoint lineEnd(-900,500,-850);
ofSetColor(255, 0, 0);
ofDrawLine(lineStart,lineEnd);
ofPoint lineStart2(-1000,500, -750);
ofPoint lineEnd2(1000,500, -750);
ofSetColor(0, 0, 255);
ofDrawLine(lineStart2,lineEnd2);
ofPoint intersectPoint;
bool bIntersect = ofLineSegmentIntersection(lineStart,lineEnd,lineStart2,lineEnd2,intersectPoint);
ofPoint pa,pb;
bool bIntersect2 = LineLineIntersect(lineStart,lineEnd, lineStart2,lineEnd2,pa,pb); //,mua,mub);
ofLog()<<"bIntersect "<<bIntersect<<" intersectPoint "<<intersectPoint;
ofLog()<<"bIntersect2 "<<bIntersect2<<" pa "<<pa<<" pb "<<pb;
ofPopMatrix();
}
bool ofApp::LineLineIntersect(ofVec3f p1,ofVec3f p2,ofVec3f p3,ofVec3f p4, ofVec3f &pa,ofPoint &pb){ //},double *mua, double *mub){
//http://paulbourke.net/geometry/pointlineplane/
//http://paulbourke.net/geometry/pointlineplane/lineline.c
// ofVec3f pa;
// ofVec3f pb;
double mua;
double mub;
ofPoint p13,p43,p21;
double d1343,d4321,d1321,d4343,d2121;
double numer,denom;
p13.x = p1.x - p3.x;
p13.y = p1.y - p3.y;
p13.z = p1.z - p3.z;
p43.x = p4.x - p3.x;
p43.y = p4.y - p3.y;
p43.z = p4.z - p3.z;
if (ABS(p43.x) < EPS && ABS(p43.y) < EPS && ABS(p43.z) < EPS)
return false;
p21.x = p2.x - p1.x;
p21.y = p2.y - p1.y;
p21.z = p2.z - p1.z;
if (ABS(p21.x) < EPS && ABS(p21.y) < EPS && ABS(p21.z) < EPS)
return false;
d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
denom = d2121 * d4343 - d4321 * d4321;
if (ABS(denom) < EPS)
return false;
numer = d1343 * d4321 - d1321 * d4343;
mua = numer / denom;
mub = (d1343 + d4321 * (mua)) / d4343;
pa.x = p1.x + mua * p21.x;
pa.y = p1.y + mua * p21.y;
pa.z = p1.z + mua * p21.z;
pb.x = p3.x + mub * p43.x;
pb.y = p3.y + mub * p43.y;
pb.z = p3.z + mub * p43.z;
// ofLog()<<"mua "<<mua;
// ofLog()<<"mub "<<mub;
// ofLog()<<" pa pb dist "<<pa.distance(pb);
return true;
}
//--------------------------------------------------------------
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){
}
#pragma once
#include "ofMain.h"
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);
bool LineLineIntersect(ofVec3f p1,ofVec3f p2,ofVec3f p3,ofVec3f p4, ofVec3f &pa,ofPoint &pb);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment