- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 apply Above-Below-on Test to find the position of a point with respect to a Line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting m and n is found by calculating the scalar s −

Y = A xt + B yt + C

If Y< 0, t lies in the clockwise halfplane of L; if Y>0, t lies on the counter-clockwise halfplane; if Y= 0, t lies on L.

## Algorithm

Begin Take the points as input. For generating equation of the line, generate random numbers for coefficient of x and y (x1,x2,y1,y2) by using rand function at every time of compilation. Compute s as (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2). if (s < 0) Print "The point lies below the line or left side of the line". else if (s >0) print "The point lies above the line or right side of the line"; else print "The point lies on the line" End

## Example Code

#include<stdlib.h> #include<iostream> #include<math.h> #include<time.h> using namespace std; const int L = 0; const int H= 20; int main(int argc, char **argv) { time_t seconds; time(&seconds); srand((unsigned int) seconds); int x1, x2, y1, y2; x1 = rand() % (H - L + 1) + L; x2 = rand() % (H - L + 1) + L; y1 = rand() % (H - L + 1) + L; y2 = rand() % (H - L + 1) + L; cout << "The Equation of the 1st line is : (" << (y2 - y1) << ")x+(" << (x1 - x2) << ")y+(" << (x2 * y1 - x1 * y2) << ") = 0

"; int x, y; cout << "

Enter the point:"; cin >>x; cin >>y; int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2); if (s < 0) cout << "The point lies below the line or left side of the line"; else if (s >0) cout << "The point lies above the line or right side of the line"; else cout << "The point lies on the line"; return 0; }

## Output

The Equation of the 1st line is : (7)x+(0)y+(-105) = 0 Enter the point:7 6 The point lies below the line or left side of the line

- Related Questions & Answers
- C++ Program to use above below primitive to test whether two lines intersect
- Program to find the mid-point of a line in C++
- How to create a point chart with point size increment based on the position of the point in R?
- Program to find the number of possible position in a line in Python
- Find the number of points that have atleast 1 point above, below, left or right of it in C++
- How to count the number of elements in an array below/above a given number (JavaScript)
- Program to remove sublist to get same number of elements below and above k in C++
- Program to find the highest altitude of a point in Python
- C++ Program to Show the Duality Transformation of Line and Point
- Python program to find the Decreasing point in a List
- Find foot of perpendicular from a point in 2D plane to a Line in C++
- Program to find slope of a line in C++
- Program to find position of first event number of line l of a triangle of numbers in Python
- C++ Program to find out the moves to read a point from another point in a 2D plane
- Program to find out is a point is reachable from the current position through given points in Python

Advertisements