Rotate a NxN 2D matrix by 90 degrees – C Program Source Code


The below program does the following:

  1. Creates a 2D matrix whose size can be input through Command Line arguments.
  2. Prints the 2D Matrix.
  3. Rotates the matrix by right angle (90 degrees) clockwise.

Source Code:

#include<stdio.h>
#include<stdlib.h>
#define DEFN 6

int** CreateMatrix(int**, int N);
void PrintMatrix(int**, int N);
int** RotateMatrix(int**,int N);

int main(int argc, char *argv[])
{

 int **matrix = NULL;
 int N;
 if(argc==2)
 N = atoi(argv[1]);
 else
 N = DEFN;
 matrix = CreateMatrix(matrix, N);
 printf("\nInitialMatrix:\n");
 PrintMatrix(matrix,N);
 printf("\n");
 matrix = RotateMatrix(matrix,N);
 printf("\nRotated Matrix:\n");
 PrintMatrix(matrix,N);

 char c = getchar();

 return(0);
}
int** CreateMatrix(int **matrix, int N)
{
 int i,j;
 matrix = (int**) calloc(N,sizeof(int*));
 for(i=0; i<N; i++)
 {
 *(matrix+i) = (int*) calloc(N, sizeof(int));
 for(j=0; j<N; j++)
 *(*(matrix+i)+j) = rand();
 }
 return(matrix);
}
void PrintMatrix(int **matrix, int N)
{
 int i,j;
 for(i=0; i<N; i++)
 {
 for(j=0; j<N; j++)
 printf("%6d ", *(*(matrix+i)+j));
 printf("\n");
 }
} 

int** RotateMatrix(int **matrix, int N)
{
 int i,temp,layer;

 for(layer=0; layer<(N/2); layer++)
 for(i=layer; i<N-layer-1; i++)
 {

 temp = *(*(matrix+N-i-1)+layer);
 *(*(matrix+N-i-1)+layer) = *(*(matrix+N-layer-1)+N-i-1);
 *(*(matrix+N-layer-1)+N-i-1) = *(*(matrix+i)+N-layer-1);
 *(*(matrix+i)+N-layer-1) = *(*(matrix+layer)+i);
 *(*(matrix+layer)+i) = temp;
 }
 return(matrix);
}

OUTPUT:

InitialMatrix:
 41 18467 6334 26500 19169 15724
11478 29358 26962 24464 5705 28145
23281 16827 9961 491 2995 11942
4827 5436 32391 14604 3902 153
292 12382 17421 18716 19718 19895
5447 21726 14771 11538 1869 19912
Rotated Matrix:
 5447 292 4827 23281 11478 41
21726 12382 5436 16827 29358 18467
14771 17421 32391 9961 26962 6334
11538 18716 14604 491 24464 26500
1869 19718 3902 2995 5705 19169
19912 19895 153 11942 28145 15724

RotateMatrix

Reverse Single Linked List – C Program Source Code


The below program performs the following:

  1. Takes input of the size of the Linked List as Command Line argument.
  2. Creates a Singly Linked List of input size (default: 20 members) filled with random numbers.
  3. Prints the members of the Linked List.
  4. Reverses the Linked List and prints it again.

Source Code:

#include<stdio.h>
#define NELEMENTS 20
struct node
{
 int value;
 struct node *next;
};
struct node* CreateLinkedList(int, struct node*);
void printList(struct node*);
struct node* Reverse(struct node*);
int main(int argc, char *argv[])
{
 struct node *head;
 int NofElements;
 if(argc == 2)
 NofElements = atoi(argv[1]);
 else
 NofElements = NELEMENTS;
 head = CreateLinkedList(NofElements, head);
 printList(head);
 head = Reverse(head);
 printList(head);

 char c;
 c = getchar();
 return(0);
}
struct node* CreateLinkedList(int NElements, struct node *head)
{ 
 struct node *temp = NULL;
 temp = (struct node*) malloc(sizeof(struct node));
 head = temp;

