📜  计算机图形学中的点裁剪算法

📅  最后修改于: 2021-04-23 17:44:39             🧑  作者: Mango

裁剪:在计算机图形学中,我们的屏幕充当2D坐标系。不必在我们的查看窗格(即我们的计算机屏幕)上查看每个点。我们可以查看点,它们位于(0,0)和(Xmax,Ymax)的特定范围内。因此,剪辑是识别图片在我们查看窗格内部或外部的那些部分的过程。
在进行点裁剪的情况下,我们仅在窗口中显示/打印位于查看窗格范围内的点,而其他超出范围的点将被丢弃。
例子

输入 : 剪辑2输出 : 剪辑3

点裁剪算法:

  1. 获取两个查看窗格的最小和最大坐标。
  2. 获取一个点的坐标。
  3. 检查给定的输入是否位于查看窗格的最小和最大坐标之间。
  4. 如果是,则显示该区域内的点,否则将其丢弃。
C++
// C++ program for point clipping Algorithm 
#include  
using namespace std;
  
// Function for point clipping 
void pointClip(int XY[][2], int n, int Xmin, int Ymin, 
                                int Xmax, int Ymax) 
{ 
    /*************** Code for graphics view 
    // initialize graphics mode 
    detectgraph(&gm,&gr); 
    initgraph(&gm,&gr,"d:\\tc\\BGI"); 
    for (int i=0; i= Xmin) && (XY[i][0] <= Xmax)) 
    { 
            if ( (XY[i][1] >= Ymin) && (XY[i][1] <= Ymax)) 
        putpixel(XY[i][0],XY[i][1],3); 
    } 
    } 
    **********************/
    /**** Arithmetic view ****/
    cout << "Point inside the viewing pane:" << endl; 
    for (int i = 0; i < n; i++) 
    { 
        if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax)) 
        { 
            if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax)) 
                cout <<"[" << XY[i][0] <<","< Xmax)) 
            cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
        if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax)) 
            cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
    } 
} 
  
// Driver code 
int main() 
{ 
    int XY[6][2] = {{10, 10}, {-10, 10}, {400, 100}, 
                    {100, 400}, {400, 400}, {100, 40}}; 
  
    // getmaxx() & getmaxy() will return Xmax, Ymax 
    // value if graphics.h is included 
    int Xmin = 0; 
    int Xmax = 350; 
    int Ymin = 0; 
    int Ymax = 350; 
    pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax); 
    return 0; 
} 
  
// This code is contributed by SHUBHAMSINGH10


C
// C program for point clipping Algorithm
#include
//#include
  
// Function for point clipping
void pointClip(int XY[][2], int n, int Xmin, int Ymin,
                                   int Xmax, int Ymax)
{
    /*************** Code for graphics view
    // initialize graphics mode
    detectgraph(&gm,&gr);
    initgraph(&gm,&gr,"d:\\tc\\BGI");
    for (int i=0; i= Xmin) && (XY[i][0] <= Xmax))
    {
            if ( (XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
        putpixel(XY[i][0],XY[i][1],3);
    }
    }
    **********************/
    /**** Arithmetic view ****/
    printf ("Point inside the viewing pane:\n");
    for (int i=0; i= Xmin) && (XY[i][0] <= Xmax))
        {
            if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
                printf ("[%d, %d] ", XY[i][0], XY[i][1]);
        }
    }
  
    // print point coordinate outside viewing pane
    printf ("\nPoint outside the viewing pane:\n");
    for (int i=0; i Xmax))
            printf ("[%d, %d] ", XY[i][0], XY[i][1]);
        if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
            printf ("[%d, %d] ", XY[i][0], XY[i][1]);
    }
}
  
// Driver code
int main()
{
    int XY[6][2] = {{10,10}, {-10,10}, {400,100},
                    {100,400}, {400,400}, {100,40}};
  
    // getmaxx() & getmaxy() will return Xmax, Ymax
    // value if graphics.h is included
    int Xmin = 0;
    int Xmax = 350;
    int Ymin = 0;
    int Ymax = 350;
    pointClip(XY, 6,  Xmin, Ymin, Xmax, Ymax);
    return 0;
}


Java
// Java program for point clipping Algorithm
class GFG 
{
  
// Function for point clipping
static void pointClip(int XY[][], int n, 
                        int Xmin, int Ymin,
                        int Xmax, int Ymax)
{
    /*************** Code for graphics view
    // initialize graphics mode
    detectgraph(&gm,&gr);
    initgraph(&gm,&gr,"d:\\tc\\BGI");
    for (int i=0; i= Xmin) && (XY[i][0] <= Xmax))
    {
            if ( (XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
        putpixel(XY[i][0],XY[i][1],3);
    }
    }
    **********************/
    /**** Arithmetic view ****/
    System.out.printf ("Point inside the viewing pane:\n");
    for (int i = 0; i < n; i++)
    {
        if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax))
        {
            if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
                System.out.printf ("[%d, %d] ", XY[i][0], XY[i][1]);
        }
    }
  
    // print point coordinate outside viewing pane
    System.out.printf ("\nPoint outside the viewing pane:\n");
    for (int i=0; i Xmax))
            System.out.printf ("[%d, %d] ", XY[i][0], XY[i][1]);
        if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
            System.out.printf ("[%d, %d] ", XY[i][0], XY[i][1]);
    }
}
  
