Tuesday, 28 November 2017

Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not


Code:

import java.util.Scanner;

public class Collinear_Points
{
    public static void main(String args[])
    {
        System.out.println("Enter the points : ,");
        Scanner scan = new Scanner(System.in);
        int x, y, x1, x2, y1, y2;
        x = scan.nextInt();
        y = scan.nextInt();
        x1 = scan.nextInt();
        x2 = scan.nextInt();
        y1 = scan.nextInt();
        y2 = scan.nextInt();

        /*
         * System.out.println("The Equation of the line is : (" + (y2 - y1) +
         * ")x+(" + (x1 - x2) + ")y+(" + (x2 * y1 - x1 * y2) + ") = 0");
         */

        int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2);
        if (s < 0)
            System.out.println("The points are NOT collinear");
        else if (s > 0)
            System.out.println("The points are NOT collinear");
        else
            System.out.println("The points are collinear");
        scan.close();
    }
}


Output:

Enter the points : ,
3 2
1 3
1 5
The points are NOT collinear

Enter the points : ,
1 1
1 5
1 9
The points are collinear


More Java Programs:

















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...