 for( ; NElements>0; NElements--, temp=temp->next)
 {
 temp->value = rand();
 temp->next = (struct node*) malloc(sizeof(struct node));

 }
 temp->value = '';
 temp->next = NULL;

 return(head);
}
void printList(struct node *head)
{
 struct node *temp;

 for(temp=head; (temp->value)!=''; temp=temp->next)
 printf("%8d", temp->value);
printf("\n");
}
struct node* Reverse(struct node *head)
{
 struct node *temp_prev, *temp, *temp_post;

 for(temp_prev=head,temp=head->next; (temp_prev->next)!=NULL && (temp->next)!= NULL; )
 {
 temp_post = temp->next;
 temp->next = temp_prev;
 temp_prev = temp;
 temp = temp_post;
 }
 head->next = temp;
 head = temp_prev;

 return(head);
}

Polygon Filling (Seedfill) using OpenGL – Program Source Code


#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int x1,Y1,x2,y2;
void seedfill(int,int,int,int);
void square(void);
void Axes(void);
void display(void)
 {
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 glBegin(GL_POINTS);
 square();
 glEnd();
 glFlush();
 }
void square(void)
{
 seedfill(x1 , Y1 , 0 , 0);
 Axes();
} 
void seedfill(int x , int y , int flagx , int flagy)
{

 if(x<x2 && y<y2)
 {
 if(flagx==0 && flagy==0)
 {
 seedfill(x+1,y,1,0);
 seedfill(x,y+1,0,1);
 }
 if(flagx==1 && flagy ==0)
 {
 seedfill(x+1,y,1,0);
 seedfill(x,y+1,0,1);

 }
 if(flagx==0 && flagy ==1)
 {
 seedfill(x,y+1,0,1);
 }
 glVertex2s(x,y);
 } 
}

 void Axes(void)
{
 int i;
 glColor3f (1.0, 1.0, 1.0);
 for(i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 }
 for(i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 }
}
 void init(void)
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }
int main(int argc, char** argv)
{
 printf("Enter the bottom left co-ordinates of rectangle:\n");
 scanf("%d %d",&x1,&Y1);
 printf("Enter the top right co-ordinates of rectangle:\n");
 scanf("%d %d",&x2,&y2);
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("Square : Seed Fill Method ");
 init ();
 glutDisplayFunc(display);
 glutMainLoop(); 
 return 0;
}

OUTPUT:

SeedFill

2D Transformations using OpenGL – Program Source Code


The following Source code performs the following 2 dimensional transformations:

  • Translations
  • Scaling
  • Rotation
  • Shearing
  • Reflection
  • Composite Transformations

Source Code:

#include 
#include 
#include 
#include 

typedef float Matrix3x3 [3][3];
Matrix3x3 theMatrix;
int NEdges;
float ptsIni[20][2];
float ptsFin[20][2];
float refpt[2];
float RotAngle;
float TransDistX,TransDistY;
float ScaleX,ScaleY;
int choice,choiceRef,choiceShear;
float slope,yIntercept;
float shearValue;

void matrixSetIdentity(Matrix3x3 m)
// Initialises the matrix as Unit Matrix
{
   int i, j;
   for (i=0; i<3; i++)
   for (j=0; j<3; j++)
   m[i][j] = (i == j);
}

void matrixPreMultiply(Matrix3x3 a , Matrix3x3 b)
// Multiplies matrix a times b, putting result in b
{
   int i,j;
   Matrix3x3 tmp;
   for (i = 0; i < 3; i++)
     for (j = 0; j < 3; j++)
       tmp[i][j] = a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j];
   for (i = 0; i < 3; i++)
     for (j = 0; j < 3; j++)
       theMatrix[i][j] = tmp[i][j];
}

void Translate(int tx, int ty)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   m[0][2] = tx;
   m[1][2] = ty;
   matrixPreMultiply(m, theMatrix);
}

void Scale(float sx , float sy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   m[0][0] = sx;
   m[0][2] = (1 - sx)*refpt[0];
   m[1][1] = sy;
   m[1][2] = (1 - sy)*refpt[1];
   matrixPreMultiply(m , theMatrix);
}