// Driver code
public static void main(String[] args) 
{
        int XY[][] = {{10,10}, {-10,10}, {400,100},
                    {100,400}, {400,400}, {100,40}};
  
    // getmaxx() & getmaxy() will return Xmax, Ymax
    // value if graphics.h is included
    int Xmin = 0;
    int Xmax = 350;
    int Ymin = 0;
    int Ymax = 350;
    pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
}
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 program for poclipping Algorithm
  
# Function for poclipping 
def pointClip(XY, n, Xmin, Ymin, Xmax, Ymax):
  
    """************** Code for graphics view 
    # initialize graphics mode 
    detectgraph(&gm, &gr) 
    initgraph(&gm, &gr, "d:\\tc\\BGI") 
    for (i=0 i= Xmin) and 
        (XY[i][0] <= Xmax)) 
      
        if ((XY[i][1] >= Ymin) and 
            (XY[i][1] <= Ymax)) 
        putpixel(XY[i][0], XY[i][1], 3) 
      
    *********************"""
    """*** Arithmetic view ***"""
    print("Point inside the viewing pane:") 
    for i in range(n):
        if ((XY[i][0] >= Xmin) and 
            (XY[i][0] <= Xmax)): 
            if ((XY[i][1] >= Ymin) and 
                (XY[i][1] <= Ymax)): 
                print("[", XY[i][0], ", ", XY[i][1], 
                      "]", sep = "", end = " ") 
          
    # prpocoordinate outside viewing pane 
    print("\n\nPoint outside the viewing pane:") 
    for i in range(n):     
        if ((XY[i][0] < Xmin) or (XY[i][0] > Xmax)) :
            print("[", XY[i][0], ", ", XY[i][1],
                  "]", sep = "", end = " ") 
        if ((XY[i][1] < Ymin) or (XY[i][1] > Ymax)) :
            print("[", XY[i][0], ", ", XY[i][1], 
                  "]", sep = "", end = " ") 
  
# Driver Code
if __name__ == '__main__':
    XY = [[10, 10], [-10, 10], [400, 100], 
          [100, 400], [400, 400], [100, 40]] 
  
    # getmaxx() & getmaxy() will return Xmax, 
    # Ymax value if graphics.h is included 
    Xmin = 0
    Xmax = 350
    Ymin = 0
    Ymax = 350
    pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax)
  
# This code is contributed by
# SHUBHAMSINGH10


C#
// C# program for point clipping Algorithm
using System;
  
class GFG 
{
  
// Function for point clipping
static void pointClip(int [,]XY, int n, 
                        int Xmin, int Ymin,
                        int Xmax, int Ymax)
{
    /*************** Code for graphics view
    // initialize graphics mode
    detectgraph(&gm,&gr);
    initgraph(&gm,&gr,"d:\\tc\\BGI");
    for (int i=0; i= Xmin) && (XY[i,0] <= Xmax))
    {
            if ( (XY[i,1] >= Ymin) && (XY[i,1] <= Ymax))
        putpixel(XY[i,0],XY[i,1],3);
    }
    }
    **********************/
    /**** Arithmetic view ****/
    Console.Write("Point inside the viewing pane:\n");
    for (int i = 0; i < n; i++)
    {
        if ((XY[i, 0] >= Xmin) && (XY[i, 0] <= Xmax))
        {
            if ((XY[i, 1] >= Ymin) && (XY[i, 1] <= Ymax))
                Console.Write("[{0}, {1}] ", XY[i, 0], XY[i, 1]);
        }
    }
  
    // print point coordinate outside viewing pane
    Console.Write("\nPoint outside the viewing pane:\n");
    for (int i = 0; i < n; i++)
    {
        if ((XY[i, 0] < Xmin) || (XY[i, 0] > Xmax))
            Console.Write("[{0}, {1}] ", XY[i, 0], XY[i, 1]);
        if ((XY[i, 1] < Ymin) || (XY[i, 1] > Ymax))
            Console.Write("[{0}, {1}] ", XY[i, 0], XY[i, 1]);
    }
}
  
// Driver code
public static void Main(String[] args) 
{
        int [,]XY = {{10, 10}, {-10, 10}, {400, 100},
                    {100, 400}, {400, 400}, {100, 40}};
  
    // getmaxx() & getmaxy() will return Xmax, Ymax
    // value if graphics.h is included
    int Xmin = 0;
    int Xmax = 350;
    int Ymin = 0;
    int Ymax = 350;
    pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
}
}
  
// This code contributed by Rajput-Ji


输出:

Point inside the viewing pane:
[10, 10] [100, 40] 

Point outside the viewing pane:
[-10, 10] [400, 100] [100, 400] [400, 400] [400, 400] 

相关文章:
剪线|集合1(Cohen–Sutherland算法)
多边形裁剪| Sutherland-Hodgman算法