
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check if a Given Set of Three Points Lie on a Single Line or Not
This is a C++ program to check if a given set of three points lie on a single line or not. Three points lie on a single line if the area of the triangle formed by this points is equal to zero.
The area of the triangle is −
0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)).
Algorithms
Begin Generate the points randomly. Calculate the area by using above formula. If area > 0 Then the points don't lie on the straight line. else if area < 0 Then the points don't lie on the straight line. else The points lie on the straight line. End
Example
#include <iostream> #include <time.h> #include <stdlib.h> using namespace std; static int L = 1; static int U= 20; int main(int argc, char **argv) { int x3, y3, x1, x2, y1, y2; time_t seconds; time(&seconds); srand((unsigned int) seconds); //Generate the points randomly using rand(). x1 = rand() % ( U- L+ 1) + L; y1 = rand() % (U - L+ 1) + L; x2 = rand() % (U - L + 1) + L; y2 = rand() % (U - L+ 1) + L; x3 = rand() % (U - L+ 1) + L; y3 = rand() % (U - L+ 1) + L; cout << "The points are: (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << "), & (" << x3 << ", " << y3 << ")\n"; //calculate area float a = 0.5 *(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); if (a < 0) cout << "The points don't lie on the straight line"; else if (a > 0) cout << "The points don't lie on the straight line "; else cout << "The points lie on the straight line"; }
Output
The points are: (20, 9), (6, 13), & (13, 11) The points lie on the straight line The points are: (9, 15), (4, 15), & (11, 16) The points don't lie on the straight line
- Related Articles
- Program to count number of points that lie on a line in Python
- Golang program to check if k’th bit is set for a given number or not.
- Program to check whether list of points form a straight line or not in Python
- Program to check given graph is a set of trees or not in Python
- C program to check if a given string is Keyword or not?
- Python program to check if a given string is Keyword or not
- Swift Program to check if a given string is Keyword or not
- JAVA Program to Check if a Point is On the Left or Right Side of a Line
- C++ Program to check if given numbers are coprime or not
- Check if a given graph is tree or not
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- C++ Program to Check if a Given Graph must Contain Hamiltonian Cycle or Not\n
- Program to check if three points are collinear in C++
- Golang Program to check a given number is finite or not
- Haskell Program to check a given number is finite or not

Advertisements