📌  相关文章
📜  计算由给定的水平和垂直线段切出的三角形的数量

📅  最后修改于: 2021-05-17 17:26:53             🧑  作者: Mango

给定一个数组triangles [] [],它由{x1,y1,x2,y2,x3,y3}形式的N个三角形组成,而数组cuts []M条水平和垂直线组成,形式为“ X = x”,或者“ Y = y”代表线段的方程。任务是打印每次切割相交的三角形数量,以使三角形的左,右部分的面积均应大于零。

例子:

方法:以下是线段将三角形分为具有非零区域的两个部分的条件:

  • 如果在X轴上进行切割,并且切割严格位于三角形的最小X坐标和最大X坐标之间,则它将以这样的方式划分三角形,使得左侧和右侧部分的面积应大于零。
  • 同样,如果在Y轴上进行切割并且严格位于三角形的最小Y坐标与最大Y坐标之间,则它将以这样的方式划分三角形,使得左,右部分的面积应大于零。

请按照以下步骤解决问题:

  1. 创建一个结构来存储每个三角形的最大和最小XY坐标。
  2. [0,M – 1]范围内遍历array cuts [] array。
  3. 对于每个剪切,用0初始化一个计数器计数以存储当前剪切的答案,并开始从j = 0到N – 1遍历triangles []数组。
  4. 检查每个三角形, cuts [i]的格式为X = x,即垂直切割和x严格位于i三角形的最大X坐标和最小X坐标之间,增加计数器计数,否则继续检查每个切割的其他三角形。
  5. 计算出cuts [i]的答案后,打印count
  6. 对每个切割重复上述步骤,并打印每行切割的三角形的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Store the minimum and maximum
// X and Y coordinates
struct Tri {
    int MinX, MaxX, MinY, MaxY;
};
 
// Function to convert string to int
int StringtoInt(string s)
{
    stringstream geek(s);
 
    int x;
    geek >> x;
 
    return x;
}
 
// Function to print the number of
// triangles cut by each line segment
int TriangleCuts(
    vector > Triangle,
    string Cuts[], int N, int M, int COL)
{
 
    // Initialize Structure
    Tri Minimized[N];
 
    // Find maximum and minimum X and Y
    // coordinates for each triangle
    for (int i = 0; i < N; i++) {
        int x1 = Triangle[i][0];
        int y1 = Triangle[i][1];
        int x2 = Triangle[i][2];
        int y2 = Triangle[i][3];
        int x3 = Triangle[i][4];
        int y3 = Triangle[i][5];
 
        // Minimum X
        Minimized[i].MinX
            = min({ x1, x2, x3 });
 
        // Maximum X
        Minimized[i].MaxX
            = max({ x1, x2, x3 });
 
        // Minimum Y
        Minimized[i].MinY
            = min({ y1, y2, y3 });
 
        // Maximum Y
        Minimized[i].MaxY
            = max({ y1, y2, y3 });
    }
 
    // Traverse each cut from 0 to M-1
    for (int i = 0; i < M; i++) {
 
        string Cut = Cuts[i];
 
        // Store number of trianges cut
        int CutCount = 0;
 
        // Extract value from the line
        // segment string
        int CutVal = StringtoInt(
            Cut.substr(2, Cut.size()));
 
        // If cut is made on X-axis
        if (Cut[0] == 'X') {
 
            // Check for each triangle
            // if x lies b/w max and
            // min X coordinates
            for (int j = 0; j < N; j++) {
 
                if ((Minimized[j].MinX)
                        < (CutVal)
                    && (Minimized[j].MaxX)
                           > (CutVal)) {
                    CutCount++;
                }
            }
        }
 
        // If cut is made on Y-axis
        else if (Cut[0] == 'Y') {
 
            // Check for each triangle
            // if y lies b/w max and
            // min Y coordinates
            for (int j = 0; j < N; j++) {
                if ((Minimized[j].MinY)
                        < (CutVal)
                    && (Minimized[j].MaxY)
                           > (CutVal)) {
                    CutCount++;
                }
            }
        }
 
        // Print answer for ith cut
        cout << CutCount << " ";
    }
}
 
