# Window to Viewport Transformation in Computer Graphics with Implementation

**Window to Viewport Transformation **is the process of transforming a 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 X_{wmin}, X_{wmax}, Y_{wmin}, Y_{wmax}**Device Coordinate –**It is the screen coordinate where the objects is to be displayed, like X_{vmin}, X_{vmax}, Y_{vmin}, Y_{vmax}**Window –**It is the area on world coordinate selected for display.**ViewPort –**It is the area on 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.

(x_{w}, y_{w}): A point on Window (x_{v}, y_{v}): Corresponding point on Viewport

- we have to calculate the point
**(x**_{v}, y_{v}) - 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, s
_{x}is scaling factor of x coordinate and s_{y}is scaling factor of y coordinate

**Example:**

Lets assume,

- for window,
**X**._{wmin}= 20, X_{wmax}= 80, Y_{wmin}= 40, Y_{wmax}= 80 - for viewport,
**X**._{vmin}= 30, X_{vmax}= 60, Y_{vmin}= 40, Y_{vmax}= 60 - Now a point ( X
_{w}, Y_{w}) be ( 30, 80 ) on the window. We have to calculate that point on viewport

i.e**( X**._{v}, Y_{v}) - First of all, calculate scaling factor of x coordinate S
_{x}and scaling factor of y coordinate S_{y}using above mentioned formula.S

_{x}= ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60 S_{y}= ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40 - So, now calculate the point on viewport ( X
_{v}, Y_{v}).X

_{v}= 30 + ( 30 - 20 ) * ( 30 / 60 ) = 35 Y_{v}= 40 + ( 80 - 40 ) * ( 20 / 40 ) = 60 - So, the point on window ( X
_{w}, Y_{w}) = ( 30, 80 ) will be**( X**on viewport._{v}, Y_{v}) = ( 35, 60 )

Here is the implementation of the above approach:

**Implementation:**

`// 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; ` ` ` ` ` `// calculatng 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); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The point on viewport: (35, 60 )

## Recommended Posts:

- 2D Transformation in Computer Graphics | Set 1 (Scaling of Objects)
- Segments in Computer Graphics
- Computer Graphics | Antialiasing
- Applications of Computer Graphics
- Computer Graphics | The RGB color model
- Display Processor in Computer Graphics
- Translation of objects in computer graphics
- DDA Line generation Algorithm in Computer Graphics
- Draw a moving car using computer graphics programming in C
- Point Clipping Algorithm in Computer Graphics
- Refresh type output devices in Computer Graphics
- Computer Graphics | Cathode ray tube (video display device)
- 2D Transformation | Rotation of objects
- Piece-wise Linear Transformation
- Fast Fourier Transformation for poynomial multiplication

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.