C++ Program to Check Whether a Given Points are Colinear or Not

This is a C++ Program to check whether points are colinear or not.

Here is source code of the C++ Program to Check Whether a Given Points are Colinear or Not. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

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

Output:

$ g++ ColinearPoints.cpp
$ a.out
 
The points are: (9, 5), (4, 6), & (1, 2)
The Equation of the line is : (-4)x+(3)y+(-2) = 0
The points are NOT colinear
------------------
(program exited with code: 0)
Press return to continue

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.