Tower Line of Sight Issue ID:2616 JAVA


Four towers A, B, C, D are to be erected. Tower A is to communicate with tower C. Tower B is to communicate with tower D.

Line of sight issue can occur under the following conditions

- when tower B or D is in the straight line connecting A and C

- when tower A or C is in the straight line connecting B and D


The program must accept the co-ordinates of all four towers and print yes or no depending on whether Line of sight issue will occur or not.


Input Format:

The first line will contain X and Y co-ordinates of tower A separated by a space.

The second line will contain X and Y co-ordinates of tower B separated by a space.

The third line will contain X and Y co-ordinates of tower C separated by a space.

The fourth line will contain X and Y co-ordinates of tower D separated by a space


Output Format:

The first line will contain yes or no (smaller case)


Boundary Conditions:

The value of the co-ordinates will be from -500 to 500.


Example Input/Output 1:

0 0

0 -2

2 0

0 2


Output:

yes



Example Input/Output 2:

Input:

0 0

0 -2

2 0

0 -5


Output:

no


 import java.util.*;

public class Hello{

  

static class pair

    int first, second; 

    public pair(int first, int second)  

    { 

        this.first = first; 

        this.second = second; 

    }    

}


static int checkIntersection(pair p1,

                      pair p2,

                      pair p)

{

    int val;

   

    // If parallel to X-axis

    if (p1.second == p2.second

        && p1.second == p.second) {

   

        if (p.first <= Math.max(p1.first, p2.first)

            && (p.first >= Math.min(p1.first, p2.first)))

   

            // Point p lies between p1 and p2

            return 1;

    }

   

    // If parallel to Y-axis

    if (p1.first == p2.first

        && p1.first == p.first) {

   

        if (p.second <= Math.max(p1.second, p2.second)

            && (p.second >= Math.min(p1.second, p2.second)))

   

            // Point p lies between p1 and p2

            return 1;

    }

   

    // If point p satisfies the equation

    // of line joining p1 and p2

    else {

        val = (p.second - p1.second)

                  * (p2.first - p1.first)

              - (p.first - p1.first)

                    * (p2.second - p1.second);

   

        if (val == 0)

            if ((p.first <= Math.max(p1.first, p2.first)

                 && (p.first >= Math.min(p1.first, p2.first)))

                && (p.second <= Math.max(p1.second, p2.second)

                    && (p.second >= Math.min(p1.second, p2.second))))

                return 1;

    }

   

    return 0;

}

  

static void towerOfSight(pair a,

                  pair b,

                  pair c,

                  pair d)

{

    int flag = 0;

   

    if (checkIntersection(a, c, b) == 1)

   

        flag = 1;

   

    else if (checkIntersection(a, c, d) == 1)

   

        flag = 1;

   

    else if (checkIntersection(b, d, a) == 1)

   

        flag = 1;

   

    else if (checkIntersection(b, d, c) == 1)

   

        flag = 1;

   

    System.out.print(flag==1?"yes\n":"no\n");

}

   

// Driver code

public static void main(String[] args)

{

    Scanner sc=new Scanner(System.in);

    int a=sc.nextInt();

    int b=sc.nextInt();

    int c=sc.nextInt();

    int d=sc.nextInt();

    int e=sc.nextInt();

    int f=sc.nextInt();

    int g=sc.nextInt();

    int h=sc.nextInt();

    pair z = new pair( a, b );

   

    // Point B

    pair y = new pair( c, d );

   

    // Point C

    pair x = new pair( e,f );

   

    // Point D

    pair w = new pair( g,h);

   

    towerOfSight(z,y,x,w);

   

}

}


Comments

Popular Posts