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.
- 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 is to be displayed, like Xvmin, Xvmax, Yvmin, Yvmax
- 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.
(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 scaling factor of x coordinate and sy is scaling factor of y coordinate
- 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 viewport
i.e ( Xv, Yv ).
- First of all, calculate scaling factor of x coordinate Sx and scaling factor of y coordinate Sy using above mentioned formula.
Sx = ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60 Sy = ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40
- So, now calculate the point on 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:
The point on viewport: (35, 60 )
- 2D Transformation in Computer Graphics | Set 1 (Scaling of Objects)
- Segments in Computer Graphics
- Applications of Computer Graphics
- Computer Graphics | Antialiasing
- 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 firstname.lastname@example.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.