void Rotate(float a)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   a = a*22/1260;
   m[0][0] = cos(a);
   m[0][1] = -sin(a) ;
   m[0][2] = refpt[0]*(1 - cos(a)) + refpt[1]*sin(a);
   m[1][0] = sin(a);
   m[1][1] = cos(a);
   m[1][2] = refpt[1]*(1 - cos(a)) - refpt[0]*sin(a);
   matrixPreMultiply(m , theMatrix);
}

void Reflect(int xy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   if(xy == 2)
   m[1][1] = -1;
   if(xy == 3)
   m[0][0] = -1;
   matrixPreMultiply(m , theMatrix);
}

void Shear(int xy)
{
   Matrix3x3 m;
   matrixSetIdentity(m);
   if(xy == 1)
     m[0][1] = shearValue;
   if(xy == 2)
     m[1][0] = shearValue;
   matrixPreMultiply(m , theMatrix);
}void TransformPoints(void)
{
   int k;
   float tmp ;
   for (k = 0 ; k ");
  scanf("%d",&NEdges);
  printf("Enter %d Co-ordinate pairs \n",NEdges);
  int PlotCt;
  for(PlotCt=0 ; PlotCt");
  scanf("%d",&choice);
  switch(choice)
  {
    case 1:     printf("Enter reference point Co-ordinates:\n=>");
      scanf("%f %f",&refpt[0],&refpt[1]);
      printf("Enter Angle of Rotation\n=>");
      scanf("%f",&RotAngle);
      printf("Enter translation along X & Y\n=>");
      scanf("%f%f",&TransDistX , &TransDistY);
      printf("Enter Scaling ratios along X & Y\n=>");
      scanf("%f%f",&ScaleX , &ScaleY);
      break;
    case 2:     printf("Enter translation along X & Y\n=>");
      scanf("%f%f",&TransDistX , &TransDistY);
      break;
    case 3:     printf("Enter Angle of Rotation\n=>");
      scanf("%f",&RotAngle);
      printf("Enter reference point Co-ordinates:\n=>");
      scanf("%f %f",&refpt[0],&refpt[1]);
      break;
    case 4:     printf("Enter Scaling ratios along X & Y\n=>");
      scanf("%f%f",&ScaleX , &ScaleY);
      break;
    case 5:     printf("Enter your choice number for Reflection about:\n
                    1.Origin\n 2.X-axis\n 3.Y-axis\n 4.y = mx+c\n=>");
      scanf("%d",&choiceRef);
      if(choiceRef == 4)
      {
        printf("Enter m & c: \n=>");
        scanf("%f %f",&slope,&yIntercept);
      }
      break;      
    case 6:     printf("Enter your choice number to shear about:\n
                    1.x-axis\n 2.y-axis\n=>");
      scanf("%d",&choiceShear);
      printf("Enter shear value:\n=>");
      scanf("%f",&shearValue);
      break;
    default:    printf("Please enter a valid choice!!!\n");
      return 0;
  }
  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}

OUTPUT:

1 . Translation

INPUT :

4                                  // No. of vertices of Polygon
20      20                         //(x,y) Co-ordinates of Vertices
100     20
100     100
20      100
2                                  // Selected option 2 for Translation
-50      -140                      //  Translation along X & Y

OUTPUT :

2DTrans1

2 . Rotation

INPUT :

4                                 // No. of vertices of Polygon
20      20                        // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
3                                 // Choice no. for Rotation
60                                // Angle of Rotation in Degrees
-50      -50                      // Reference point Co-ordinates

OUTPUT :

2DTrans2

3 . Scaling

INPUT :

4                                  // No. of vertices of Polygon
20      20                         // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
4                                  // Choice no. for Scaling
0.5      0.5                       // Scaling factors along X & Y

OUTPUT :

2DTrans3

4 . Reflection

About arbitrary line y=mx+c

INPUT :

4                                 // No. of vertices of Polygon
20      20                        // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
5                                 // Choice no. for Reflection
4                                 // Choice for Reflection about y=mx+c
1        -30                      //  m  &  c values

OUTPUT :

2DTrans4

5 . Shearing

X-direction Shear

INPUT :

4                                  // No. of vertices of Polygon
20      20                         // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
6                                  // Choice no. for Shearing
1                                  // Choice no. for Shear about X axis
1.5                                // Shear Coefficient

OUTPUT :

2DTrans5

6 . Composite Transformation

INPUT :

4                                   // No. of vertices of Polygon
20      20                          // (x,y) Co-ordinates of Vertices
100     20
100     100
20      100
1                                   // Choice no. for Comp. Transf.
-50     -50                         // Reference Point Co-ordinates
60                                  // Angle of Rotation in Degrees
-40      -80                        // Translation along X & Y
1.5      1.5                        // Scaling factors along X & Y

OUTPUT :

2DTrans6

Polygon Filling (Scanline) using OpenGL – Program Source Code


#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h,k,r;
void scanline(int,int);
void circle(void);
void Axes(void);
 void display(void)
 {
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 glBegin(GL_POINTS);
 circle();
 glEnd();
 glFlush();
 }
 void scanline(int ax, int by)
{
 int varx;
 for(varx=h+ax ; varx>=(h-ax) ; varx--)
 glVertex2s(varx,by);
} 
void circle(void)
{
 double XEnd,J;
 int i,j; 
 XEnd=(r/1.414);
 for( i=0 ; i<=XEnd ; i++)
 { 
 J=sqrt(r*r - i*i);
 j=(int)(J);
 scanline(i, j);
 scanline(j, i);
 scanline(j,-i);
 scanline(i,-j);
 }
 Axes();
 glVertex3s(h,k,-25);
} 
 void Axes(void)
{
 int i;
 glColor3f (1.0, 1.0, 1.0);
 for(i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 }
 for(i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 }
}
 void init(void)
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }
int main(int argc, char** argv)
{
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 printf("Enter the center of circle:\n");
 scanf("%d %d",&h,&k);
 printf("Enter the radius:\n");
 scanf("%d",&r);
 glutCreateWindow ("Circle : Scanline Filling Method ");
 init ();
 glutDisplayFunc(display);
 glutMainLoop(); 
 return 0;
}

OUTPUT:

Scanline

Ellipse Generation (Trigonometric) using OpenGL – Program Source Code


#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h,k;
float a,b;

 void display(void)
 {
 double I,J,A;
 int i,j;
 float theta; 
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 glBegin(GL_POINTS);
 glVertex2s(h,k);
 for( theta=0 ; theta<=90 ; theta++)
 { 
 A=theta*11/630;
 J=b*sin(A);
 I=a*cos(A);
 i=(int)(I);
 j=(int)(J);
 glVertex2s(h+i,k+j);
 glVertex2s(h-i,k+j);
 glVertex2s(h-i,k-j);
 glVertex2s(h+i,k-j);
 }
 glColor3f (1.0, 1.0, 1.0);
 for(int i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 }
 for(int i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(int i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 }
 glEnd();
 glFlush();
 }
void init(void)
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }
int main(int argc, char** argv)
{
 printf("Enter the center of ellipse:\n");
 scanf("%d %d",&h,&k);
 printf("Enter the parameters a & b:\n");
 scanf("%f %f",&a,&b);
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("Ellipse : Trignometric Method ");
 init ();
 glutDisplayFunc(display);
 glutMainLoop();
 return 0;
}

Ellipse Generation (Polynomial) using OpenGL – Program Source Code


#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int h,k;
float a,b;
 void display(void)
 {
 double I,J;
 int i,j;
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 glBegin(GL_POINTS);
 glVertex2s(h,k);
 for( i=0 ; i<=a ; i+=1)
 { 
 J=sqrt(1 - (i*i)/(a*a))*b;
 j=(int)(J);
 glVertex2s(h+i,k+j);
 glVertex2s(h-i,k+j);
 glVertex2s(h-i,k-j);
 glVertex2s(h+i,k-j);
 }
 glColor3f (1.0, 1.0, 1.0);
 for(int i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 }
 for(int i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(int i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 }
 glEnd();
 glFlush();
 }
void init(void)
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }
int main(int argc, char** argv)
{
 printf("Enter the center of ellipse:\n");
 scanf("%d %d",&h,&k);
 printf("Enter the parameters a & b:\n");
 scanf("%f %f",&a,&b);
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("Ellipse : Polynomial Method ");
 init ();
 glutDisplayFunc(display);
 glutMainLoop();
 return 0;
}

Dashed Line Generation (DDA) using OpenGL – Program Source Code


#include <GL/glut.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int x1,x2,y1,y2;
float gap;
void display(void)
 {
int DelX,DelY,Max;
 int n=0,Total=1,j=0;
 double Sigma=1,XIncr,YIncr,x,y,slope;
 double gapX,gapY;
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 if(x2<x1 && y2<y1)
 {
 int var;
 var=x1; x1=x2; x2=var;
 var=y1; y1=y2; y2=var;
 }
 DelX = x2 - x1;
 DelY = y2 - y1; 
 Max = ((DelX > DelY) ? DelX:DelY); 

 for( n=0 ; Total<=Max ; n++)
 {
 Total=Total*2;
 }
 for( int i=n ; i>0 ; i--)
 {
 Sigma = Sigma/2;
 }
 XIncr = Sigma*DelX;
 YIncr = Sigma*DelY; 
 x=x1; y=y1;

 glBegin(GL_POINTS);
 glVertex2s(x1,y1);
 slope = DelY/DelX; 
 gapX = gap/sqrt(1+slope*slope);
 // gapX = gap/.5;
 while(x!=x2 || y!=y2)
 {
 x = x+XIncr;
 y = y+YIncr;
 j = x/gapX;

 if((j%2)==0)
 {
 glVertex2s(x,y);
 printf("%lf %lf\n ",x,y);
 }
 } 
 glColor3f (1.0, 1.0, 1.0);
 for(int i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 } 
 for(int i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(int i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 } 
 glEnd();
 glFlush();
}
 void init(void) 
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }
int main(int argc, char **argv)
 { 
 printf("Enter Initial points:\n"); 
 scanf("%d%d", &x1 , & y1);
 printf("Enter Final points:\n");
 scanf("%d%d", &x2 , & y2);
 printf("Enter Gapping:\n");
 scanf("%f", &gap); 

 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500); 
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("DDA");
 init ();
 glutDisplayFunc(display);
 glutMainLoop ();
 return 0;
 }

OUTPUT:

Dashed DDA

Line Generation (Bresenhams) using OpenGL – Program Source Code


#include<stdlib.h>
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
void init()
{
 glMatrixMode(GL_PROJECTION);
 gluOrtho2D(0.0,300.0,0.0,300.0);
 glClearColor(0.0,0.0,0.0,0.0);
}
void setpixel(int x,int y)
{
 glBegin(GL_POINTS);
 glVertex2i(x,y);
 glEnd();
 glFlush();
}
void lineBressenham(int xa,int ya,int xb,int yb)
{
 int x,y,xEnd,yEnd;
int dx=abs(xa-xb);
 int dy=abs(ya-yb);
int p=2*dy-dx;
 int twody=2*dy;
 int twodydx=2*(dy-dx);
float m=(float)(yb-ya)/(float)(xb-xa);
if((m>=0) && (m<=1))
 { 
 if(xa>xb)
 {
 x=xb;
 y=yb;
 xEnd=xa;
 }
 else
 {
 x=xa;
 y=ya;
 xEnd=xb;
 }

 setpixel(x,y);
while(x<xEnd)
 {
 x++;
 if(p<0)
 p+=twody;
 else
 {
 y++; 
 p+=twodydx;
 }

 setpixel(x,y);
 }
 }

 else if(m>1)
 {
 if(ya>yb)
 {
 x=xb;
 y=yb;
 yEnd=ya;
 }
 else
 {
 x=xa;
 y=ya;
 yEnd=yb;
 }

 setpixel(x,y);
while(y<yEnd)
 {
 y++;
 if(p<0)
 p+=2*dx;
 else
 {
 x++; 
 p+=2*(dx-dy);
 }

 setpixel(x,y);
 }
 }
else if(m<-1)
 {
 if(ya>yb)
 {
 x=xb;
 y=yb;
 yEnd=ya;
 }
 else
 {
 x=xa;
 y=ya;
 yEnd=yb;
 }

 setpixel(x,y);
while(y<yEnd)
 {
 y--;
 if(p<0)
 p+=2*dx;
 else
 {
 x++; 
 p+=2*(dx-dy);
 }

 setpixel(x,y);
 }
 }
else if((m>=-1) && (m<=0))
 {
 if(xa>xb)
 {
 x=xb;
 y=yb;
 xEnd=xa;
 }
 else
 {
 x=xa;
 y=ya;
 xEnd=xb;
 }

 setpixel(x,y);
while(x<xEnd)
 {
 x++;
 if(p<0)
 p+=twody;
 else
 {
 y--; 
 p+=twodydx;
 }

 setpixel(x,y);
 }
 }

}
void setdata()
{
 int xa,xb,ya,yb; 
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1.0,0.0,0.0);
 glPointSize(4.0);
 printf("Enter the points : ");
 scanf("%d %d %d %d",&xa,&ya,&xb,&yb);
 lineBressenham(xa,ya,xb,yb);
}
int main(int argc,char**argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize(400,400);
 glutInitWindowPosition(0,0);
 glutCreateWindow("Bressenham Line Algorithm");
 init();
 glutDisplayFunc(setdata);
 glutMainLoop();
 return 0;
}

OUTPUT:

Line Bresenhams

Line Generation (Simple DDA) using OpenGL – Program Source Code


#include<GL/glut.h>
#include <GL/gl.h>
#include<stdlib.h>
#define ROUND(x) ((int)(x+0.5))
int xa,xb,ya,yb;
void display (void)
 {
 int dx=xb-xa,dy=yb-ya,steps,k;
 float xIncrement,yIncrement,x=xa,y=ya;
 glClear (GL_COLOR_BUFFER_BIT);
 glColor3f (1.0, 0.0, 0.0);
 if(abs(dx)>abs(dy))
 steps=abs(dx);
 else steps=abs(dy);
 xIncrement=dx/(float)steps;
 yIncrement=dy/(float)steps;
 glBegin(GL_POINTS);
 glVertex2s(ROUND(x),ROUND(y));
for(k=0;k<steps;k++)
 {
 x+=xIncrement;
 y+=yIncrement;
 glVertex2s(ROUND(x),ROUND(y));
 printf("%lf %lf\n",x,y);
 }
 glColor3f (1.0, 1.0, 1.0);
 for(int i=-100 ; i<=100 ; i++)
 {
 glVertex2s(i,0);
 glVertex2s(0,i);
 }
 for(int i=-2; i<=2 ; i++)
 {
 glVertex2s(95+i,4+i);
 glVertex2s(95-i,4+i);
 } 
 for(int i=0; i<=2 ; i++)
 {
 glVertex2s(4+i,95+i);
 glVertex2s(4-i,95+i);
 glVertex2s(4,95-i);
 }

 glEnd();
 glFlush();
 }
 void init(void)
 {
 glClearColor (0.0, 0.0, 0.0, 0.0);
 glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
 }

int main(int argc, char** argv)
{
 printf("Enter the points\n");
 scanf("%d %d %d %d",&xa,&ya,&xb,&yb);
 glutInit(&argc, argv);
 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize (500, 500);
 glutInitWindowPosition (100, 100);
 glutCreateWindow ("Simple DDA ");
 init ();
 glutDisplayFunc(display);
 glutMainLoop();
 return 0;
}

OUTPUT:

Line Generation ( Simple DDA)