Open In App

Point Clipping Algorithm in Computer Graphics

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The Point Clipping Algorithm is a fundamental algorithm used in Computer Graphics to determine whether a point lies inside or outside a specific region or boundary. It is particularly useful when dealing with objects that have complex shapes or when displaying only a portion of an image.

Clipping: In computer graphics our screen act as a 2-D coordinate system. it is not necessary that each and every point can be viewed on our viewing pane(i.e. our computer screen). We can view points, which lie in particular range (0,0) and (Xmax, Ymax). So, clipping is a procedure that identifies those portions of a picture that are either inside or outside of our viewing pane. 
In case of point clipping, we only show/print points on our window which are in range of our viewing pane, others points which are outside the range are discarded. 
Example 
 

Input :
 
Output :

Point Clipping Algorithm: 
 

  1. Get the minimum and maximum coordinates of both viewing pane.
  2. Get the coordinates for a point.
  3. Check whether given input lies between minimum and maximum coordinate of viewing pane.
  4. If yes display the point which lies inside the region otherwise discard it.

 

 

C++




// C++ program for point clipping Algorithm 
#include <bits/stdc++.h> 
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<n; i++) 
    
    if ( (XY[i][0] >= 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] <<","<<XY[i][1]<<"] "
        
    
  
    // print point coordinate outside viewing pane 
    cout<<"\n"<< endl;
    cout << "Point outside the viewing pane:"<<endl; 
    for (int i = 0; i < n; i++) 
    
        if ((XY[i][0] < Xmin) || (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<stdio.h>
//#include<graphics.h>
  
// 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<n; i++)
    {
    if ( (XY[i][0] >= 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<n; i++)
    {
        if ((XY[i][0] >= 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<n; i++)
    {
        if ((XY[i][0] < Xmin) || (XY[i][0] > 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<n; i++)
    {
    if ( (XY[i][0] >= 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<n; i++)
    {
        if ((XY[i][0] < Xmin) || (XY[i][0] > 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 point clipping Algorithm
  
# Function for point clipping 
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<n i++) 
      
    if ((XY[i][0] >= 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<n; i++)
    {
    if ( (XY[i,0] >= 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


Javascript




// JS code
const pointClip = (XY, n, Xmin, Ymin, Xmax, Ymax) => {
    console.log("Point inside the viewing pane:");
    for (let i = 0; i < n; i++) {
        if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax)) {
            if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
                console.log(`[${XY[i][0]},${XY[i][1]}]`);
        }
    }
  
    // print point coordinate outside viewing pane 
    console.log("\nPoint outside the viewing pane:");
    for (let i = 0; i < n; i++) {
        if ((XY[i][0] < Xmin) || (XY[i][0] > Xmax))
            console.log(`[${XY[i][0]},${XY[i][1]}]`);
        if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
            console.log(`[${XY[i][0]},${XY[i][1]}]`);
    }
};
  
// Driver code 
  
    let XY = [[10, 10], [-10, 10], [400, 100], [100, 400], [400, 400], [100, 40]];
    let Xmin = 0;
    let Xmax = 350;
    let Ymin = 0;
    let Ymax = 350;
    pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
  
// This code is contributed by ishankhandelwals.


Output: 
 

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

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

Time Complexity: O(N)
Auxiliary Space: O(1) 
Related Post : 
Line Clipping | Set 1 (Cohen–Sutherland Algorithm) 
Polygon Clipping | Sutherland–Hodgman Algorithm

Advantages :
The Point Clipping Algorithm has several advantages in computer graphics, including:

  1. Versatility: The algorithm can be applied to a wide range of objects, including points, lines, polygons, and other complex shapes. This makes it a versatile tool for a wide range of applications.
  2. Efficiency: The algorithm is relatively efficient and can be used to quickly identify and remove points that lie outside a specified region or boundary. This can help improve the performance of computer graphics applications, particularly those that involve rendering large or complex scenes.
  3. Accuracy: The Point Clipping Algorithm can be used to accurately clip objects and ensure that only the portions of an object that are visible or relevant are displayed. This can help improve the visual quality of images and animations.
  4. Flexibility: The algorithm can be easily adapted to different applications and scenarios. For example, different variants of the algorithm can be used to clip points, lines, and polygons, and to handle different types of regions and boundaries.
  5. Ease of Implementation: The algorithm is relatively straightforward to implement and can be integrated into computer graphics applications with minimal effort. This makes it an attractive option for developers who want to add clipping functionality to their applications.

Dis-Advantages :

There are also some disadvantages, including:

  1. Limited to 2D: The algorithm is designed to work in two dimensions, and it may not be suitable for handling objects in three-dimensional space. This can limit its usefulness in some applications, such as 3D modeling and virtual reality.
  2. Complexity: Some variants of the algorithm, such as the Cyrus-Beck algorithm, can be relatively complex and require a significant amount of computational resources to execute. This can make them less efficient than other algorithms, particularly for large or complex objects.
  3. Precision Issues: The algorithm can suffer from precision issues, particularly when dealing with objects that are very small or have complex shapes. This can result in clipping errors or inaccuracies in the rendered image.
  4. Performance: Although the algorithm is relatively efficient, it can still have a significant impact on the performance of a computer graphics application, particularly if it is used to clip a large number of objects.
  5. Limitations: The algorithm is limited in its ability to handle certain types of objects or regions. For example, it may not be suitable for handling objects with concave shapes or regions that are defined by curved boundaries.
     


Last Updated : 13 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads