/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
struct point
{
int x, y;
//default constructor
point(): x(0), y(0)
{
}
//overload operator>>
friend std::istream &operator>>(std::istream &is, point& inputPoint);
};
int orientation(point p1, point p2, point p3)
{
int val = (p2.y - p1.y) * (p3.x - p2.x) -
(p2.x -p1.x) * (p3.y -p2.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
bool validprojection(int a, int b, int c, int d)
{
if (a > b)
swap(a,b);
if (c > d)
swap(c, d);
return max(a, c) <= min(b, d);
}
bool doIntersect(point a, point b, point c, point d)
{
int o1 = orientation(a, b, c);
int o2 = orientation(a, b, d);
int o3 = orientation(c, d, a);
int o4 = orientation(c, d, b);
if(o1 != o2 && o3 != o4)
return true;
if (o1 == 0 && o4 == 0)
{
if(validprojection(a.x, b.x, c.x, d.x) && validprojection(a.y, b.y, c.y, d.y))
return true;
}
return false;
}
std::istream &operator>>(std::istream &is, point& inputPoint)
{
std::cout<<"Enter x value: ";
std::cin >> inputPoint.x ;
std::cout<<"Enter y value: ";
std::cin >> inputPoint.y;
//check if input succeeded
if(cin)
{
//do something if needed
;
}
else
{
inputPoint = point();
}
return is;
}
int main()
{
cout<<"To find the intersection point of two line segment"<<std::endl;
//point p1 = {1, 1}, p2 = {10, 1}, p3 = {1, 2}, p4 = {10, 2};
//TAKE INPUT(x and y values) FROM USER
point p1;
std::cin >> p1;
point p2;
std::cin >> p2;
point p3;
std::cin >> p3;
point p4;
std::cin >> p4;
//check if intersect
doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";
//p1 = {10, 0}, p2 = {0, 10}, p3 = {0, 0}, p4 = {10, 10};
//AGAIN TAKE INPUT FROM USER
std::cin >> p1;
std::cin >> p2;
std::cin >> p3;
std::cin >> p4;
doIntersect(p1, p2, p3, p4)? cout << "yes\n": cout<< "No\n";
return 0;
}