Challenge: Jim Beam

Scor cont: 40.0 / 40

Submission status: Accepted

Submission score: 1.0

Submission ID: 464729262

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/jim-beam/problem

Cerinta

Jim likes to play with laser beams.  
Jim stays at point $(0,0)$.  
There is a mirror at point $(X_m, Y_m)$ and a wall between points $(X_1, Y_1)$ and $(X_2, Y_2)$.

Jim wants to find out if he can point the laser beam on the mirror.

**Input Format**  
First line contains the number of test cases, $T$.  
Each subsequent line contains one test case: space separated integers that denote endpoints of the wall $X_1, Y_1$, $X_2, Y_2$ and position of the mirror $X_m, Y_m$.  

**Output Format**  
The answer for each test case: Display `YES` if Jim can point the laser beam to the mirror, otherwise display `NO` .  

**Constraints**  
$1 \le T \le 100$  
$0 \le |X_1|,|Y_1|,|X_2|,|Y_2|,|X_m|,|Y_m| \le 10^6$  
Mirror doesn't have common points with wall.  
Wall doesn't pass through the point $(0,0)$.  

**Sample Input**

	5
	1 2 2 1 2 2
	-1 1 1 1 1 -1
	1 1 2 2 3 3
	2 2 3 3 1 1
    0 1 1 1 0 2

**Sample Output**
	
    NO
	YES
	NO
	YES
    NO

Cod sursa

//jim_beam.cpp
//Jim Beam
//Ad Infinitum - Math Programming Contest August'14
//Author: derekhh

#include<iostream>
#include<algorithm>
using namespace std;

#define EPS 1e-8
#define geq(x,y) ((x)+EPS>=(y))

struct Point
{
	double x, y;
	Point(double x0 = 0, double y0 = 0) :x(x0), y(y0){}
};

struct Line
{
	Point p1, p2;
};

double times(Point p0, Point p1, Point p2)
{
	return (p1.x - p0.x)*(p2.y - p0.y) - (p1.y - p0.y)*(p2.x - p0.x);
}

bool LineSegIntersect(Line L1, Line L2)
{
	return(geq(max(L1.p1.x, L1.p2.x), min(L2.p1.x, L2.p2.x))
		&& geq(max(L2.p1.x, L2.p2.x), min(L1.p1.x, L1.p2.x))
		&& geq(max(L1.p1.y, L1.p2.y), min(L2.p1.y, L2.p2.y))
		&& geq(max(L2.p1.y, L2.p2.y), min(L1.p1.y, L1.p2.y))
		&& times(L1.p1, L2.p1, L1.p2)*times(L1.p1, L2.p2, L1.p2) <= EPS
		&&times(L2.p1, L1.p1, L2.p2)*times(L2.p1, L1.p2, L2.p2) <= EPS);
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		Line L1, L2;
		L1.p1 = Point(0, 0);
		cin >> L2.p1.x >> L2.p1.y >> L2.p2.x >> L2.p2.y >> L1.p2.x >> L1.p2.y;
		if (LineSegIntersect(L1, L2))
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}
	return 0;
}
HackerRank Geometry – Jim Beam