📜  检查是否出现视塔问题

📅  最后修改于: 2021-04-24 21:54:36             🧑  作者: Mango

给定需要建造塔的四个坐标A,B,CD ,任务是检查是否出现视塔问题。

例子:

方法:

  • 如果A和C平行于X轴,请检查B或D的y坐标是否等于A和C的y坐标,以及x坐标是否介于A和C的坐标之间。
  • 如果A和C平行于Y轴,请检查B或D的x坐标是否等于A和C的x坐标,以及y坐标在A和C的Y坐标之间。
  • 否则,检查B或D是否满足A和C的线方程。
  • 同样,按照上述三个步骤检查A或C是否位于B或D之间。

下面的代码是上述方法的实现:

C++
// C++ program to check if tower
// of sight issue occurs or not
  
#include 
using namespace std;
  
// Function to check if point p lies in
// between the line joining p1 and p2
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 <= max(p1.first, p2.first)
            && (p.first >= 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 <= max(p1.second, p2.second)
            && (p.second >= 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 <= max(p1.first, p2.first)
                 && (p.first >= min(p1.first, p2.first)))
                && (p.second <= max(p1.second, p2.second)
                    && (p.second >= min(p1.second, p2.second))))
                return 1;
    }
  
    return 0;
}
  
// Function to check if tower
// of sight issue occurred
void towerOfSight(pair a,
                  pair b,
                  pair c,
                  pair d)
{
    int flag = 0;
  
    if (checkIntersection(a, c, b))
  
        // B lies between AC
        flag = 1;
  
    else if (checkIntersection(a, c, d))
  
        // D lies between AC
        flag = 1;
  
    else if (checkIntersection(b, d, a))
  
        // A lies between BD
        flag = 1;
  
    else if (checkIntersection(b, d, c))
  
        // C lies between BD
        flag = 1;
  
    flag ? cout << "Yes\n"
         : cout << "No\n";
}
  
// Driver code
int main()
{
    // Point A
    pair a = { 0, 0 };
  
    // Point B
    pair b = { 0, -2 };
  
    // Point C
    pair c = { 2, 0 };
  
    // Point D
    pair d = { 0, 2 };
  
    towerOfSight(a, b, c, d);
  
    return 0;
}


Java
// Java program to check if tower
// of sight issue occurs or not
class GFG{
  
static class pair
{ 
    int first, second; 
    public pair(int first, int second)  
    { 
        this.first = first; 
        this.second = second; 
    }    
}
    
// Function to check if point p lies in
// between the line joining p1 and p2
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;
}
   
// Function to check if tower
// of sight issue occurred
static void towerOfSight(pair a,
                  pair b,
                  pair c,
                  pair d)
{
    int flag = 0;
   
    if (checkIntersection(a, c, b) == 1)
   
        // B lies between AC
        flag = 1;
   
    else if (checkIntersection(a, c, d) == 1)
   
        // D lies between AC
        flag = 1;
   
    else if (checkIntersection(b, d, a) == 1)
   
        // A lies between BD
        flag = 1;
   
    else if (checkIntersection(b, d, c) == 1)
   
        // C lies between BD
        flag = 1;
   
    System.out.print(flag==1?"Yes\n":"No\n");
}
   
// Driver code
public static void main(String[] args)
{
    // Point A
    pair a = new pair( 0, 0 );
   
    // Point B
    pair b = new pair( 0, -2 );
   
    // Point C
    pair c = new pair( 2, 0 );
   
    // Point D
    pair d = new pair( 0, 2 );
   
    towerOfSight(a, b, c, d);
   
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python 3 program to check if tower
# of sight issue occurs or not
   
# Function to check if point p lies in
# between the line joining p1 and p2
def checkIntersection(p1,p2,p):
   
    # If parallel to X-axis
    if (p1[1] == p2[1]
        and p1[1] == p[1]):
   
        if (p[0] <= max(p1[0], p2[0])
            and (p[0] >= min(p1[0], p2[0]))):
   
            # Point p lies between p1 and p2
            return 1
   
    # If parallel to Y-axis
    if (p1[0] == p2[0]
        and p1[0] == p[0]):
   
        if (p[1] <= max(p1[1], p2[1])
            and (p[1] >= min(p1[1], p2[1]))):
   
            # Point p lies between p1 and p2
            return 1
   
    # If point p satisfies the equation
    # of line joining p1 and p2
    else :
        val = ((p[1] - p1[1])*(p2[0] - p1[0])
              - (p[0] - p1[0])
                    * (p2[1] - p1[1]))
   
        if (val == 0):
            if ((p[0] <= max(p1[0], p2[0])
                 and (p[0] >= min(p1[0], p2[0])))
                and (p[1] <= max(p1[1], p2[1])
                    and (p[1] >= min(p1[1], p2[1])))):
                return 1
   
    return 0
   
# Function to check if tower
# of sight issue occurred
def towerOfSight(a, b, c, d):
    flag = 0
   
    if (checkIntersection(a, c, b)):
   
        # B lies between AC
        flag = 1
   
    elif (checkIntersection(a, c, d)):
        # D lies between AC
        flag = 1
   
    elif (checkIntersection(b, d, a)):
   
        # A lies between BD
        flag = 1
   
    elif (checkIntersection(b, d, c)):
   
        # C lies between BD
        flag = 1
   
    if(flag):
        print("Yes")
    else:
        print("No")
   
# Driver code
if __name__ == "__main__":
  
    # Point A
    a = [ 0, 0 ]
   
    # Point B
    b = [ 0, -2 ]
   
    # Point C
    c = [ 2, 0 ]
   
    # Point D
    d = [ 0, 2 ]
   
    towerOfSight(a, b, c, d)
   
# This code is contributed by chitranayal


C#
// C# program to check if tower
// of sight issue occurs or not
using System;
  
class GFG{
   
class pair
{ 
    public int first, second; 
    public pair(int first, int second)  
    { 
        this.first = first; 
        this.second = second; 
    }    
}
     
// Function to check if point p lies in
// between the line joining p1 and p2
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;
}
    
// Function to check if tower
// of sight issue occurred
static void towerOfSight(pair a,
                  pair b,
                  pair c,
                  pair d)
{
    int flag = 0;
    
    if (checkIntersection(a, c, b) == 1)
    
        // B lies between AC
        flag = 1;
    
    else if (checkIntersection(a, c, d) == 1)
    
        // D lies between AC
        flag = 1;
    
    else if (checkIntersection(b, d, a) == 1)
    
        // A lies between BD
        flag = 1;
    
    else if (checkIntersection(b, d, c) == 1)
    
        // C lies between BD
        flag = 1;
    
    Console.Write(flag==1?"Yes\n":"No\n");
}
    
// Driver code
public static void Main(String[] args)
{
    // Point A
    pair a = new pair( 0, 0 );
    
    // Point B
    pair b = new pair( 0, -2 );
    
    // Point C
    pair c = new pair( 2, 0 );
    
    // Point D
    pair d = new pair( 0, 2 );
    
    towerOfSight(a, b, c, d);
    
}
}
  
// This code is contributed by Princi Singh


输出:
Yes