You are on page 1of 7

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU

Computer Graphics (CG)

Lab Report 10
Window to View Port Transformation

Submitted by
Jeshika Baniya
019BIM017

Submitted to
Mr. Ganesh Yogi
Lecturer
Department of Computer Science
St. Xavier’s College
Theory:
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.

In order to maintain the same relative placement of the point in the viewport as in the window,
we require:

Solving these impressions for the viewport position (xv, yv), we have

xv=xvmin+(xw-xwmin)sx
yv=yvmin+(yw-ywmin)sy ...........equation 2
Where scaling factors are

Equation (1) and Equation (2) can also be derived with a set of transformation that converts the
window or world coordinate area into the viewport or screen coordinate area. This conversation
is performed with the following sequence of transformations:

1. Perform a scaling transformation using a fixed point position (xw min,ywmin) that scales the
window area to the size of the viewport.
2. Translate the scaled window area to the position of the viewport. Relative proportions of
objects are maintained if the scaling factors are the same (sx=sy).

From normalized coordinates, object descriptions are mapped to the various display devices.
Algorithm
Step1:Translate window to origin 1
Tx=-Xwmin Ty=-Ywmin
Step2:Scaling of the window to match its size to the viewport
Sx=(Xymax-Xvmin)/(Xwmax-Xwmin)
Sy=(Yvmax-Yvmin)/(Ywmax-Ywmin)
Step3:Again translate viewport to its correct position on screen.
Tx=Xvmin
Ty=Yvmin
Above three steps can be represented in matrix form:
VT=T * S * T1
T = Translate window to the origin
S=Scaling of the window to viewport size
T1=Translating viewport on screen.

Viewing Transformation= T * S * T1
Program
package jeshika;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class windowviewport extends JFrame
{
static int xwmin = 100, ywmin = 100, xwmax = 300, ywmax = 250;
static int xvmin = 10, yvmin = 10, xvmax = 100, yvmax = 100;
windowviewport()
{
setSize(800, 800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.drawRect(xwmin, ywmin, 200, 150);
int x1 = 100, y1 = 150, x2 = 300, y2 = 250;
g2d.drawLine(x1, y1, x2, y2);

double sx =(double) (xvmax - xvmin) / (xwmax - xwmin);


double sy =(double) (yvmax - yvmin) / (ywmax - ywmin);
int x1vp =(int) (xvmin + (double)(sx *(x1 - xwmin)));
int y1vp =(int) (yvmin + (double)(sy * (y1 - ywmin)));
int x2vp =(int) (xvmin + (double)(sx * (x2 - xwmin)));
int y2vp =(int) (yvmin + (double)(sy *(y2 - ywmin)));
AffineTransform translate = new AffineTransform();
translate.setToTranslation(100,200);
g2d.setTransform(translate);
g2d.drawRect(xvmin, xvmax, 90, 90);
translate.setToTranslation(100,300);
g2d.setTransform(translate);
g2d.drawLine(x1vp, y1vp, x2vp, y2vp);
}
public static void main(String[] args)
{
new windowviewport();
}
}

Output

You might also like