Program to check if three points are collinear
Given three points, check whether they lie on a straight (collinear) or not
Examples :
Input : (1, 1), (1, 4), (1, 5)
Output : Yes
The points lie on a straight line
Input : (1, 5), (2, 5), (4, 6)
Output : No
The points do not lie on a straight line
First approach
Three points lie on the straight line if the area formed by the triangle of these three points is zero. So we will check if the area formed by the triangle is zero or not
Formula for area of triangle is :
0.5 * [x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)]
The formula is basically half of determinant
value of following.
x1 x2 x3
y1 y2 y3
1 1 1
The above formula is derived from shoelace formula.
If this equals zero then points lie on a straight line
C++
#include <bits/stdc++.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
void collinear( int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int x1 = 1, x2 = 1, x3 = 1,
y1 = 1, y2 = 4, y3 = 5;
collinear(x1, y1, x2, y2, x3, y3);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void collinear( int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)
printf ( "Yes" );
else
printf ( "No" );
}
int main()
{
int x1 = 1, x2 = 1, x3 = 1,
y1 = 1, y2 = 4, y3 = 5;
collinear(x1, y1, x2, y2, x3, y3);
return 0;
}
|
Java
class GFG
{
static void collinear( int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String args[])
{
int x1 = 1 , x2 = 1 , x3 = 1 ,
y1 = 1 , y2 = 4 , y3 = 5 ;
collinear(x1, y1, x2, y2, x3, y3);
}
}
|
Python
def collinear(x1, y1, x2, y2, x3, y3):
a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)
if (a = = 0 ):
print "Yes"
else :
print "No"
x1, x2, x3, y1, y2, y3 = 1 , 1 , 1 , 1 , 4 , 5
collinear(x1, y1, x2, y2, x3, y3)
|
C#
using System;
class GFG
{
static void collinear( int x1, int y1, int x2,
int y2, int x3, int y3)
{
int a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main ()
{
int x1 = 1, x2 = 1, x3 = 1,
y1 = 1, y2 = 4, y3 = 5;
collinear(x1, y1, x2, y2, x3, y3);
}
}
|
PHP
<?php
function collinear( $x1 , $y1 , $x2 ,
$y2 , $x3 , $y3 )
{
$a = $x1 * ( $y2 - $y3 ) +
$x2 * ( $y3 - $y1 ) +
$x3 * ( $y1 - $y2 );
if ( $a == 0)
printf( "Yes" );
else
printf( "No" );
}
$x1 = 1; $x2 = 1; $x3 = 1;
$y1 = 1; $y2 = 4; $y3 = 5;
collinear( $x1 , $y1 , $x2 , $y2 , $x3 , $y3 );
?>
|
Javascript
<script>
function collinear(x1, y1, x2, y2, x3, y3)
{
var a = x1 * (y2 - y3) +
x2 * (y3 - y1) +
x3 * (y1 - y2);
if (a == 0)
document.write( "Yes" );
else
document.write( "No" );
}
var x1 = 1, x2 = 1, x3 = 1,y1 = 1, y2 = 4, y3 = 5;
collinear(x1, y1, x2, y2, x3, y3);
</script>
|
Output :
Yes
Time Complexity: O(1)
Auxiliary Space: O(1)
Second approach
For three points, slope of any pair of points
must be same as other pair.
For example, slope of line joining (x2, y2)
and (x3, y3), and line joining (x1, y1) and
(x2, y2) must be same.
(y3 - y2)/(x3 - x2) = (y2 - y1)/(x2 - x1)
In other words,
(y3 - y2)(x2 - x1) = (y2 - y1)(x3 - x2)
C++
#include <bits/stdc++.h>
using namespace std;
void collinear( int x1, int y1, int x2, int y2, int x3,
int y3)
{
if ((y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int x1 = 1, x2 = 1, x3 = 0, y1 = 1, y2 = 6, y3 = 9;
collinear(x1, y1, x2, y2, x3, y3);
return 0;
}
|
C
#include <stdio.h>
#include <math.h>
void collinear( int x1, int y1, int x2,
int y2, int x3, int y3)
{
if ((y3 - y2) * (x2 - x1) ==
(y2 - y1) * (x3 - x2))
printf ( "Yes" );
else
printf ( "No" );
}
int main()
{
int x1 = 1, x2 = 1, x3 = 0,
y1 = 1, y2 = 6, y3 = 9;
collinear(x1, y1, x2, y2, x3, y3);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void cool_line( int x1, int y1, int x2,
int y2, int x3, int y3)
{
if ((y3 - y2) * (x2 - x1) ==
(y2 - y1) * (x3 - x2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main (String[] args) {
int a1 = 1 , a2 = 1 , a3 = 0 ,
b1 = 1 , b2 = 6 , b3 = 9 ;
cool_line(a1, b1, a2, b2, a3, b3);
}
}
|
Python
def collinear(x1, y1, x2, y2, x3, y3):
if ((y3 - y2) * (x2 - x1) = = (y2 - y1) * (x3 - x2)):
print ( "Yes" )
else :
print ( "No" )
x1, x2, x3, y1, y2, y3 = 1 , 1 , 0 , 1 , 6 , 9
collinear(x1, y1, x2, y2, x3, y3);
|
C#
using System;
class GFG
{
static void cool_line( int x1, int y1, int x2,
int y2, int x3, int y3)
{
if ((y3 - y2) * (x2 - x1) ==
(y2 - y1) * (x3 - x2))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static public void Main ()
{
int a1 = 1, a2 = 1, a3 = 0,
b1 = 1, b2 = 6, b3 = 9;
cool_line(a1, b1, a2, b2, a3, b3);
}
}
|
PHP
<?php
function collinear( $x1 , $y1 , $x2 ,
$y2 , $x3 , $y3 )
{
if (( $y3 - $y2 ) * ( $x2 - $x1 ) ==
( $y2 - $y1 ) * ( $x3 - $x2 ))
echo ( "Yes" );
else
echo ( "No" );
}
$x1 = 1;
$x2 = 1;
$x3 = 0;
$y1 = 1;
$y2 = 6;
$y3 = 9;
collinear( $x1 , $y1 , $x2 ,
$y2 , $x3 , $y3 );
?>
|
Javascript
<script>
function cool_line(x1 , y1 , x2 , y2 , x3 , y3)
{
if ((y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2))
document.write( "Yes" );
else
document.write( "No" );
}
var a1 = 1, a2 = 1, a3 = 0, b1 = 1, b2 = 6, b3 = 9;
cool_line(a1, b1, a2, b2, a3, b3);
</script>
|
Output :
No
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
03 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...