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

Output:

$ gcc PointWRTLine.c
$ ./a.out
 
The Equation of the line is: (-1)x+(2)y+(-8) = 0
 
Enter the point:2 3
The point lies below the line or left side of the line

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.