C++ - Find if 3 points or coordinates are collinear
C++ - Find if 3 points or coordinates are collinear
CODE
//This program takes 3 coordinates as inputs,
//...calculates the area of the triangle formed by the 3 points
//...and determine if these 3 points are collinear
//The 3 points are collinear, if the area is 0
#include <iostream>
using namespace std;
int main()
{
//Declare the required variables to store the 3 coordinates
int x1, y1, x2, y2, x3, y3;
//Get the x, y coordinates of the 1st one.
//Store it in the variables x1, y1
cout << "Enter x, y separated by spaces of the coordinate 1: ";
cin >> x1 >> y1;
//Get the x, y coordinates of the 2nd one.
//Store it in the variables x2, y2
cout << "Enter x, y separated by spaces of the coordinate 2: ";
cin >> x2 >> y2;
//Get the x, y coordinates of the 3rd one.
//Store it in the variables x3, y3
cout << "Enter x, y separated by spaces of the coordinate 3: ";
cin >> x3 >> y3;
//The area of the triangle can be found using the formula below
//Calculate the area and store it in the variable "area"
float area = 0.5 * ( x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) );
//Check if the calculated area is 0 and display the message acccordingly
if(area == 0)
cout << "The 3 points are collinear.";
else
cout << "The 3 points are not collinear.";
return 0;
//Input: 2 4, 4 6, 6 8
//Output: The 3 points are collinear.
}
Comments
Post a Comment