Window to Viewport Transformation in Computer Graphics with Implementation
Last Updated :
08 Aug, 2022
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.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read