The Wayback Machine - https://web.archive.org/web/20240726050017/https://www.geeksforgeeks.org/window-to-viewport-transformation-in-computer-graphics-with-implementation/
Open In App

Window to Viewport Transformation in Computer Graphics with Implementation

Last Updated : 08 Aug, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Window to Viewport Transformation is the process of transforming 2D world-coordinate objects to device coordinates. Objects inside the world or clipping window are mapped to the viewport which is the area on the screen where world coordinates are mapped to be displayed.

General Terms: 

  • World coordinate – It is the Cartesian coordinate w.r.t which we define the diagram, like Xwmin, Xwmax, Ywmin, Ywmax
  • Device Coordinate –It is the screen coordinate where the objects are to be displayed, like Xvmin, Xvmax, Yvmin, Yvmax
  • Window –It is the area on the world coordinate selected for display.
  • ViewPort –It is the area on the device coordinate where graphics is to be displayed.

Mathematical Calculation of Window to Viewport: 

It may be possible that the size of the Viewport is much smaller or greater than the Window. In these cases, we have to increase or decrease the size of the Window according to the Viewport and for this, we need some mathematical calculations.

(xw, yw): A point on Window
(xv, yv): Corresponding  point on Viewport

We have to calculate the point (xv, yv)

Now the relative position of the object in Window and Viewport are same.

For x coordinate,                        

 For y coordinate,

So, after calculating for x and y coordinate, we get

Where sx is the scaling factor of x coordinate and sy is the scaling factor of y coordinate

Example: Let us assume, 

  • for window, Xwmin = 20, Xwmax = 80, Ywmin = 40, Ywmax = 80.
  • for viewport, Xvmin = 30, Xvmax = 60, Yvmin = 40, Yvmax = 60.
  • Now a point ( Xw, Yw ) be ( 30, 80 ) on the window. We have to calculate that point on the viewport 
    i.e ( Xv, Yv ).
  • First of all, calculate the scaling factor of x coordinate Sx and the scaling factor of y coordinate Sy using the above-mentioned formula.
Sx = ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60
Sy = ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40
  • So, now calculate the point on the viewport ( Xv, Yv ).
Xv = 30 + ( 30 - 20 ) * ( 30 / 60 ) = 35
Yv = 40 + ( 80 - 40 ) * ( 20 / 40 ) = 60
  • So, the point on window ( Xw, Yw ) = ( 30, 80 ) will be ( Xv, Yv ) = ( 35, 60 ) on viewport.

Here is the implementation of the above approach:

C++




// C++ program to implement
// Window to ViewPort Transformation
 
#include <iostream>
using namespace std;
 
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,
                      int y_wmax, int x_wmin, int y_wmin,
                      int x_vmax, int y_vmax, int x_vmin,
                      int y_vmin)
{
    // point on viewport
    int x_v, y_v;
 
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
 
    // calculating Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
 
    // calculating the point on viewport
    x_v = x_vmin + (float)((x_w - x_wmin) * sx);
    y_v = y_vmin + (float)((y_w - y_wmin) * sy);
 
    cout<< "The point on viewport: ("<<x_v  <<","<< y_v<<")" ;
}
 
// Driver Code
int main()
{
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
 
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
 
    // point on window
    int x_w = 30, y_w = 80;
 
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}
 
// This code is contributed by khusboogoyal499.


C




// C program to implement
// Window to ViewPort Transformation
 
#include <stdio.h>
 
// Function for window to viewport transformation
void WindowtoViewport(int x_w, int y_w, int x_wmax,
                      int y_wmax, int x_wmin, int y_wmin,
                      int x_vmax, int y_vmax, int x_vmin,
                      int y_vmin)
{
    // point on viewport
    int x_v, y_v;
 
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
 
    // calculating Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
 
    // calculating the point on viewport
    x_v = x_vmin + (float)((x_w - x_wmin) * sx);
    y_v = y_vmin + (float)((y_w - y_wmin) * sy);
 
    printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}
 
// Driver Code
void main()
{
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
 
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
 
    // point on window
    int x_w = 30, y_w = 80;
 
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}
 
//this code is added by khushboogoyal499


Java