// Driver Code
int main()
{
    // Given coordinates of traingles
    vector > Triangle
        = { { 0, 2, 2, 9, 8, 5 },
            { 5, 0, 6, 3, 7, 0 } };
 
    int N = Triangle.size();
 
    int COL = 6;
 
    // Given cuts of lines
    string Cuts[] = { "X=2", "Y=2", "Y=9" };
    int M = sizeof(Cuts) / sizeof(Cuts[0]);
 
    // Function Call
    TriangleCuts(Triangle, Cuts,
                 N, M, COL);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Store the minimum and maximum
// X and Y coordinates
static class Tri
{
    int MinX, MaxX, MinY, MaxY;
};
 
// Function to convert String to int
static int StringtoInt(String s)
{
    return Integer.valueOf(s);
}
 
static int min(int a, int b, int c)
{
    return Math.min(a, Math.min(b, c));
}
 
static int max(int a, int b, int c)
{
    return Math.max(a, Math.max(b, c));
}
 
// Function to print the number of
// triangles cut by each line segment
static void TriangleCuts(int[][] Triangle,
          String Cuts[], int N, int M, int COL)
{
     
    // Initialize Structure
    Tri []Minimized = new Tri[N];
    for(int i = 0; i < N; i++)
    {
        Minimized[i] = new Tri();
        Minimized[i].MaxX = 0;
        Minimized[i].MaxY = 0;
        Minimized[i].MinX = 0;
        Minimized[i].MinY = 0;
    }
 
    // Find maximum and minimum X and Y
    // coordinates for each triangle
    for(int i = 0; i < N; i++)
    {
        int x1 = Triangle[i][0];
        int y1 = Triangle[i][1];
        int x2 = Triangle[i][2];
        int y2 = Triangle[i][3];
        int x3 = Triangle[i][4];
        int y3 = Triangle[i][5];
 
        // Minimum X
        Minimized[i].MinX = min(x1, x2, x3);
 
        // Maximum X
        Minimized[i].MaxX = max(x1, x2, x3);
 
        // Minimum Y
        Minimized[i].MinY = min(y1, y2, y3);
 
        // Maximum Y
        Minimized[i].MaxY = max(y1, y2, y3);
    }
 
    // Traverse each cut from 0 to M-1
    for(int i = 0; i < M; i++)
    {
        String Cut = Cuts[i];
         
        // Store number of trianges cut
        int CutCount = 0;
 
        // Extract value from the line
        // segment String
        int CutVal = StringtoInt(
            Cut.substring(2, Cut.length()));
 
        // If cut is made on X-axis
        if (Cut.charAt(0) == 'X')
        {
             
            // Check for each triangle
            // if x lies b/w max and
            // min X coordinates
            for(int j = 0; j < N; j++)
            {
                 
                if ((Minimized[j].MinX) < (CutVal) &&
                    (Minimized[j].MaxX) > (CutVal))
                {
                    CutCount++;
                }
            }
        }
 
        // If cut is made on Y-axis
        else if (Cut.charAt(0) == 'Y')
        {
             
            // Check for each triangle
            // if y lies b/w max and
            // min Y coordinates
            for(int j = 0; j < N; j++)
            {
                if ((Minimized[j].MinY) < (CutVal) &&
                    (Minimized[j].MaxY) > (CutVal))
                {
                    CutCount++;
                }
            }
        }
         
        // Print answer for ith cut
        System.out.print(CutCount + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given coordinates of traingles
    int[][] Triangle = { { 0, 2, 2, 9, 8, 5 },
                         { 5, 0, 6, 3, 7, 0 } };
 
    int N = Triangle.length;
 
    int COL = 6;
 
    // Given cuts of lines
    String Cuts[] = { "X=2", "Y=2", "Y=9" };
    int M = Cuts.length;
 
    // Function Call
    TriangleCuts(Triangle, Cuts,
                 N, M, COL);
}
}
 
// This code is contributed by Princi Singh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Store the minimum and maximum
// X and Y coordinates
public class Tri
{
    public int MinX, MaxX, MinY, MaxY;
};
 
// Function to convert String to int
static int StringtoInt(String s)
{
    return Int32.Parse(s);
}
 
static int min(int a, int b, int c)
{
    return Math.Min(a, Math.Min(b, c));
}
 
static int max(int a, int b, int c)
{
    return Math.Max(a, Math.Max(b, c));
}
 
// Function to print the number of
// triangles cut by each line segment
static void TriangleCuts(int[,] Triangle,
                         String []Cuts,
                         int N, int M,
                         int COL)
{
     
    // Initialize Structure
    Tri []Minimized = new Tri[N];
    for(int i = 0; i < N; i++)
    {
        Minimized[i] = new Tri();
        Minimized[i].MaxX = 0;
        Minimized[i].MaxY = 0;
        Minimized[i].MinX = 0;
        Minimized[i].MinY = 0;
    }
 
    // Find maximum and minimum X and Y
    // coordinates for each triangle
    for(int i = 0; i < N; i++)
    {
        int x1 = Triangle[i, 0];
        int y1 = Triangle[i, 1];
        int x2 = Triangle[i, 2];
        int y2 = Triangle[i, 3];
        int x3 = Triangle[i, 4];
        int y3 = Triangle[i, 5];
 
        // Minimum X
        Minimized[i].MinX = min(x1, x2, x3);
 
        // Maximum X
        Minimized[i].MaxX = max(x1, x2, x3);
 
        // Minimum Y
        Minimized[i].MinY = min(y1, y2, y3);
 
        // Maximum Y
        Minimized[i].MaxY = max(y1, y2, y3);
    }
 
    // Traverse each cut from 0 to M-1
    for(int i = 0; i < M; i++)
    {
        String Cut = Cuts[i];
         
        // Store number of trianges cut
        int CutCount = 0;
 
        // Extract value from the line
        // segment String
        int CutVal = StringtoInt(
            Cut.Substring(2, Cut.Length - 2));
 
        // If cut is made on X-axis
        if (Cut[0] == 'X')
        {
             
            // Check for each triangle
            // if x lies b/w max and
            // min X coordinates
            for(int j = 0; j < N; j++)
            {
                if ((Minimized[j].MinX) < (CutVal) &&
                    (Minimized[j].MaxX) > (CutVal))
                {
                    CutCount++;
                }
            }
        }
 
        // If cut is made on Y-axis
        else if (Cut[0] == 'Y')
        {
             
            // Check for each triangle
            // if y lies b/w max and
            // min Y coordinates
            for(int j = 0; j < N; j++)
            {
                if ((Minimized[j].MinY) < (CutVal) &&
                    (Minimized[j].MaxY) > (CutVal))
                {
                    CutCount++;
                }
            }
        }
         
        // Print answer for ith cut
        Console.Write(CutCount + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given coordinates of traingles
    int[,] Triangle = { { 0, 2, 2, 9, 8, 5 },
                        { 5, 0, 6, 3, 7, 0 } };
 
    int N = Triangle.GetLength(0);
 
    int COL = 6;
 
    // Given cuts of lines
    String []Cuts = { "X=2", "Y=2", "Y=9" };
    int M = Cuts.Length;
 
    // Function Call
    TriangleCuts(Triangle, Cuts,
                 N, M, COL);
}
}
 
// This code is contributed by Princi Singh


输出:
1 1 0






时间复杂度: O(M * N)
辅助空间: O(M + N)