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.
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
const int LOW = 1;
const int HIGH = 10;
int main(int argc, char **argv)
{
int x, y, x1, x2, y1, y2;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
x = rand() % (HIGH - LOW + 1) + LOW;
y = rand() % (HIGH - LOW + 1) + LOW;
x1 = rand() % (HIGH - LOW + 1) + LOW;
x2 = rand() % (HIGH - LOW + 1) + LOW;
y1 = rand() % (HIGH - LOW + 1) + LOW;
y2 = rand() % (HIGH - LOW + 1) + LOW;
cout << "The points are: (" << x << ", " << y << "), (" << x1 << ", " << y1
<< "), & (" << x2 << ", " << y2 << ")\n";
cout << "The Equation of the line is : (" << (y2 - y1) << ")x+(" << (x1
- x2) << ")y+(" << (x2 * y1 - x1 * y2) << ") = 0\n";
int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
if (s < 0)
cout << "The points are NOT colinear";
else if (s > 0)
cout << "The points are NOT colinear";
else
cout << "The points are colinear";
}
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.
Related Posts:
- Check C++ Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Check Programming Books
- Practice Programming MCQs