c graphics – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:05:13 +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 c graphics – Programmerbay https://programmerbay.com 32 32 C Program to Draw a Hut https://programmerbay.com/c-program-to-draw-a-hut/ https://programmerbay.com/c-program-to-draw-a-hut/#respond Thu, 29 Feb 2024 17:23:26 +0000 https://www.programmerbay.com/?p=1886 C supports a header file named “graphics.h” which enables us to draw various figures. In this, we’ll be using line and rectangle function of that particular header file to draw a hut.

C Program to draw a hut in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t ********* HUT ********");
line(150,100,50,200);
line(150,100,350,100); 
line(150,100,300,200);
line(300,200,500,200);
line(350,100,500,200);
line(50,200,300,200);
rectangle(50,400,300,200);
rectangle(300,200,500,400);
rectangle(130,250,230,400);

getch();
closegraph();

}

Output:

Hut

]]>
https://programmerbay.com/c-program-to-draw-a-hut/feed/ 0
C program to draw arc from 135 to 270 degree using Arc() function https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/ https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/#respond Thu, 25 Aug 2022 04:44:05 +0000 https://programmerbay.com/?p=5335 Here, we will be using arc() function that is supported by graphics.h header file.

It takes 5 arguments. First two arguments are the coordinates of a circle, its successive two arguments are starting angle and end angle and last one represents the radius of the circle.

The angle value of arc() function can be ranged from 0 to 360 degree.

Syntax:

void arc(coordinateX,coordinateY,startingAngle,closingAngle,radius)

Program to draw an arc from 135 to 270 degree in C

Program:

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
int main()
{
        int gd=DETECT,gm=0;
        initgraph(&gd,&gm,"c:\\tc\\bgi");
       arc(100, 100, 135, 270, 100);
       getch();
       closegraph();
}

Output:

arc function

]]>
https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/feed/ 0
Program to Print Histogram in C https://programmerbay.com/c-program-to-create-histogram/ https://programmerbay.com/c-program-to-create-histogram/#respond Sat, 20 Aug 2022 09:55:04 +0000 https://programmerbay.com/?p=5379 The program accepts an array as input and prints a histogram. It uses srand() method for random data creation ranging from 0 to 20 elements.

We need to generate a frequency array for 100 numbers between 0-19. Further, we need to create, add data and print a corresponding histogram for same.

Program to Print Histogram in C

Program:

