Java Program to Check if Two Points are on the Same Side

bookmark

public class Main
{
   public static void main(String[] args)
   {
       int x1 = 0;
       int y1 = 5;
       int x2 = 1;
       int y2 = 2;
       int px = -2;
       int py = 11;
       int qx = -1;
       int qy = 8;
       double m = (y2-y1)/(x2-x1); // formula to find slope
       double c = y2 - (m*x2); // formula to find the constant of the line equation
       if ((py >= m * px + c && qy >= m * qx + c) || (py <= m * px + c && qy <= m *qx + c))
           System.out.println("points P, Q lies on same side");
       else
           System.out.println("points P, Q lies on opposite side");  
   }
}

 


Output:

points P, Q lies on same side