Write a program to clip a line using Liang - Barsky line clipping algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

#define ROUND(a)    ( (int) (a+0.5))

#define TRUE  1
#define FALSE 0

int xMin, xMax, yMin, yMax;

int  test(float, float, float *, float *);
void clip(int, int, int, int);

void  main()
{
    int gdriver=DETECT, gmode;
    int x1,y1,x2,y2;

    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    cleardevice();

    printf("Enter the End-Points of the LINE...\n\n");
    printf("Enter the (x1,y1) : ");
    scanf("%d %d",&x1,&y1);
    printf("Enter the (x2,y2) : ");
    scanf("%d %d",&x2,&y2);

    printf("\n\n\nEnter the Corner-Points of the Clip-Rectangle...\n\n");
    printf("Enter the (xMin,yMin) : ");
    scanf("%d %d",&xMin,&yMin);
    printf("Enter the (xMax,yMax) : ");
    scanf("%d %d",&xMax,&yMax);

    cleardevice();
    line(x1,y1,x2,y2);
    rectangle(xMin,yMin,xMax,yMax);

    getch();
    setcolor(0);    line(x1,y1,x2,y2);
    clip(x1,y1,x2,y2);

    getch();
    closegraph();
}

 void clip(int x1, int y1, int x2, int y2)
{
    float u1=0.0, u2=1.0;
    int   dx=x2-x1, dy;

    if( test(-dx, x1-xMin, &u1, &u2) )
        if( test(dx, xMax-x1, &u1, &u2) )
        {
            dy = y2 - y1;
            if( test(-dy, y1-yMin, &u1, &u2) )
            if( test( dy, yMax-y1, &u1, &u2) )
            {
                if(u2 < 1.0)
                {
                    x2 = x1 + u2 * dx;
                    y2 = y1 + u2 * dy;
                }
                if(u1 > 0.0)
                {
                    x1 += u1 * dx;
                    y1 += u1 * dy;
                }
                setcolor(4);
                line(ROUND(x1), ROUND(y1), ROUND(x2), ROUND(y2));
            }
        }
}

int test(float p, float q, float *u1, float *u2)
{
    float r = q / p;

    if(p < 0.0)
    {
        if(r > *u2)
            return FALSE;
        else if(r > *u1)
            *u1 = r;
    }

    else if(p > 0.0)
    {
        if(r < *u1)
            return FALSE;
        else if(r < *u2)
            *u2 = r;
    }

    else if(q < 0.0)
        return FALSE;

    return TRUE;
}

Read more...

Write a program to clip a line using Cohen – Sutherland line clipping algorithm.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

#define ROUND(a)    ( (int) a+0.5 )
#define ACCEPT(a,b) (!(a|b))
#define REJECT(a,b) (a&b)
#define INSIDE(a)   (!a)

#define LEFT   0x1
#define RIGHT  0x2
#define BOTTOM 0x4
#define TOP    0x8

#define TRUE  1
#define FALSE 0

int xMin, xMax, yMin, yMax;

void clip(int, int, int, int);
char encode(int, int);

void  main()
{
   int gdriver = DETECT, gmode;
   int x1,y1,x2,y2;

   initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

   cleardevice();

   clip(x1,y1,x2,y2);

   getch();
   closegraph();
}

char encode(int x, int y)
{
   char code = 0x00;

   if(x < xMin)
         code = code | LEFT;
   if(x > xMax)
         code = code | RIGHT;
   if(y < yMin)
         code = code | BOTTOM;
   if(y > yMax)
         code = code | TOP;

   return(code);
}

void clip(int x1, int y1, int x2, int y2)
{
   char code1, code2;
   int done = FALSE, draw = FALSE;
   float m;

   while( !done )
   {
         code1 = encode(x1,y1);
         code2 = encode(x2,y2);

         if( ACCEPT(code1, code2) )
         {
               done = TRUE;
               draw = TRUE;
         }
         else if( REJECT(code1, code2) )
               done = TRUE;
         else if( INSIDE(code1) )
               swap(x1,y1,x2,y2,code1,code2);

         if( x2 != x1)
               m = (float) (y2 -y1) / (x2 - x1);
         if(code1 & LEFT)
         {
               y1 = (xMin - x1) * m;
               x1 = xMin;
         }
         else if(code1 & RIGHT)
         {
               y1 = (xMax - x1) * m;
               x1 = xMax;
         }
         else if(code1 & BOTTOM)
         {
               y1 = yMin;
               x1 = (yMin - y1) / m;
         }
         else if(code1 & TOP)
         {
               y1 = yMax;
               x1 = (yMax - y1) / m;
         }
   }
   if(draw)
         line( ROUND(x1), ROUND(y1), ROUND(x2), ROUND(y2) );
}

Read more...

