Check if the tower of sight issue occurs or not | C ++ program to check if tower of sight issue occurs or not ; Function to check if point p lies in between the line joining p1 and p2 ; If parallel to X - axis ; Point p lies between p1 and p2 ; If parallel to Y - axis ; Point p lies between p1 and p2 ; If point p satisfies the equation of line joining p1 and p2 ; Function to check if tower of sight issue occurred ; B lies between AC ; D lies between AC ; A lies between BD ; C lies between BD ; Driver code ; Point A ; Point B ; Point C ; Point D
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkIntersection ( pair < int , int > p1 , pair < int , int > p2 , pair < int , int > p ) { int val ; if ( p1 . second == p2 . second && p1 . second == p . second ) { if ( p . first <= max ( p1 . first , p2 . first ) && ( p . first >= min ( p1 . first , p2 . first ) ) ) return 1 ; } if ( p1 . first == p2 . first && p1 . first == p . first ) { if ( p . second <= max ( p1 . second , p2 . second ) && ( p . second >= min ( p1 . second , p2 . second ) ) ) return 1 ; } else { val = ( p . second - p1 . second ) * ( p2 . first - p1 . first ) - ( p . first - p1 . first ) * ( p2 . second - p1 . second ) ; if ( val == 0 ) if ( ( p . first <= max ( p1 . first , p2 . first ) && ( p . first >= min ( p1 . first , p2 . first ) ) ) && ( p . second <= max ( p1 . second , p2 . second ) && ( p . second >= min ( p1 . second , p2 . second ) ) ) ) return 1 ; } return 0 ; } void towerOfSight ( pair < int , int > a , pair < int , int > b , pair < int , int > c , pair < int , int > d ) { int flag = 0 ; if ( checkIntersection ( a , c , b ) ) flag = 1 ; else if ( checkIntersection ( a , c , d ) ) flag = 1 ; else if ( checkIntersection ( b , d , a ) ) flag = 1 ; else if ( checkIntersection ( b , d , c ) ) flag = 1 ; flag ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; } int main ( ) { pair < int , int > a = { 0 , 0 } ; pair < int , int > b = { 0 , -2 } ; pair < int , int > c = { 2 , 0 } ; pair < int , int > d = { 0 , 2 } ; towerOfSight ( a , b , c , d ) ; return 0 ; }