Window to Viewport Transformation – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:11:05 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Window to Viewport Transformation – Programmerbay https://programmerbay.com 32 32 C Program to implement Window to Viewport Transformation https://programmerbay.com/program-to-implement-window-to-viewport-transformation/ https://programmerbay.com/program-to-implement-window-to-viewport-transformation/#respond Mon, 25 Jul 2022 07:45:10 +0000 https://www.programmerbay.com/?p=2148 In this article, we’ll be discussing meaning of window, viewport, window to viewport mapping and  implementing the concept in form of code.

Window

It is a world coordinate. It is a region which is used for displaying an object.

Viewport

A viewport is considered as a device coordinate after it got normalized. It is a part of a screen in which object requires to display. Basically, world coordinate gets mapped into device coordinate. So that an object gets fitted in respective to a viewport.

What is Window to Viewport Transformation ?

It is the process of transforming world coordinate in respective to device coordinate. Usually, the size of a mapped object in the viewport is smaller than a window, but it can be possible to have a greater size than the Window.

window1
window

C program for window to viewport transformation

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main ()
{
  int W_xmax, W_ymax, W_xmin, W_ymin;
  int V_xmax, V_ymax, V_xmin, V_ymin;
  float sx, sy;
  int x, x1, x2, y, y1, y2;
  int gr = DETECT, gm;
  initgraph (&gr, &gm, "C:\\TURBOC3\\BGI");
  printf ("\n****** Window to Viewport ***********\n");
  printf ("Enter the coordinates for triangle \n x and y = ");
  scanf ("%d %d", &x, &y);
  printf ("\n x1 and y1 = ");
  scanf ("%d %d", &x1, &y1);
  printf ("\n x2 and y2 = ");
  scanf ("%d %d", &x2, &y2);
  printf ("Please enter Window coordinates \n First enter XMax, YMax =");
  scanf ("%d %d", &W_xmax, &W_ymax);
  printf ("\n Now, enter XMin, YMin =");
  scanf ("%d %d", &W_xmin, &W_ymin);
  cleardevice ();
  delay (50);			
  //Window 
  rectangle (W_xmin, W_ymin, W_xmax, W_ymax);
  outtextxy (W_xmin, W_ymin - 10, "Window");	
  //drawing a triangle 
  line (x, y, x1, y1);
  line (x1, y1, x2, y2);
  line (x2, y2, x, y);	
  // viewport 
  V_xmin = 300;
  V_ymin = 30;
  V_xmax = 550;
  V_ymax = 350;
  rectangle (V_xmin, V_ymin, V_xmax, V_ymax);
  outtextxy (V_xmin, V_ymin - 10, "Viewport");	
  // calculatng Sx and Sy 
  sx = (float) (V_xmax - V_xmin) / (W_xmax - W_xmin);
  sy = (float) (V_ymax - V_ymin) / (W_ymax - W_ymin);
  x = V_xmin + (float) ((x - W_xmin) * sx);
  x1 = V_xmin + (float) ((x1 - W_xmin) * sx);
  x2 = V_xmin + (float) ((x2 - W_xmin) * sx);
  y = V_ymin + (float) ((y - W_ymin) * sy);
  y1 = V_ymin + (float) ((y1 - W_ymin) * sy);
  y2 = V_ymin + (float) ((y2 - W_ymin) * sy);
  // drawing triangle 
  line (x, y, x1, y1);
  line (x1, y1, x2, y2);
  line (x2, y2, x, y);
  getch ();
  closegraph ();
}

  Output: window to viewport window to viewport output 1

]]>
https://programmerbay.com/program-to-implement-window-to-viewport-transformation/feed/ 0