/******************************************************************************

    Program to print histogram from given array in C                

*******************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
int main ()
{

  int arr[100], i, barSize, p, count;

  // Variables that are being used for printing histogram 

  int firstBar = 0, secBar = 0, thirdBar = 0, fourthBar = 0, lowerRange =
    0, upperRange = 5;

  // srand for creating random values between 0 to 20 

  srand (time (0));
  for (i = 0; i < 100; i++){
      arr[i] = rand () % 20;
    }


// printing autogenerated array elements on output screen    
      printf ("Array values for the histogram :: \n");

  for (i = 0; i < 100; i++){
      printf ("%d  ", arr[i]);
    }


// printing histogram in C  by spliting bar in intervals of 5 i.e 0-5, 5-10
  for (i = 0; i < 100; i++){
      if (arr[i] < 5){
    firstBar = firstBar + 1;
  }
      else if ((arr[i] < 10) && (arr[i] > 5)){
    secBar = secBar + 1;
  }
      else if ((arr[i] < 15) && (arr[i] > 10)){
    thirdBar = thirdBar + 1;
  }
      else{
    fourthBar = fourthBar + 1;
  }
    }
    
    // Printing histogram in C
    
  printf ("\n\n*************** HISTOGRAM ***************\n\n");
  i = 1;
  barSize = firstBar;
  
  while (i <= 4){
      printf ("\n     |\n%2d-%2d|", lowerRange, upperRange);
      p = 1;
      while (p <= barSize){
    printf ("*");
    p++;
  }
  
      printf ("\n     |\n");
      if (i == 2)
  barSize = secBar;
      if (i == 3)
       barSize = thirdBar;
      if (i == 4)
       barSize = fourthBar;
         lowerRange = lowerRange + 5;
         upperRange = upperRange + 5;
         i++;
    }
  return 0;
}

 

Output:

Random data inputs are generated first:-

Array values for the histogram :: 
6  18  0  9  16  16  17  6  3  10  6  18  13  6  5  15  7  5  17  9  15  6  16  7  15  14  0  0  12  8  12  18  18  12  7  14  0  17  0  15  7  19  13  12  5  10  0  4  15  17  14  2  15  10  9  10  17  2  2  1  2  14  0  0  18  19  14  19  16  6  14  15  5  8  0  2  10  0  19  6  17  13  0  13  15  2  15  4  4  18  6  6  4  18  6  3  9  12  14  18

Plotted to Histogram:

*************** HISTOGRAM ***************


     |
 0- 5|************************
     |

     |
 5-10|************************
     |

     |
10-15|********************
     |

     |
15-20|****************
     |

Explanation:

  1. We have generated frequency data, with values ranging from 0 to 19
  2. To create a histogram, we have used intervals 0-5,5- 10, 10-15, 15-20 with 5 class size
  3. In order to create 4 bars, we have used 4 variables named, firstBar, secBar, thirdBar, and fourthBar
  4. While iterating frequency data, if the value lies between 0 to 5, count it as 1 and add it to the first bar
  5. Similarly, if it ranges from 5 to 10, then add 1 to secBar variable
  6. Lastly, we have printed the histogram on the output screen

]]>
https://programmerbay.com/c-program-to-create-histogram/feed/ 0
C Program for Translation of a Triangle https://programmerbay.com/c-program-for-translation-of-a-triangle/ https://programmerbay.com/c-program-for-translation-of-a-triangle/#respond Fri, 19 Aug 2022 14:44:00 +0000 https://www.programmerbay.com/?p=1843 triangle

A Triangle is made up of three vertices or points in which three line segments are joined together.

Here is the snippet.

Program for Translation of a Triangle in C

Program:

/* translation */
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n Please enter first coordinate of the triangle= ");
scanf("%d %d", &x,&y);
printf("\n Enter second coordinate of the trinagle = ");
scanf("%d %d",&x1,&y1);
printf("\n Enter third coordinate of the triangle = ");
scanf("%d %d",&x2,&y2);
printf("\n\t\t********** TRIANGLE before & after translation ***********");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n Now enter the translation vector = ");
scanf("%d %d",&tx,&ty);

setcolor(RED);
line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
getch();
closegraph();
}

 

 

Output:

triangle translation

]]>
https://programmerbay.com/c-program-for-translation-of-a-triangle/feed/ 0
Types Of Curves in Computer Graphics https://programmerbay.com/c-graphics-curve-and-its-types/ https://programmerbay.com/c-graphics-curve-and-its-types/#respond Wed, 10 Aug 2022 17:57:28 +0000 https://programmerbay.com/?p=5144 A curve is a collection of indefinite points where each and every point has two neighbors, except start and endpoints (as they have one).

There are three types of curves:

  1. Implicit Curve

  2. Explicit Curve

  3. Parametric Curve

Types of Curves in Computer Graphics

Implicit Curve

It can be expressed as the collection of points which are defined by an implicit equation to check whether a point on the curve or not.
The given function is referred to as implicit function
f(x,y)=0
In this, this function would give multiple values of x for a single value of y. It is also known as multivalued function

Implicit : When a soloution can not be expressed  for x in form of y 

Explicit Curve

It can be expressed as the collection of points which are plotted on using a function y = f(x).

y=f(x)

In this, the given function would give a single value of x for a single value of y. It is also known as a Single valued function

Parametric Curve

If a curve whose points are plotted using a parametric function is known as the parametric curve.

f = g1(t), g2(t),  it can be represented as (x,y)

