Oh, thought you were looking or intercepts, not intersects. Try this?
/* The Ocular Man */
//Math bits jacked from: http://www.ambrsoft.com/MathCalc/Line/TwoLinesIntersection/TwoLinesIntersection.htm
#include <iostream>
#include <math.h>
#include <stdlib.h>
double findM(double x1, double y1, double x2, double y2);
double findB(double y, double x, double m);
double findXintercept(double m, double b);
double findYintercept(double m, double b);
double findXintersection(double a, double b, double m1, double m2);
double findYintersection(double a, double b, double m1, double m2);
double findTheta(double m1, double m2);
void printLineEqu(double m, double b);
int main()
{
std::cout << "Goodbye world!" << std::endl;
const double PI = atan(1) * 4; //calculates pi, 3.14159
std::cout << "PI: " << PI << std::endl;
//data set 1
int x1 = 3;
int y1 = -12;
int x2 = 16;
int y2 = -10;
//data set 2
int x3 = 8;
int y3 = -15;
int x4 = 12;
int y4 = -1;
/*
y = M1x + a
y = M2x + b
*/
double m1 = findM(x1, y1, x2, y2);
double m2 = findM(x3, y3, x4, y4);
double a = findB(y1, x1, m1);
double b = findB(y3, x3, m2);
/* view work so far */
std::cout << "EQUATION 1: ";
printLineEqu(m1, a);
std::cout << "EQUATION 2: ";
printLineEqu(m2, b);
/* done */
//parallel line check
if((m2 - m1) == 0) {
std::cout << "both lines are parallel." << std::endl;
return 0; //exit if out of range
}
//calculate theta
double theta = findTheta(m1, m2);
if((theta < 0) || (theta > (PI/2))) {
std::cout << "theta: " << theta << std::endl;
std::cout << "angle out of range." << std::endl;
return 0; //exit if out of range
}
//find intersections, if all is well
double Xintersection = findXintersection(a, b, m1, m2);
std::cout << "X intersection: " << Xintersection << std::endl;
double Yintersection = findYintersection(a, b, m1, m2);
std::cout << "Y intersection: " << Yintersection << std::endl;
/*
//not used
double m = findM(x1, y1, x2, y2);
std::cout << "m: " << m << std::endl;
double b = findB(y1, x1, m);
std::cout << "b: " << b << std::endl;
//x-intercept, cross x axis (n,0)
//y-intercept, cross y axis (0,n)
double Xi = findXintercept(m, b);
std::cout << "X intercept: " << Xi << std::endl;
double Yi = findYintercept(m, b);
std::cout << "Y intercept: " << Yi << std::endl;
*/
return 0;
}
//used for plotting on ti-89 as check, plug and chug
void printLineEqu(double m, double b) {
if(b < 0) {
std::cout << "Y = " << m << "*x" << b << std::endl;
}
else {
std::cout << "Y = " << m << "*x+" << b << std::endl;
}
return;
}
//find theta
double findTheta(double m1, double m2) {
double _theta = (m1 - m2) / (1 + (m1 * m2));
_theta = abs(_theta); //absolute value
_theta = atan(_theta); //inverse tan
return _theta;
}
//find the y intersection
double findYintersection(double a, double b, double m1, double m2) {
return ((a * m2) - (b * m1)) / (m2 - m1);
}
//Fine the x intersection
double findXintersection(double a, double b, double m1, double m2) {
return (a - b) / (m2 - m1);
}
//find slope
double findM(double x1, double y1, double x2, double y2) {
return (y2 - y1) / (x2 - x1);
}
//find b
double findB(double y, double x, double m) {
return y / (m * x);
}
//find X intercept
double findXintercept(double m, double b) {
return (0 - b) / m;
}
//find Y intercept
double findYintercept(double m, double b) {
return (m * 0) + b;
}
Output