Tuesday, 10 June 2014

Lagrange Interpolation Curve Drawing



/* This routine draws Lagrange interpolation curves with 4 points.
/* It accepts four input points and finds out some other points
/* on the curve at specified parameter values.              
/* Output is presented in text mode only.                    
/******************************************************************/


#include<stdio.h>
#include<stdlib.h>

typedef struct {float x, y;} point;
point SmpPt[4], NewPt[10];

main()
{
 int i, n;
 float j0, j1, j2, j3;
 float u, v[10];

 for(i=0;i<4;i++)
   {
    printf("\nGive sample point[%d]:",i);
    scanf("%f %f", &SmpPt[i].x, &SmpPt[i].y);
   }
 printf("\nHow many new points ? ");
 scanf("%d", &n);
                                                                                                        
 for(i=0;i<n;i++)
   {
    printf("\nGive u value[%d](1.0 - 3.0): ",i);
    scanf("%f", &v[i]);
   }
                                                                                                        
 printf("u \t j0\t j1\t j2\t j3\t x\t y\n");

 for(i=0;i<n;i++)
 {
 u=v[i];
 j0=(u-1.0)*(u-2.0)*(u-3.0)/(-6.0);
 j1=u*(u-2.0)*(u-3.0)/2.0;
 j2=u*(u-1.0)*(u-3.0)/(-2.0);
 j3=u*(u-1.0)*(u-2.0)/6.0;

 NewPt[i].x = SmpPt[0].x*j0 + SmpPt[1].x*j1 +SmpPt[2].x*j2 +SmpPt[3].x*j3;
 NewPt[i].y = SmpPt[0].y*j0 + SmpPt[1].y*j1 +SmpPt[2].y*j2 +SmpPt[3].y*j3;
                                                                                                        
 printf("%4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f\n",
                  u,j0,j1,j2,j3, NewPt[i].x,NewPt[i].y);

 }
}


Gourad shading algorithm


/* This routine performs shading of a triangular surface using
/* Gourad algorithm. Here, vertex co-ordinates, vertex intensities
/* are needed as inputs. Number of vertices is 3 by default.  
/* Display window is set to wsize by wsize pixels in size.  
/******************************************************************/

#include<stdio.h>
#include<math.h>                                                                                         
#define wsize 300
                                                                      
typedef struct {int x, y, I;} point;
point vertex[10];
int buffer[wsize][wsize], left[wsize], right[wsize];
int ymin = 999, ymax = -999;
FILE *fp;

float compute_distance(int, int, int, int);

main()
{
 int i, j, n;

 fp = fopen("shadeT.txt", "w");
 printf("\nHow many vertices in polygon: ");
 scanf("%d", &n);
 for(i = 0; i < n; i++)
   {
    printf("\nGive vertex[%d]: ",i);
    scanf("%d %d", &vertex[i].x, &vertex[i].y);
   }

 for(i = 0; i < n; i++)
   {
    if(vertex[i].y < ymin)
       ymin = vertex[i].y;
    if(vertex[i].y > ymax)
       ymax = vertex[i].y;
   }
 printf("\nymin = %d, ymax = %d", ymin, ymax);

 for(i = 0; i < n; i++)
   {
    printf("\nGive intensity at vertex[%d]: ",i);
    scanf("%d", &vertex[i].I);
   }
                                                                                         
 for(i = 0; i < wsize; i++)
   for(j = 0; j < wsize; j++)
      buffer[i][j] = 10;
                                                                                         
 drawpolygon(n);

 printf("\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }
 getboundaries();                                                                              
 //uniformfill();          
 //printf("\n\n\n");
 //for(i = 0; i < wsize; i++)
 //  {
 //   for(j = 0; j < wsize; j++)
 //     fprintf(fp2, "%d ",buffer[i][j]);
 //   fprintf(fp2, "\n");
 //  }

 gouradfill();
                                                                                         
 printf("\n\n\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      fprintf(fp, "%d ",buffer[i][j]);
    fprintf(fp, "\n");
   }
   
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }


 fclose(fp);
}