where the value of t is in between 0 and 1
t =[0,1]


What is B-Spline Curve with equation

In B-spline curve, Local control is imposed on a curve, means a B-spline curve usually divide into segments and changing control points respective to a particular segment would only change that shape of that region only. In Bezier curve, controls are global, changing a control will lead to change the entire shape of a curve.

Each and every segment uses a unique basis function

S(t) = ∑ni=0   SiNi, p(u)         0<=t<=n-p+2

p are the points that control a segment

S is number of control points

Ni, p(t)  = (u-ki)Ni,p-1(u)/ki+p-1-k (u-ki+p)Ni+1,p-1(u)/ki+p-ki +1

k are the number of knot points

ki where i lies  ( 0<= i <= n+p )

ki = 0,  if i< p

ki = i-p +1 ,  if p<= i<= n

ki = n-p+2,  if i>n

Ni, p(u)     = 1  if  ki <= u <= ki+1

Properties of  B-spline curve

1) B-spline curve consists of n+1 control points and p order of the curve.

2) It has local control over curve that controls segments separately.

3) A degree of polynomial depends on the order of the curve which is p-1.

4) B-spline consists of n-p+2 segments.

5) Number of control points can be changed without affecting the degree of a polynomial


What is Bezier curve with equation

Bezier curve was founded by a French scientist named Pierre Bézier. This Curve is drawn by using Control points. In this, Approximate tangents act as control points which are used to generate the desired Bezier. It is a parametric curve which follows bernstein polynomial as the basis function.

B(t) =  ∑ki=0  PkBkn(t)       , Where t lies between 0 and 1, 0<=t<=1

P represents number of control points

Bk, n(t)=  nCk    uk (1-t)n-k

Tangent: It is a straight line that exactly touches curve.

Types of Bezier Curve

1)  Simple Bezier Curve : The simple line connecting endpoint.

simple bezier curve

2) Quadric Bezier Curve: Quadric curve using 3 control points

quadratic bezier curve

3) Cubic Bezier Curve: Cubic curve using 4 control points

cubic bezier curve

Bezier curve Properties:

1) A Bezier curve always depends on the number of control points that require to draw it.

2) Curve can be drawn using endpoints only.

3) The polynomial equation also depends on the number of control points Suppose, n is a control point then the degree of the polynomial equation will be n-1.

4) Curve passes through initial and terminating control points.

5) Closed Bezier curve can be generated by making the first and last control points the same.

]]>
https://programmerbay.com/c-graphics-curve-and-its-types/feed/ 0
C Program to Perform Shearing on Triangle https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/ https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/#respond Mon, 01 Aug 2022 11:55:53 +0000 https://www.programmerbay.com/?p=1928 In this article, we’ll be implementing shearing transformation program with output. In Transformation, shearing is the process of slanting the shape of an object.

C Program to perform shearing of a triangle along the x axis with output

Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter shearing factor x = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);

setcolor(RED);
x=x+ y*shear_f;
x1=x1+ y1*shear_f;
x2=x2+ y2*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

 

Output:

triangle shearing x

C Program to perform shearing of a triangle along the y axis with output

Program:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,shear_f;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter first coordinate = ");
scanf("%d %d",&x,&y);
printf("\n please enter second coordinate = ");
scanf("%d %d",&x1,&y1);
printf("\n please enter third coordinate = ");
scanf("%d %d",&x2,&y2);
printf("\n please enter shearing factor y = ");
scanf("%d",&shear_f);
cleardevice();
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);

setcolor(RED);
y=y+ x*shear_f;
y1=y1+ x1*shear_f;
y2=y2+ x2*shear_f;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

Output:

triangle shearing y

]]>
https://programmerbay.com/c-program-to-perform-shearing-on-a-triangle/feed/ 0
C Program to Draw a Circle using Midpoint Circle Drawing Algorithm https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/ https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/#respond Fri, 29 Jul 2022 07:58:45 +0000 https://www.programmerbay.com/?p=2180 An algorithm that is used to find points required for plotting and converting a circle over display. Midpoint circle drawing algorithm snippet provided in this Article. If you are interested to see the algorithm ( Go here) .

C Program to draw a circle using the midpoint circle drawing algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
}
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

Output:

circle drawing algorithm

]]>
https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/feed/ 0
C Program to Rotate a Line https://programmerbay.com/c-program-to-rotate-a-line/ https://programmerbay.com/c-program-to-rotate-a-line/#respond Wed, 27 Jul 2022 12:48:40 +0000 https://www.programmerbay.com/?p=1863 Rotation can be defined as moving an object in a circular path at a given angle theta.

If there is a positive angle, it would rotate in anticlockwise whereas if it appears to a negative angle, the object would rotate in clockwise. Here’s is C program to rotate a line in computer Graphics. The below program is rotation program in C.

Program to show rotation of a line in Computer Graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int pivot_x,pivot_y,x,y;
double degree,radian;
int rotated_point_x,rotated_point_y;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
cleardevice();
printf("\t\t*********** ROTATION *********** \n");
printf("\n Enter an initial coordinates of the line = ");
scanf("%d %d",&pivot_x,&pivot_y);
printf("\n Enter a final coordinates of the line = ");
scanf("%d %d",&x,&y);
line(pivot_x,pivot_y,x,y);
printf("\n\n Now, Enter a degree = ");
scanf("%lf",&degree);
radian=degree*0.01745;
rotated_point_x=(int)(pivot_x +((x-pivot_x)*cos(radian)-(y-pivot_y)*sin(radian)));
rotated_point_y=(int)(pivot_y +((x-pivot_x)*sin(radian)+(y-pivot_y)*cos(radian)));
setcolor(RED);
line(pivot_x,pivot_y,rotated_point_x,rotated_point_y);
getch();
closegraph();
}

 

Output:

Rotation by 30

rotation 30

Rotation by 90

rotation 90

]]>
https://programmerbay.com/c-program-to-rotate-a-line/feed/ 0
C Program for Clipping a line using Cohen Sutherland Algorithm https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/ https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/#respond Mon, 25 Jul 2022 15:31:03 +0000 https://www.programmerbay.com/?p=2157 In this article, we’ll be clipping a line in C using Cohen Sutherland algorithm.

Clipping is a process of removing a portion of a line or an object that falls outside of the specified region. Cohen Sutherland is a line clipping algorithm which is used to clip out the extra portion of the line from view plane.

Must Read: What is Cohen Sutherland line clipping algorithm?

C program for line clipping using Cohen Sutherland algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax)  {
rcode_begin[0]=1;	   // Top
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1;           // Bottom
flag=1;
}
if(x>W_xmax)  {
rcode_begin[2]=1;           // Right
flag=1;
}
if(x<W_xmin)   {
rcode_begin[3]=1;           //Left
flag=1;
}

//end point of Line
if(y1>W_ymax){
rcode_end[0]=1;           // Top
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1;           // Bottom
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1;           // Right
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1;           //Left
flag=1;
 }
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
 flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1)   //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;

}
if(rcode_begin[2]==1 && rcode_begin[3]==0)       // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;

}
if(rcode_begin[0]==1 && rcode_begin[1]==0)      // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

}
if(rcode_begin[0]==0 && rcode_begin[1]==1)     // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;

}
// end points
if(rcode_end[2]==0 && rcode_end[3]==1)   //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;

}
if(rcode_end[2]==1 && rcode_end[3]==0)       // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

}
if(rcode_end[0]==1 && rcode_end[1]==0)      // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;

}
if(rcode_end[0]==0 && rcode_end[1]==1)     // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;

}
}
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}

 

 

Output:

Getting input from user:

cohen setherland

Before Clipping the line

cohen setherland before clipping

After Clipping the line:

cohen setherland after clipping

]]>
https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/feed/ 0
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