// ------------------------------------------------------------
// ------------ into testApp.cpp
// ------------------------------------------------------------
void testApp::draw(){
ofxVec2f pta = ofxVec2f(0,0);
ofxVec2f ptb = ofxVec2f(mouseX, mouseY);
ofxVec2f ptc = ofxVec2f(100,0);
ofxVec2f ptd = ofxVec2f(0,100);
cout << doLineSegmentIntersection(pta, ptb, ptc, ptd) << endl;
ofLine(pta.x, pta.y, ptb.x, ptb.y);
ofLine(ptc.x, ptc.y, ptd.x, ptd.y);
}
// ------------------------------------------------------------
// ------------ into lineintersection.h
// ------------------------------------------------------------
#ifndef _LINTERSECTION
#define _LINTERSECTION
#include "ofMain.h"
#define OF_ADDON_USING_OFXVECTORMATH
#include "ofAddons.h"
int doLineSegmentIntersection(const ofxVec2f& p0, const ofxVec2f& p1, const ofxVec2f& p2, const ofxVec2f& p3);
#endif
// ------------------------------------------------------------
// ------------ into lineintersection.cpp
// ------------------------------------------------------------
#include "lineintersection.h"
class LineSegment
{
public:
ofxVec2f begin_;
ofxVec2f end_;
LineSegment(const ofxVec2f& begin, const ofxVec2f& end)
: begin_(begin), end_(end) {}
enum IntersectResult { PARALLEL, COINCIDENT, NOT_INTERESECTING, INTERESECTING };
IntersectResult Intersect(const LineSegment& other_line, ofxVec2f& intersection)
{
float denom = ((other_line.end_.y - other_line.begin_.y)*(end_.x - begin_.x)) -
((other_line.end_.x - other_line.begin_.x)*(end_.y - begin_.y));
float nume_a = ((other_line.end_.x - other_line.begin_.x)*(begin_.y - other_line.begin_.y)) -
((other_line.end_.y - other_line.begin_.y)*(begin_.x - other_line.begin_.x));
float nume_b = ((end_.x - begin_.x)*(begin_.y - other_line.begin_.y)) -
((end_.y - begin_.y)*(begin_.x - other_line.begin_.x));
if(denom == 0.0f)
{
if(nume_a == 0.0f && nume_b == 0.0f)
{
return COINCIDENT;
}
return PARALLEL;
}
float ua = nume_a / denom;
float ub = nume_b / denom;
if(ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f)
{
// Get the intersection point.
intersection.x = begin_.x + ua*(end_.x - begin_.x);
intersection.y = begin_.y + ua*(end_.y - begin_.y);
return INTERESECTING;
}
return NOT_INTERESECTING;
}
};
int doLineSegmentIntersection(const ofxVec2f& p0, const ofxVec2f& p1, const ofxVec2f& p2, const ofxVec2f& p3)
{
LineSegment linesegment0(p0, p1);
LineSegment linesegment1(p2, p3);
ofxVec2f intersection;
std::cout << "Line Segment 0: (" << p0.x << ", " << p0.y << ") to (" << p1.x << ", " << p1.y << ")\n"
<< "Line Segment 1: (" << p2.x << ", " << p2.y << ") to (" << p3.x << ", " << p3.y << ")\n";
int returnVal = 0;
switch(linesegment0.Intersect(linesegment1, intersection))
{
case LineSegment::PARALLEL:
returnVal = 0;
std::cout << "The lines are parallel\n\n";
break;
case LineSegment::COINCIDENT:
returnVal = 1;
std::cout << "The lines are coincident\n\n";
break;
case LineSegment::NOT_INTERESECTING:
returnVal = 2;
std::cout << "The lines do not intersect\n\n";
break;
case LineSegment::INTERESECTING:
returnVal = 3;
std::cout << "The lines intersect at (" << intersection.x << ", " << intersection.x << ")\n\n";
break;
}
return returnVal;
}
[/code]