getboundaries()
{
 int i, j, rmark;
 for(j = 0; j < wsize; j++)
   {
    i = 0;
    while((buffer[i][j] == 10) && (i < wsize)) i++;
    if(buffer[i][j] == 11)
       {left[j] = i; while(buffer[i][j] == 11) i++; rmark = i-1;}
    while((buffer[i][j] == 10) && (i < wsize))
       {i++; if(buffer[i][j] == 11) right[j] = i;else right[j] = rmark;}
   }

 for(i = 0; i < 3; i++)
   buffer[vertex[i].x][vertex[i].y] = vertex[i].I;
}

uniformfill()
{
 int i, j;

 for(i = ymin; i < ymax; i++)
   for(j = left[i]; j <= right[i]; j++)
     buffer[j][i] = 50;
}

gouradfill()
{
 int i, j, k;

 for(i = ymin; i < ymax; i++)
  {
   buffer[left[i]][i] = (int)(-(compute_distance(vertex[0].x, vertex[0].y, left[i], i)/compute_distance(vertex[0].x, vertex[0].y, vertex[1].x, vertex[1].y)*(vertex[0].I - vertex[1].I)) + vertex[0].I);

   buffer[right[i]][i] = (int)(-(compute_distance(vertex[0].x, vertex[0].y, right[i], i)/compute_distance(vertex[0].x, vertex[0].y, vertex[2].x, vertex[2].y)*(vertex[0].I - vertex[2].I)) + vertex[0].I);

   //printf("\n\ni = %d) l=%d, r=%d, bl=%d, br=%d :: ", i, left[i], right[i], buffer[left[i]][i], buffer[right[i]][i]);
   for(j = left[i]; j < right[i]; j++)
    {
     buffer[j][i] = (int)(-((float)(j-left[i])/(float)(right[i]-left[i]))* (buffer[left[i]][i] - buffer[right[i]][i]) + buffer[left[i]][i]);
     //printf("%d ", buffer[j][i]);
    }
  }
}


float compute_distance(int x1, int y1, int x2, int y2)
{
 return(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));  
}

drawpolygon(int k)
{
 int i;
 for(i = 0;i < k-1;i++)
  {
   BresLine(i, vertex[i].x, vertex[i].y, vertex[i+1].x, vertex[i+1].y);
  }
 BresLine(k-1, vertex[k-1].x, vertex[k-1].y, vertex[0].x, vertex[0].y);
}


BresLine(int j, int x1, int y1, int x2, int y2)
{
 int x, y, dx, dy, p, temp, k;
 float slope;
                                                                                         
                                                                                          
 dx = abs(x2 - x1); dy = abs(y2 - y1);

 if(dx == 0) verline(j, x1, y1, x2, y2);
 else if(dy == 0) horline(j, x1, y1, x2, y2);
 else if(dx == dy) digline(j, x1, y1, x2, y2);
 else
 {
  x = x1; y = y1;
  slope = (float)dy/(float)dx;
  if(slope > 1)
    {
     swap(&x1, &y1); swap(&x2, &y2);
     temp = dx; dx = dy; dy = temp;
     temp = x; x = y; y = temp;
    }
  p = 2 * dy - dx;
  if(slope > 1) buffer[y][x] = 11; else buffer[x][y] = 11;
                                                                                         
                                                                                         
  for(k=0; k < dx; k++)
    {
     if(p < 0)
       {
        if(x < x2) x++; else x--;
        p = p + 2 * dy;
       }
     else
       {
        if(y < y2) y++; else y--;
        if(x < x2) x++; else x--;
        p = p + 2 * (dy - dx);
       }
     if(slope > 1) buffer[y][x] = 11; else buffer[x][y] = 11;  
    }
  }
}

swap(int *a,Gourad shading algorithm  int *b)
{
 int tmp;
 tmp = *a; *a = *b; *b = tmp;
}
                                                                                         