// Java program to implement
// Window to ViewPort Transformation
class GFG
{
 
// Function for window to viewport transformation
static void WindowtoViewport(int x_w, int y_w, int x_wmax,
                    int y_wmax, int x_wmin, int y_wmin,
                    int x_vmax, int y_vmax, int x_vmin,
                    int y_vmin)
{
    // point on viewport
    int x_v, y_v;
 
    // scaling factors for x coordinate and y coordinate
    float sx, sy;
 
    // calculating Sx and Sy
    sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
 
    // calculating the point on viewport
    x_v = (int) (x_vmin + (float)((x_w - x_wmin) * sx));
    y_v = (int) (y_vmin + (float)((y_w - y_wmin) * sy));
 
    System.out.printf("The point on viewport: (%d, %d )\n ", x_v, y_v);
}
 
// Driver Code
public static void main(String[] args)
{
 
    // boundary values for window
    int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
 
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
 
    // point on window
    int x_w = 30, y_w = 80;
 
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 program to implement
# Window to ViewPort Transformation
 
# Function for window to viewport transformation
def WindowtoViewport(x_w, y_w, x_wmax, y_wmax,
                       x_wmin, y_wmin, x_vmax,
                       y_vmax, x_vmin, y_vmin):
                            
    # point on viewport
    # calculating Sx and Sy
    sx = (x_vmax - x_vmin) / (x_wmax - x_wmin)
    sy = (y_vmax - y_vmin) / (y_wmax - y_wmin)
 
    # calculating the point on viewport
    x_v = x_vmin + ((x_w - x_wmin) * sx)
    y_v = y_vmin + ((y_w - y_wmin) * sy)
 
    print("The point on viewport:(", int(x_v),
                                ",", int(y_v), ")")
 
# Driver Code
if __name__ == '__main__':
     
    # boundary values for window
    x_wmax = 80
    y_wmax = 80
    x_wmin = 20
    y_wmin = 40
 
    # boundary values for viewport
    x_vmax = 60
    y_vmax = 60
    x_vmin = 30
    y_vmin = 40
 
    # point on window
    x_w = 30
    y_w = 80
 
    WindowtoViewport(30, 80, 80, 80, 20,
                     40, 60, 60, 30, 40)
     
# This code is contributed by Surendra_Gangwar


C#




// C# program to implement
// Window to ViewPort Transformation
using System;
 
class GFG
{
 
// Function for window to viewport transformation
static void WindowtoViewport(int x_w, int y_w,
                             int x_wmax, int y_wmax,
                             int x_wmin, int y_wmin,
                             int x_vmax, int y_vmax,
                             int x_vmin, int y_vmin)
{
    // point on viewport
    int x_v, y_v;
 
    // scaling factors for x coordinate
    // and y coordinate
    float sx, sy;
 
    // calculating Sx and Sy
    sx = (float)(x_vmax - x_vmin) /
                (x_wmax - x_wmin);
    sy = (float)(y_vmax - y_vmin) /
                (y_wmax - y_wmin);
 
    // calculating the point on viewport
    x_v = (int) (x_vmin +
        (float)((x_w - x_wmin) * sx));
    y_v = (int) (y_vmin +
        (float)((y_w - y_wmin) * sy));
 
    Console.Write("The point on viewport: " +
                 "({0}, {1} )\n ", x_v, y_v);
}
 
// Driver Code
public static void Main(String[] args)
{
 
    // boundary values for window
    int x_wmax = 80, y_wmax = 80,
        x_wmin = 20, y_wmin = 40;
 
    // boundary values for viewport
    int x_vmax = 60, y_vmax = 60,
        x_vmin = 30, y_vmin = 40;
 
    // point on window
    int x_w = 30, y_w = 80;
 
    WindowtoViewport(30, 80, 80, 80, 20,
                     40, 60, 60, 30, 40);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
// Javascript program to implement
// Window to ViewPort Transformation
 
// Function for window to viewport transformation
function WindowtoViewport(x_w, y_w, x_wmax,y_wmax, x_wmin,
                    y_wmin,x_vmax, y_vmax, x_vmin,y_vmin)
{
    // point on viewport
    let x_v, y_v;
 
    // scaling factors for x coordinate and y coordinate
    let sx, sy;
 
    // calculating Sx and Sy
    sx = (x_vmax - x_vmin) / (x_wmax - x_wmin);
    sy = (y_vmax - y_vmin) / (y_wmax - y_wmin);
 
    // calculating the point on viewport
    x_v = x_vmin + ((x_w - x_wmin) * sx);
    y_v = y_vmin + ((y_w - y_wmin) * sy);
 
    document.write("The point on viewport: (" + x_v + ", "
                                + y_v + " )<br>");
}
 
// Driver Code
    // boundary values for window
    let x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
 
    // boundary values for viewport
    let x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
 
    // point on window
    let x_w = 30, y_w = 80;
 
    WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
 
</script>


Output: 

The point on viewport: (35, 60 )

 

Time Complexity: O(1), as we are not using any looping statements.

Space Complexity: O(1) , as we are not using any extra space.



Previous Article
Next Article

Similar Reads

2D Transformation in Computer Graphics | Set 1 (Scaling of Objects)
We can use a 2 × 2 matrix to change or transform, a 2D vector. This kind of operation, which takes in a 2-vector and produces another 2-vector by a simple matrix multiplication, is a linear transformation. By this simple formula, we can achieve a variety of useful transformations, depending on what we put in the entries of the matrix. For our purpo
5 min read
Computer Graphics - 3D Shearing Transformation
Shearing transformation is the same as we see in 2D space, but here we have to deal with the x, y, and z axes whereas in 2D we deal with the only x and y axes. Shearing is the process of slanting an object in 3D space either in x, y, or in the z-direction. Shearing changes(or deformed) the shape of the object. AS we are discussing 3D space so shear
4 min read
Computer Graphics - 3D Translation Transformation
3-D Transformation: In very general terms a 3D model is a mathematical representation of a physical entity that occupies space. In more practical terms, a 3D model is made of a description of its shape and a description of its color appearance.3-D Transformation is the process of manipulating the view of a three-D object with respect to its origina
3 min read
Computer Graphics - 3D Scaling Transformation
Prerequisite: Computer Graphics – 3D Translation Transformation Scaling Transformation : It is performed to resize the 3D-object that is the dimension of the object can be scaled(alter) in any of the x, y, z direction through Sx, Sy, Sz scaling factors. Matrix representation of Scaling transformation Condition : [Tex]\large S[x, y, z]= \left [ \beg
3 min read
Computer Graphics - Reflection Transformation in 3D
Reflection in 3D space is quite similar to the reflection in 2D space, but a single difference is there in 3D, here we have to deal with three axes (x, y, z). Reflection is nothing but a mirror image of an object. Three kinds of Reflections are possible in 3D space: Reflection along the X-Y plane.Reflection along Y-Z plane.Reflection along X-Z plan
3 min read
Computer Graphics - 3D Composite Transformation
3-D Transformation is the process of manipulating the view of a three-D object with respect to its original position by modifying its physical attributes through various methods of transformation like Translation, Scaling, Rotation, Shear, etc. Types of Transformation: Translation TransformationScaling TransformationRotation TransformationShearing
6 min read
Composite Transformation in 2-D graphics
Prerequisite - Basic types of 2-D Transformation :   TranslationScalingRotationReflectionShearing of a 2-D objectComposite Transformation : As the name suggests itself Composition, here we combine two or more transformations into one single transformation that is equivalent to the transformations that are performed one after one over a 2-D object.
3 min read
Minimum window size containing atleast P primes in every window of given range
Given three integers X, Y and P, the task is to find the minimum window size K such that every window in the range [X, Y] of this size have atleast P prime numbers.Examples: Input: X = 2, Y = 8, P = 2 Output: 4 Explanation: In the range [2, 8], window size of 4 contains atleast 2 primes in each window. Possible Windows - {2, 3, 4, 5} - No of Primes
15+ min read
Maximum possible sum of a window in an array such that elements of same window in other array are unique
Given two arrays A and B of equal number of elements. The task is to find the maximum sum possible of a window in array B such that elements of same window in A[] are unique. Examples: Input: A = [0, 1, 2, 3, 0, 1, 4] B = [9, 8, 1, 2, 3, 4, 5] Output: sum = 20 The maximum sum possible in B[] such that all corresponding elements in A[] are unique is
10 min read
Segments in Computer Graphics
Introduction : Introduction segments are a fundamental concept in computer graphics, used to represent the basic building blocks of a graphical scene. They are commonly used in 2D graphics to represent lines or curves that connect two or more points. An introduction segment is defined by two endpoints, which are specified using their (x,y) coordina
8 min read
three90RightbarBannerImg