Find the intersection of two lines: Difference between revisions

→‎{{header|C++}}: minor change
(→‎{{header|C++}}: minor change)
Line 465:
<lang cpp>#include <iostream>
#include <cmath>
#include <assert.hcassert>
using namespace std;
 
Line 477:
}
 
/// Calculate intersection of two lines.
///\return true if found, false if not found or error
bool LineLineIntersect(double x1, double y1, // Line 1 start
double x2, double y2, // Line 1 end
double x3, double y3, // Line 2 start
double x4, double y4, // Line 2 end
double &ixOut, double &iyOut) // Output
{
double detL1 = Det(x1, y1, x2, y2);
Line 492:
double y3my4 = y3 - y4;
 
double xnom = Det(detL1, x1mx2, detL2, x3mx4);
double ynom = Det(detL1, y1my2, detL2, y3my4);
double denom = Det(x1mx2, y1my2, x3mx4, y3my4);
if(denom == 0.0) // Lines don't seem to cross
{
ixOut = NAN;
Line 502 ⟶ 500:
}
 
double xnom = Det(detL1, x1mx2, detL2, x3mx4);
double ynom = Det(detL1, y1my2, detL2, y3my4);
ixOut = xnom / denom;
iyOut = ynom / denom;
if(!isfinite(ixOut) || !isfinite(iyOut)) // Probably a numerical issue
return false;
 
Line 514:
// **Simple crossing diagonal lines**
 
// Line 1
double x1=4.0, y1=0.0;
double x2=6.0, y2=10.0;
// Line 2
double x3=0.0, y3=3.0;
double x4=10.0, y4=7.0;
Line 530:
assert(fabs(ix - 5.0) < eps);
assert(fabs(iy - 5.0) < eps);
return 0;
 
}</lang>
 
{{out}}
<pre>result 1,5,5</pre>
 
 
=={{header|Clojure}}==
Anonymous user