# C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line

«
»
This is a C++ Program to check whether point lies above, below or on the line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting p and q is found by calculating the scalar s:
s = A xt + B yt + C
If s < 0, t lies in the clockwise halfplane of L; if s > 0, t lies on the counter-clockwise halfplane; if s = 0, t lies on L.
For example, the equation of the line connecting points (2, 2) and (4, 5) is -3x + 2y + 2 = 0. The point (6, 3) lies in the clockwise halfplane of this line, because (-3)(6) + (2)(3) + 2 = -10. Conversely, the point (0, 5) lies in the other halfplane as (-3)(0) +(2)(5) +2 = 12.

Here is source code of the C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include<time.h>`
2. `#include<stdlib.h>`
3. `#include<iostream>`
4. `#include<math.h>`
5. ` `
6. `using namespace std;`
7. `const int LOW = 0;`
8. `const int HIGH = 10;`
9. `int main(int argc, char **argv)`
10. `{`
11. `    time_t seconds;`
12. `    time(&seconds);`
13. `    srand((unsigned int) seconds);`
14. ` `
15. `    int x1, x2, y1, y2;`
16. `    x1 = rand() % (HIGH - LOW + 1) + LOW;`
17. `    x2 = rand() % (HIGH - LOW + 1) + LOW;`
18. `    y1 = rand() % (HIGH - LOW + 1) + LOW;`
19. `    y2 = rand() % (HIGH - LOW + 1) + LOW;`
20. ` `
21. `    cout << "The Equation of the 1st line is : (" << (y2 - y1) << ")x+(" << (x1`
22. `            - x2) << ")y+(" << (x2 * y1 - x1 * y2) << ") = 0\n";`
23. ` `
24. `    int x, y;`
25. `    cout << "\nEnter the point:";`
26. `    cin >> x;`
27. `    cin >> y;`
28. ` `
29. `    int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);`
30. `    if (s < 0)`
31. `        cout << "The point lies below the line or left side of the line";`
32. `    else if (s > 0)`
33. `        cout << "The point lies above the line or right side of the line";`
34. `    else`
35. `        cout << "The point lies on the line";`
36. `    return 0;`
37. `}`

Output:

```\$ g++ PointWRTLine.cpp
\$ a.out

The Equation of the 1st line is : (3)x+(0)y+(-3) = 0

Enter the point:1 4
The point lies on the line

The Equation of the 1st line is : (5)x+(-1)y+(-25) = 0

Enter the point:1 1
The point lies below the line or left side of the line

The Equation of the 1st line is : (-6)x+(8)y+(-24) = 0

Enter the point:19 21
The point lies above the line or right side of the line
------------------
(program exited with code: 0)

Sanfoundry Global Education & Learning Series – 1000 C++ Programs. 