Open In App

Using Above Below Primitive to Test Whether Two Lines Intersect in Java

Last Updated : 05 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Above Below Primitive is the method to check whether the lines are intersecting or not by comparing their endpoints. Here is a JAVA program which uses above below primitive to test whether two lines intersect with each other or not. This approach only returns true if one endpoint of the line is on the left of the other line’s left endpoint and its right endpoint is on the right of other line’s endpoint.

Example

Input:
      End Points of first line
      x1 = 2, y1 = 3
      x2 = 6, y2 = 4
      End Points of second line
      p1 = 3, q1 = 4
      p2 = 7, q2 = 1
Output:
      Equation 1: (1)x+(-4)y+(10) = 0
      Equation 2: (-3)x+(-4)y+(25) = 0
      Intersecting

Approach

Find the segment first line as a1.
         if a1 < 0
             Find the segment of line 2 as a2
         if a2 >= 0
             print intersecting
         otherwise
             print not intersecting.
         else if a1 >0
             Find the segment of line 2 as a2
         if a2 <= 0
             print intersecting.
         otherwise
             print not intersecting.

Below is the implementation of the above approach.

Java




// This is a java program to find whether two lines
// intersect or not using above and below primitive
public class Main {
    public static void check(int x1, int x2, int y1, int y2,
                             int p1, int p2, int q1, int q2)
    { // Segment of line 1 is stored as a1
 
        int a1 = (y2 - y1) * p1 + (x1 - x2) * q1
                 + (x2 * y1 - x1 * y2);
 
        if (a1 < 0) {
            // Segment of line 2 is stored as a2
            int a2 = (y2 - y1) * p2 + (x1 - x2) * q2
                     + (x2 * y1 - x1 * y2);
 
            if (a2 >= 0)
                System.out.println("Intersecting");
 
            else if (a2 < 0)
                System.out.println("Not intersecting");
        }
 
        else if (a1 > 0) {
 
            int a2 = (y2 - y1) * p2 + (x1 - x2) * q2
                     + (x2 * y1 - x1 * y2);
 
            if (a2 <= 0)
                System.out.println("Intersecting");
 
            else if (a2 > 0)
                System.out.println("Not intersecting");
        }
 
        // lines are coincinding
        else
            System.out.println(
                "points are lying on the line");
    }
    // Driver Code
    public static void main(String args[])
    {
        // Taking the coordinates of first line as input
        int x1 = 2, y1 = 3;
        int x2 = 6, y2 = 4;
 
        // Equation of line using slope point form
        System.out.println("Equation 1: (" + (y2 - y1)
                           + ")x+(" + (x1 - x2) + ")y+("
                           + (x2 * y1 - x1 * y2) + ") = 0");
 
        // Taking the coordinates of second line as input
        int p1 = 3, q1 = 4;
        int p2 = 7, q2 = 1;
 
        // Equation of line using slope point form
        System.out.println("Equation 2: (" + (q2 - q1)
                           + ")x+(" + (p1 - p2) + ")y+("
                           + (p2 * q1 - p1 * q2) + ") = 0");
 
        check(x1, x2, y1, y2, p1, p2, q1, q2);
    }
}


Output

Equation 1: (1)x+(-4)y+(10) = 0
Equation 2: (-3)x+(-4)y+(25) = 0
Intersecting

Time Complexity: O(1)



Similar Reads

Comparison of double and float primitive types in Java
Consider the following two codes in Java: Java Code // This program prints true class Geeksforgeeks { public static void main(String args[]) { float f = 5.25f; double d = 5.25 System.out.println(f == d); } } Output true Java Code // But this program prints false. class Geeksforgeeks { public static void main(String args[]) { float f = 5.1f; double
2 min read
Program to convert Primitive Array to Stream in Java
An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition of the array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are sto
3 min read
How to get slice of a Primitive Array in Java
Given a Primitive Array, the task is to get a Slice of this array in Java, using start and ending index. Examples: Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 2, endIndex = 4 Output: {3, 4, 5} Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 0, endIndex = 1 Output: {1, 2}Method 1: Naive Method.Get the Array and the startIndex and the endIndex.Create a
8 min read
Primitive data type vs. Object data type in Java with Examples
Data Types in Java Every variable in java has a data type. Data types specify the size and type of values that can be stored in an identifier. Java language is rich in its data types. The variety of data types available allow the programmer to select the type appropriate to the need of the application. In java, data types are classified into two ca
6 min read
Java Program For Arithmetic Operations Between BigDecimal and Primitive Data Types
The floating-point data types (float and double) are not as accurate to be used in the financial calculations. Therefore, Java offers a separate class "BigDecimal" to perform the operations and avoid the minimal chances of mistakes in calculations. BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comp
6 min read
Default Values Assigned to Primitive Data Types in Java
In Java, when a variable is declared but not initialized, it is assigned a default value based on its data type. The default values for the primitive data types in Java are as follows: byte: 0short: 0int: 0long: 0Lfloat: 0.0fdouble: 0.0dchar: '\u0000' (null character)boolean: false It is important to note that these default values are only assigned
4 min read
Java I/O Operation - Wrapper Class vs Primitive Class Variables
It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b
3 min read
Widening Primitive Conversion in Java
Whenever we do use double quotes around a letter or string as we all know it is treated as a string but when we do use a single quote round letter alongside performing some computations then they are treated as integers values while printing for which we must have knowledge of ASCII table concept as in computer for every character being case sensit
3 min read
Primitive Wrapper Classes are Immutable in Java
In Java, an immutable class is a class (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) which once created then its body can not be changed and the same applies to immutable objects which once created cannot be changed. Now the question arises that we do need the help of wrapper classes while dealing with primitive data types in
3 min read
Is an array a primitive type or an object in Java?
An array in Java is an object. Now the question how is this possible? What is the reason behind that? In Java, we can create arrays by using new operator and we know that every object is created using new operator. Hence we can say that array is also an object. Now the question also arises, every time we create an object for a class then what is th
2 min read
Article Tags :
Practice Tags :