horline(int l, int x1, int y1, int x2, int y2)
{
 int i,j;
 if(x1 < x2)
  for(i = x1; i <= x2; i++)
     buffer[i][y1] = 11;
 else
  for(i = x2; i <= x1; i++)
     buffer[i][y1] = 11;
}
                                                                                         
verline(int l, int x1, int y1, int x2, int y2)
{
 int i,j;
 if(y1 < y2)
  for(j = y1; j <= y2; j++)
     buffer[x1][j] = 11;
 else
  for(j = y2; j <= y1; j++)
     buffer[x1][j] = 11;
}
                                                                                         
                                                                                         
digline(int l, int x1, int y1, int x2, int y2)
{
 int i,j;
 if(x1 < x2)
  for(i = x1; i <= x2; i++)
    buffer[i][i] = 11;
 else
  for(i = x2; i <= x1; i++)
    buffer[i][i] = 11;
}

/***************************************************************
This Program is may be tested with the following example inputs.
Example 1:
No. of vertices: 3
Vertices: 15 28; 2 2; 28 2
Vertex intensities: 9 5 1

Example 2:
No. of vertices: 3
Vertices: 5 15; 28 2; 25 25
Vertex intensities: 9 5 1
***************************************************************/

Cyrus Beck Clipping Algorithm



/* This routine performs 2D clipping. It is assumed that user  
/* will supply window and line co-ordinates.                
/* For the window only two co-ordinates are needed: left-bottom
/* and the right-top corner co-ordinates respectively.        

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

#include<stdio.h>

#define k 4

struct point {float x; float y;};
struct point EndPt[2], WinPt[2], NewPt[2];
struct point n[k], D, w[k];

float dn, wn;

main()
{
 int i;
 float t, tmin = 0.0, tmax = 1.0;

 for(i=0;i<2;i++)
   {
    printf("\nGive input line end point[%d]:",i);
    scanf("%f %f", &EndPt[i].x, &EndPt[i].y);
   }
 for(i=0;i<2;i++)
   {
    printf("\nGive Window corner point[%d]:",i);
    scanf("%f %f", &WinPt[i].x, &WinPt[i].y);
   }

 D.x = EndPt[1].x - EndPt[0].x;
 D.y = EndPt[1].y - EndPt[0].y;

 n[0].x = 1; n[0].y = 0;  // normal to left boundary
 n[1].x = -1; n[1].y = 0; // normal to right boundary
 n[2].x = 0; n[2].y = 1;  // normal to bottom boundary
 n[3].x = 0; n[3].y = -1; // normal to top boundary

 w[0].x = EndPt[0].x - WinPt[0].x;
 w[0].y = EndPt[0].y - WinPt[0].y;

 w[2].x = EndPt[0].x - WinPt[0].x;
 w[2].y = EndPt[0].y - WinPt[0].y;

 w[1].x = EndPt[0].x - WinPt[1].x;
 w[1].y = EndPt[0].y - WinPt[1].y;

 w[3].x = EndPt[0].x - WinPt[1].x;
 w[3].y = EndPt[0].y - WinPt[1].y;

 for(i=0;i<k;i++)
 {
  dn = D.x * n[i].x + D.y * n[i].y; // dot product D dot n
  wn = w[i].x * n[i].x + w[i].y * n[i].y; // dot product w dot n

  if(dn != 0.0)
   {
    t=-wn/dn;
    if (dn > 0.0)
       {
        if(t <= 1.0)
          if (t>tmin) tmin=t;
       }
    else if (dn < 0.0)
       {  
        if(t >= 0.0)
          if(t<tmax) tmax=t;
       }
   }
  printf("%d) %4.2f %4.2f   %4.2f   %4.2f %4.2f\n", i, wn, dn, t, tmin, tmax);
 }
 if(tmax>tmin)
    { 
     NewPt[1].x = EndPt[0].x + tmax * D.x;
     NewPt[1].y = EndPt[0].y + tmax * D.y;
     NewPt[0].x = EndPt[0].x + tmin * D.x;
     NewPt[0].y = EndPt[0].y + tmin * D.y;
    }
   //line(round(NewPt[0].x),round(NewPt[0].y),round(NewPt[1].x),round(NewPt[1].y));
   printf("\n%4.2f %4.2f, %4.2f %4.2f\n", NewPt[0].x, NewPt[0].y, NewPt[1].x, NewPt[1].y);
}

/* Instructions are used for text mode outputs only. Students may convert it into a graphics mode program.                    
/* Here, a regular-rectangular window is used only. Students are advised to write a general program.                      
/*******************************************************************/

C-Program for Bresenham’s Line algorithm

                 (for lines with |slope|<1).                                                 

This program performs scan conversion of a straight line between two points supplied as inputs. Although the main program calls line drawing function with an example point pairs, one may change it for arbitrary inputs. This  is a text  mode program for the Bresenham’s line drawing algorithm (scan conversion algorithm). Here output co-ordinate locations will be printed only.

                                                               
                                                                                                                                                          
#include<stdio.h>
                                                                                            
#define wsize 50
int buffer[wsize][wsize];
                                                                               
main()
{
 int i, j;
                                                                                            
 for(i = 0; i < wsize; i++)
   for(j = 0; j < wsize; j++)
      buffer[i][j] = 1;
                                                                                            
 BresLine(39, 25, 4, 5);
 printf("\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }

}
                                                                               
BresLine(int x1, int y1, int x2, int y2)
{
 int x, y, dx, dy, p, temp, k;

 dx = abs(x2-x1); dy = abs(y2-y1);
 x = x1; y = y1;
 p = 2 * dy - dx;
 printf("x = %d,  y = %d\n", x, y);
 buffer[x][y]=0;
 for(k=0; k < dx; k++)
   {
    if(p < 0)
      {
       if(x < x2) x++; else x--;
       p = p + 2 * dy;
      }
    else
      {
       if(y < y2) y++; else y--;
       if(x < x2) x++; else x--;
       p = p + 2 * (dy - dx);
      }
    printf("x = %d,  y = %d\n", x, y);
    buffer[x][y]=0;
   }
}

    
     

Wednesday, 4 June 2014

Bresenham Line drawing program (General) in text mode


                                                                            
#include<stdio.h>
                                                                               
main()
{
 BresLine(0, 9, 14, 1);
}
                                                                               
BresLine(int x1, int y1, int x2, int y2)
{
 int x, y, dx, dy, dx1, dy1, p, temp, k;
 float slope;
 dx1 = x2 - x1; dy1 = y2 - y1;
 dx = abs(dx1); dy = abs(dy1);
 if(dx == 0) verline(x1, y1, x2, y2);
 else if(dy == 0) horline(x1, y1, x2, y2);
 else if(dx1 == dy1) diagline(x1, y1, x2, y2);
 else
 {

 x = x1; y = y1; 
 slope = (float)dy/dx;
 if(slope > 1)
   {
    swap(&x1, &y1); swap(&x2, &y2);
    temp = dx; dx = dy; dy = temp;
    temp = x; x = y; y = temp;
   }

 p = 2 * dy - dx;
 if(slope > 1) printf("k = 0, x = %d, y = %d\n", y, x);
 else printf("k = 0, x = %d, y = %d\n", x, y);
 for(k=0; k < dx; k++)
   {
    if(p < 0)
      {
       if(x < x2) x++; else x--;
       p = p + 2 * dy;
      }
    else
      {
       if(y < y2) y++; else y--;
       if(x < x2) x++; else x--;
       p = p + 2 * (dy - dx);
      }
     if(slope > 1) printf("k = %d, x = %d, y = %d\n",k, y, x);
     else printf("k = 0, x = %d, y = %d\n", x, y);
   }
 }
}

swap(int *a, int *b)
{
 int tmp;
 tmp = *a; *a = *b; *b = tmp;
}     
    
horline(int x1, int y1, int x2, int y2)
{
 int i,j;
 if(x1 < x2)
  for(i = x1; i <= x2; i++)
     printf("x = %d, y = %d\n",i, y1);
 else
  for(i = x2; i <= x1; i++)
     printf("x = %d, y = %d\n",i, y1);
}
                                                                                       
verline(int x1, int y1, int x2, int y2)
{
 int i,j;
 if(y1 < y2)
  for(j = y1; j <= y2; j++)
     printf("x = %d, y = %d\n",x1, j);
 else
  for(j = y2; j <= y1; j++)
     printf("x = %d, y = %d\n",x1, j);
}
                                                                                       
diagline(int x1, int y1, int x2, int y2)
{
 int i,j;
 if(x1 < x2)
  for(i = x1; i <= x2; i++)
    printf("x = %d, y = %d\n",i, i);
 else
  for(i = x2; i <= x1; i++)
    printf("x = %d, y = %d\n",i, i);
}

Bresenham's Circle algorithm in text mode



#include<stdio.h>

#define wsize 50
int buffer[wsize][wsize];

main()
{
 int i, j;
                                                                                               
 for(i = 0; i < wsize; i++)
   for(j = 0; j < wsize; j++)
      buffer[i][j] = 1;

 MidpointCircle(25, 25, 20);
                                                                                                
 printf("\n");
 for(i = 0; i < wsize; i++)
   {
    for(j = 0; j < wsize; j++)
      printf("%d ",buffer[i][j]);
    printf("\n");
   }

}

MidpointCircle( int cen_x, int cen_y, int rad)
{
 int x, y, d;

 x = 0; y = rad; d = 3 - 2 * rad;
 CirclePoints(cen_x, cen_y, x, y);
 while(x < y)
  {
   if (d < 0) d = d + 4 * x + 6;
   else
    {
     d = d + 4 * (x - y) + 10;
     y--;
    }
   x++;
   CirclePoints(cen_x, cen_y, x, y);
  }
}

CirclePoints(int Center_x, int Center_y, int x, int y)
{

 buffer[Center_x + x][Center_y + y] = 0;
 buffer[Center_x - x][Center_y + y] = 0;
 buffer[Center_x + x][Center_y - y] = 0;
 buffer[Center_x - x][Center_y - y] = 0;
 buffer[Center_x + y][Center_y + x] = 0;
 buffer[Center_x - y][Center_y + x] = 0;
 buffer[Center_x + y][Center_y - x] = 0;
 buffer[Center_x - y][Center_y - x] = 0;

}

Cubic Bezier Curve Drawing Algorithm

                         
/* This routine draws cubic Bezier curves with four points. It accepts four input points and finds out some other points on the curve at specified parameter values.              
/* Output is presented in text mode only.                    
/******************************************************************/


#include<stdio.h>
#include<stdlib.h>

typedef struct {float x, y;} point;
point SmpPt[4], NewPt[10];

main()
{
 int i, n;
 float j0, j1, j2, j3;
 float v[10], u;

 for(i=0;i<4;i++)
   {
    printf("\nGive sample point[%d]:",i);
    scanf("%f %f", &SmpPt[i].x, &SmpPt[i].y);
   }
 printf("\nHow many new points? ");
 scanf("%d", &n);

 for(i=0;i<n;i++)
   {
    printf("\nGive u value[%d](0.0 - 1.0): ",i);
    scanf("%f", &v[i]);
   }

 printf("u \t j0\t j1\t j2\t j3\t x\t y\n");
 for(i=0;i<n;i++)
 {
  u=v[i];
  j0=(1.0-u)*(1.0-u)*(1.0-u);
  j1=3.0*u*(1.0-u)*(1.0-u);
  j2=3.0*u*u*(1.0-u);
  j3=u*u*u;

  NewPt[i].x = SmpPt[0].x*j0 + SmpPt[1].x*j1 +SmpPt[2].x*j2 +SmpPt[3].x*j3;
  NewPt[i].y = SmpPt[0].y*j0 + SmpPt[1].y*j1 +SmpPt[2].y*j2 +SmpPt[3].y*j3;

  printf("%4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f\n",
                  u,j0,j1,j2,j3, NewPt[i].x,NewPt[i].y); 
 }
}