Software & Finance





Turbo C Graphics - setviewport function





setviewport will create a new viewport by accepting left, top, right and bottom coordinates. The 5th argument for this function would be non zero, if you want to clip the drawing beyond this viewport. clearviewport function will erase the drawing done on the view port only and not the whole screen.

cleardevice is the function used to clear the whole screen with the background color.

 

In this sample code, inital drawing happening with the default view port which would 0,0,639,479. Then a new viewport is created by clipping enabled and you can notice the clipping in the screen shot given on output section this page.

 

Back to Turbo C Graphics Index



Source Code


 

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

int InitGraphics()

{

      int grd, grm;

      int gresult;

      // Detect the graphics driver and mode

      detectgraph(&grd,&grm);

      // initialize the graphics mode with initgraph

      initgraph(&grd, &grm, "");

 

      gresult = graphresult();

      if(gresult != grOk)

      {

            printf(grapherrormsg(gresult));

            getch();

            return -1;

      }

 

      // set the background color

      setbkcolor(BLUE);

      // set the foreground color

      setcolor(WHITE);

      // draw a white color border with rectangle

      rectangle(0,0,getmaxx(),getmaxy());

      return 1;

}

 

void draw_image()

{

      int i, x, y, w;

      int left,top,right,bottom;

      int margin;

      int xradius, yradius;

      int cx, cy;

 

      margin = 100; // 100 pixel margin

      left = margin;

      top = margin;

      bottom = getmaxy() - margin;

      right = getmaxx() - margin;

      xradius = (right - left) / 2;

      yradius = (bottom - top) / 2;

      cx = (left + right) / 2;

      cy = (top + bottom) / 2;

 

      // fill an ellipse will BLUE color SOLID

      setfillstyle(SOLID_FILL, RED);

      fillellipse(cx, cy, xradius, yradius);

      outtextxy(cx - 95, cy - yradius + 20, "fillellipse 0-360 deg");

 

      // fill a sector (pie) with close dot fill

      setfillstyle(CLOSE_DOT_FILL, GREEN);

      sector(cx, cy, 0, 270, xradius - 30, yradius - 40);

      outtextxy(cx - 30, cy - 50, "sector 0-270 deg");

 

      // draw an elipitical arc and have two ends of arc joined with center point by two lines

      setcolor(YELLOW);

      moveto(cx + 5, cy + 5);

      linerel(xradius - 30, 0);

      moveto(cx + 5, cy + 5);

      linerel(0,yradius - 40);

      ellipse(cx + 5, cy + 5, 270, 0, xradius - 30, yradius - 40);

      outtextxy(cx + 20, cy + 30, "Ellipse 270-360 deg");

 

      getch();

}

 

void main()

{

      if(InitGraphics() == -1)

          return;

      draw_image();

 

      cleardevice();

      rectangle(99,99,541,381);

      setviewport(100,100,540,380, 1);

      draw_image();

      clearviewport();

 

      getch();

 

      closegraph();

}

 

 

Output