Wednesday, April 20, 2022

Using C++,Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point, p2(x2, y2) is on the left of the on the right, or on the same line. > 0 ; p2 is on the left side of the line ( x1 - x0 ) * ( y2 - y0 ) - ( x2- x0 ) * ( y1 - y0 ) = 0 ; p2 is on the same line (a) p2 is on the left of the line (b) p2 is on the right of the line (c) p2 is on the same line Write a program that prompts the user to enter the x-y coordinates for the three points p0, p1, and p2 and displays whether p2 is on the left of the line from p0 to p1, on the right, or on the same line.

 Using C++,Given a directed line from point p0(x0, y0) to p1(x1, y1), you can use the following condition to decide whether a point, p2(x2, y2) is on the left of the on the right, or on the same line.


> 0 ; p2 is on the left side of the line

( x1 - x0 ) * ( y2 - y0 ) - ( x2- x0 ) * ( y1 - y0 ) = 0 ; p2 is on the same line

(a) p2 is on the left of the line



(b) p2 is on the right of the line

(c) p2 is on the same line


Write a program that prompts the user to enter the x-y coordinates for the three points p0, p1, and p2 and displays whether p2 is on the left of the line from p0 to p1, on the right, or on the same line.







import java.util.Scanner;

public class Lab01 {

  

   public static void specifyposition(int x0,int y0,int x1,int y1,int x2,int y2){

     

double val = (x1 - x0)*(y2 - y0) - (x2 - x0)*(y1 - y0);

  

if(val>0)

   System.out.println("p2 is on the left side of the line");

else if (val==0)

   System.out.println("p2 is on the same line");

else

   System.out.println("p2 is on the right side of the line");

   }

   public static void main(String[] args) {

  

   Scanner sc = new Scanner(System.in);

   System.out.println("Enter coordinate for three points: ");

   System.out.println("Enter coordinate for P0: ");

   int x1 = sc.nextInt();

   int y1 = sc.nextInt();

   System.out.println("Enter coordinate for P1: ");

   int x2 = sc.nextInt();

   int y2 = sc.nextInt();

   System.out.println("Enter coordinate for P2: ");

   int x3 = sc.nextInt();

   int y3 = sc.nextInt();

     

   specifyposition(x1,y1,x2,y2,x3,y3);

   }

}

OUTPUT:

Enter coordinate for three points:

Enter coordinate for P0:

2   3

Enter coordinate for P1:

4   5

Enter coordinate for P2:

2   3

P2 is on the same line

No comments:

Post a Comment