Reflect an object as per user’s choice. Choices are With respect to x – axis, With respect to y – axis, With respect to co-ordinate origin and With respect to an axis at 45 degree to x – axis.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>

void Triangle(int, int, int, int, int, int);
int midx=319, midy=239;

void  main()
{
int gdriver=DETECT, gmode, c,r,x1,y1,x2,y2,x3,y3;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

  do
  {
clrscr();
cleardevice();

printf("1.X-Axis \n2.Y-Axis \n3.Co-ordinate Origin \n4.An Axis 45-Degree to X-Axis \n\n");
printf("Enter the choice : ");
scanf("%d",&c);
if(c==5)
   exit(0);
clrscr();
cleardevice();

printf("Enter the value of (x1,y1) : ");
scanf("%d %d",&x1,&y1);
printf("Enter the value of (x2,y2) : ");
scanf("%d %d",&x2,&y2);
printf("Enter the value of (x3,y3) : ");
scanf("%d %d",&x3,&y3);

cleardevice();
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
Triangle(x1,y1,x2,y2,x3,y3);

switch(c)
{
       case 1:
             getch();    setcolor(4);
             Triangle(x1,-y1,x2,-y2,x3,-y3);
             break;

       case 2:
             getch();    setcolor(4);
             Triangle(-x1,y1,-x2,y2,-x3,y3);
             break;

       case 3:
             getch();    setcolor(4);
             Triangle(-x1,-y1,-x2,-y2,-x3,-y3);
             break;

       case 4:
             getch();    setcolor(4);
             Triangle(y1,x1,y2,x2,y3,x3);
             break;
      //    case 5:  exit(0);
}
printf("Do you want to continue...");
  }while(getch() != 'n');
closegraph();
}

void Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
line(midx+x1,midy-y1,midx+x2,midy-y2);
line(midx+x2,midy-y2,midx+x3,midy-y3);
line(midx+x3,midy-y3,midx+x1,midy-y1);
}

Read more...

Write a program to scale a figure given by user. Options for the users are circle, triangle or rectangle.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void Triangle(int, int, int, int, int, int);
//int midx=319, midy=239;

void  main()
{
  int gdriver=DETECT, gmode, c,r,x1,y1,x2,y2,x3,y3;
  float S,Sx,Sy;
  initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

  clrscr();
  cleardevice();

  printf("1.Circle \n2.Triangle \n3.Rectangle \n\n");
  printf("Enter the choice : ");
  scanf("%d",&c);
  clrscr();
  cleardevice();

  switch(c)
  {
        case 1:
              printf("Enter the value of center (x1,y1) : ");
              scanf("%d %d",&x1,&y1);
              printf("Enter the Radius of the circle r : ");
              scanf("%d",&r);
              printf("Enter the scaling Factor S : ");
              scanf("%f",&S);
              circle(x1,y1,r);
              //    line(0,midy,2*midx,midy);
              //    line(midx,0,midx,2*midy);
              getch();    setcolor(4);
              circle(x1,y1,r*S);
              break;

        case 2:
              printf("Enter the value of (x1,y1) : ");
              scanf("%d %d",&x1,&y1);
              printf("Enter the value of (x2,y2) : ");
              scanf("%d %d",&x2,&y2);
              printf("Enter the value of (x3,y3) : ");
              scanf("%d %d",&x3,&y3);
              printf("Enter the scaling Factor (Sx,Sy) : ");
              scanf("%f %f",&Sx,&Sy);
              //    line(0,midy,2*midx,midy);
              //    line(midx,0,midx,2*midy);
              Triangle(x1,y1,x2,y2,x3,y3);
              getch();    setcolor(4);
              Triangle(x1*Sx,y1*Sy,x2*Sx,y2*Sy,x3*Sx,y3*Sy);
              break;

  case 3:
        printf("Enter the top-left point of the rectangle : ");
        scanf("%d %d",&x1,&y1);
  printf("\n\nEnter the bottom-right point of the rectangle : ");
              scanf("%d %d",&x2,&y2);
              printf("Enter the scaling Factor (Sx,Sy) : ");
              scanf("%f %f",&Sx,&Sy);
             //     line(0,midy,2*midx,midy);
            //      line(midx,0,midx,2*midy);
              rectangle(x1,y1,x2,y2);
              getch();    setcolor(4);
              rectangle(x1*Sx,y1*Sy,x2*Sx,y2*Sy);
              break;
  }
  getch();
  closegraph();
}

void Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
  line(x1,y1,x2,y2);
  line(x2,y2,y3,y3);
  line(x3,y3,x1,y1);
}

Read more...
Related Posts Plugin for WordPress, Blogger...

Engineering material

GTU IDP/ UDP PROJECT

GTU IDP/ UDP PROJECT

Patel free software download

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP