Tuesday, December 23, 2014

2D scaling Without Using OpenGL Function For Scaling

Program to draw a square of side 100 units at the center of the screen and scale it such that it enlarges to a square of side 150 units without using OpenGL function for scaling


#include<stdio.h>
#include<GL/glut.h>

int i;
float scalex, scaley, a[10], b[10], x, y, square_side;

void line(float x, float y, float a, float b)
{
  glBegin(GL_LINES);
  glVertex2f(x, y);
  glVertex2f(a, b);
  glEnd();
}

void display_square()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0.0 , 640.0 , 0.0 , 480.0);
}

void scale()
{
  // Find the translated rectangle vertices
  for(i = 0; i < 4; i++)
  {
    a[i] = a[i] * scalex; b[i] = b[i] * scaley;
  }
}

void draw()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original square vertices
  // (x, y) represents the upper left point
  x = 270.0; y = 290.0;

  a[0] = x; b[0] = y;
  a[1] = x + square_side; b[1] = y;
  a[2] = x + square_side; b[2] = y - square_side;
  a[3] = x; b[3] = y - square_side;
  // Draw original square
  display_square();

  // Perform scaling
  scale();
  // Draw scaled square using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_square();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  square_side = 100;
  printf("\n************************************************\n");
  printf("Length of the side of the square: %f\n", square_side);

  scalex = 1.5;
  scaley = 1.5;
  printf("\n************************************************\n");
  printf("Scaling factor along both directions: %f\n", scalex);
  printf("\n************************************************\n");

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Scaling");
  Init();
  glutDisplayFunc(draw);
  glutMainLoop();
}


Written by

2D Rotation Without Using OpenGL Function For Rotation

Program to perform 2D rotation without using OpenGL function for rotation

#include<stdio.h>
#include<math.h>
#include<GL/glut.h>

int x, y, p, q, xa, ya, ra, i, a[10], b[10], da, db;
float dx, dy, theta;

void line(int x, int y, int a, int b)
{
  glVertex2d(x, y);
  glVertex2d(a, b);
}

void display_rectangle()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void rotate()
{
  // Find the vertices of the rotated rectangle
  theta = (float)(ra*(3.14/180));
  for(i = 0;i<4;i++)
  {
    a[i] = (xa + ((a[i] - xa)*cos(theta) - (b[i] - ya)*sin(theta)));
    b[i] = (ya + ((a[i] - xa)*sin(theta) + (b[i] - ya)*cos(theta)));
  }
}

void transform()
{
  glClear(GL_COLOR_BUFFER_BIT);

  glBegin(GL_LINES);
  // Find the original rectangle vertices
  da = p - x; db = q - y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;

  // Draw original rectangle
  display_rectangle();

  rotate();
  
  // Draw rotated rectangle
  display_rectangle();
  
  glEnd();

  glFlush();

}

void main(int argc, char **argv)
{
  printf("\n**********************************************\n");
  printf("Enter the upper left corner of the rectangle:\n");
  scanf("%d%d", &x, &y);
  printf("\n**********************************************\n");
  printf("Enter the lower right corner of the rectangle:\n");
  scanf("%d%d", &p, &q);

  printf("\n********************Rotation********************\n");
  printf("Enter the value of fixed point and angle of rotation:\n");
  scanf("%d%d%d", &xa, &ya, &ra);

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Rotation");
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}

Written by

Monday, December 22, 2014

2D Translation Without Using OpenGL Function For Translation

Program to draw a rectangle and translate it without using OpenGL function for translation

#include<stdio.h>
#include<GL/glut.h>

int x, y, p, q, a[10], b[10], da, db, i;
float dx, dy;

void line(int x, int y, int a, int b)
{
  glBegin(GL_LINES);
  glVertex2d(x, y);
  glVertex2d(a, b);
  glEnd();
}

void display_rectangle()
{
  for(i = 0; i < 4; i++)
  {
    if(i != 3)
      line(a[i], b[i], a[i+1], b[i+1]);
    else
      line(a[i], b[i], a[0], b[0]);
  }
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0, 1.0, 1.0, 0);
  /* Set fill color to black */
  glColor3f(0.0, 0.0, 0.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void translate()
{
  // Find the translated rectangle vertices
  for(i = 0; i < 4; i++)
  {
    a[i] = a[i] + dx; b[i] = b[i] + dy;
  }
}

void transform()
{
 glClear(GL_COLOR_BUFFER_BIT);

  // Find the original rectangle vertices
  da = p-x; db = q-y;
  a[0] = x; b[0] = y;
  a[1] = x + da; b[1] = y;
  a[2] = x + da; b[2] = y + db;
  a[3] = x; b[3] = y + db;
  // Draw original rectangle
  display_rectangle();

  // Perform translation
  translate();
  // Draw translated rectangle using dotted lines
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1, 0xF0F0);
  display_rectangle();
  glDisable(GL_LINE_STIPPLE);

  glFlush();
}

void main(int argc, char **argv)
{
  printf("\n************************************************\n");
  printf("Enter the upper left corner of the rectangle:\n");
  scanf("%d%d", &x, &y);
  printf("\n************************************************\n");
  printf("Enter the lower right corner of the rectangle:\n");
  scanf("%d%d", &p, &q);

  printf("\n******************Translation******************\n");
  printf("Enter the value of shift vector:\n");
  scanf("%f%f", &dx, &dy);

  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowPosition(0, 0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Translation");
  Init();
  glutDisplayFunc(transform);
  glutMainLoop();
}
Written by

Friday, December 19, 2014

Lexicographic sorting of strings

/* Program to perform lexicographic sorting of strings */


#include<stdio.h>
#include<string.h>
#include<ctype.h>

void main()
{
  char str[50][50], temp[50], str1[50][50], n[1], temp1[50];
  int i, j, m, k;
  printf("\n##############################\n");
  printf("Enter the number of strings\n");
  gets(n);
  m = atoi(n);
  printf("\n##############################\n");
  printf("Enter the strings\n");
  for(i = 0; i < m; i++)
    gets(str[i]);

  // Convert all letters to lowercase
  for(i = 0; i < m; i++)
    for(j = 0; j < strlen(str[i]); j++)
      if(isupper(str[i][j]) != 0)
        str1[i][j] = tolower(str[i][j]);
      else
        str1[i][j] = str[i][j];

  for(i = 0; i < m; i++)
    for(j = i + 1; j < m; j++)
      if((strcmp(str1[i], str1[j])) > 0)
      {
        strcpy(temp, str[i]);strcpy(temp1, str1[i]);
        strcpy(str[i], str[j]);strcpy(str1[i], str1[j]);
        strcpy(str[j], temp);strcpy(str1[j], temp1);
      }

  printf("\n##############################\n");
  printf("Sorted list is \n");
  for(i = 0; i < m; i++)
    puts(str[i]);
  printf("##############################\n");
}


Written by

Sunday, December 14, 2014

Smiley using Bresenham's Circle Drawing in OpenGL


#include <stdio.h>
#include <GL/glut.h>

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot two points on the lower quadrants 
// using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc-x, yc-y);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a smiley
void smiley(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw an arc
  bresenham_circle(radius);

  // Draw a vertical line
  glBegin(GL_LINES);
  glVertex2i(xc, yc);
  glVertex2i(xc, yc-60);
  glEnd();

  // Draw 2 points on either side 
  // of the line
  glBegin(GL_POINTS);
  // A big dot made of 4 small dots
  // to the left of the line
  glVertex2i(xc-20, yc-20);
  glVertex2i(xc-19, yc-20);
  glVertex2i(xc-20, yc-19);
  glVertex2i(xc-19, yc-19);

  // A big dot made of 4 small dots
  // to the right of the line
  glVertex2i(xc+20, yc-20);
  glVertex2i(xc+21, yc-20);
  glVertex2i(xc+21, yc-19);
  glVertex2i(xc+20, yc-19);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white*/
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("Smiley :-)");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(smiley);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}

Output
=====


Written by

Friday, December 12, 2014

Static Clock using Bresenham's Circle Drawing in OpenGL

/* To draw a static clock using Bresenham's circle drawing algorithm */
#include <stdio.h>
#include <GL/glut.h>

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
}

// Function to draw a static clock
void static_clock(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius = 100;
  // Draw a circle
  bresenham_circle(radius);
  glBegin(GL_LINES);
  // Minute hand
  glVertex2i(xc, yc);
  glVertex2i(xc, yc+95);
  // Hour hand
  glVertex2i(xc, yc);
  glVertex2i(xc+30, yc+50);
  glEnd();
  glFlush();
}

void Init()
{
  /* Set clear color to black */
  glClearColor(0.0,0.0,0.0,0);
  /* Set fill color to white */
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("bresenham_circle");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(static_clock);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}
Output















Written by 

Wednesday, December 10, 2014

Bresenham's Circle Drawing Algorithm using OpenGL

This program is to draw two concentric circles using bresenham's circle drawing algorithm with center (320, 240) and radii of circles as 100 and 200.

#include <stdio.h>
#include <math.h>
#include <GL/glut.h>

// Center of the cicle = (320, 240)
int xc = 320, yc = 240;

// Plot eight points using circle's symmetrical property
void plot_point(int x, int y)
{
  glBegin(GL_POINTS);
  glVertex2i(xc+x, yc+y);
  glVertex2i(xc+x, yc-y);
  glVertex2i(xc+y, yc+x);
  glVertex2i(xc+y, yc-x);
  glVertex2i(xc-x, yc-y);
  glVertex2i(xc-y, yc-x);
  glVertex2i(xc-x, yc+y);
  glVertex2i(xc-y, yc+x);
  glEnd();
}

// Function to draw a circle using bresenham's
// circle drawing algorithm
void bresenham_circle(int r)
{
  int x=0,y=r;
  float pk=(5.0/4.0)-r;

  /* Plot the points */
  /* Plot the first point */
  plot_point(x,y);
  int k;
  /* Find all vertices till x=y */
  while(x < y)
  {
    x = x + 1;
    if(pk < 0)
      pk = pk + 2*x+1;
    else
    {
      y = y - 1;
      pk = pk + 2*(x - y) + 1;
    }
    plot_point(x,y);
  }
  glFlush();
}

// Function to draw two concentric circles
void concentric_circles(void)
{
  /* Clears buffers to preset values */
  glClear(GL_COLOR_BUFFER_BIT);

  int radius1 = 100, radius2 = 200;
  bresenham_circle(radius1);
  bresenham_circle(radius2);
}

void Init()
{
  /* Set clear color to white */
  glClearColor(1.0,1.0,1.0,0);
  /* Set fill color to black */
  glColor3f(0.0,0.0,0.0);
  /* glViewport(0 , 0 , 640 , 480); */
  /* glMatrixMode(GL_PROJECTION); */
  /* glLoadIdentity(); */
  gluOrtho2D(0 , 640 , 0 , 480);
}

void main(int argc, char **argv)
{
  /* Initialise GLUT library */
  glutInit(&argc,argv);
  /* Set the initial display mode */
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  /* Set the initial window position and size */
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640,480);
  /* Create the window with title "DDA_Line" */
  glutCreateWindow("bresenham_circle");
  /* Initialize drawing colors */
  Init();
  /* Call the displaying function */
  glutDisplayFunc(concentric_circles);
  /* Keep displaying untill the program is closed */
  glutMainLoop();
}

Written by

Generalized Bresenham's Line Drawing Algorithm using OpenGL

Bresenham's line algorithm is an algorithm that determines which points in an n-dimensional raster should be plotted in order to form a close approximation to a straight line between two given points. It is commonly used to draw lines on a computer screen, as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in standard computer architectures[source]. For more visit this website.
#include<stdio.h>
#include<GL/gl.h>
#include<GL/glut.h>

/* Function that returns -1,0,1 depending on whether x */
/* is <0, =0, >0 respectively */
#define sign(x) ((x>0)?1:((x<0)?-1:0))

/* Function to plot a point */
void setPixel(GLint x, GLint y) {
  glBegin(GL_POINTS);
  glVertex2i(x,y);
  glEnd();
}

/* Generalized Bresenham's Algorithm */
void bres_general(int x1, int y1, int x2, int y2)
{
  int dx, dy, x, y, d, s1, s2, swap=0, temp;

  dx = abs(x2 - x1);
  dy = abs(y2 - y1);
  s1 = sign(x2-x1);
  s2 = sign(y2-y1);

  /* Check if dx or dy has a greater range */
  /* if dy has a greater range than dx swap dx and dy */
  if(dy > dx){temp = dx; dx = dy; dy = temp; swap = 1;}

  /* Set the initial decision parameter and the initial point */
  d = 2 * dy - dx;
  x = x1;
  y = y1;

  int i;
  for(i = 1; i <= dx; i++)
  {
    setPixel(x,y);
    
    while(d >= 0) 
    {
      if(swap) x = x + s1;
      else 
      {
        y = y + s2;
        d = d - 2* dx;
      }
    }
    if(swap) y = y + s2;
    else x = x + s1;
    d = d + 2 * dy;
  }
  glFlush();
}

/* Function to draw a rhombus inscribed in a rectangle and roll */
/* number printed in it */
void draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
 
  /* Draw rectangle */
  bres_general(20,40,620,40);
  bres_general(620,40,620,440);
  bres_general(620,440,20,440);
  bres_general(20,440,20,40);

  /* Draw rhombus */
  bres_general(320,440,20,240);
  bres_general(20,240,320,40);
  bres_general(320,40,620,240);
  bres_general(620,240,320,440);

  /* 1 */
  bres_general(250,150,250,250);
  /* 0 */
  bres_general(300,150,300,250);
  bres_general(300,250,400,250);
  bres_general(400,250,400,150);
  bres_general(400,150,300,150);

  glFlush();
}

void init() {  
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
  glutInitWindowPosition(0,0);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Green Window");
  glClearColor(0.0,0.0,0.0,0);
  glColor3f(1.0,1.0,1.0);
  gluOrtho2D(0,640,0,480);
  
}

int main(int argc, char **argv) 
{
  glutInit(&argc, argv);
  init();
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}


Written by

Monday, December 1, 2014

String replacement program in C

A program to find a particular pattern in a string and replace all occurrences of that pattern with a new pattern to produce a modified string.


#include<stdio.h>
#include<string.h>

void main()
{
  char str[100], pattern[50], replace[50], new_str[100], temp[50];
  int l1, l2, l3, flag_eos = 0, pattern_found = 0;
  int i = 0, j, k, n;
  printf("\n###################################\n");
  printf("Enter any string\n");
  gets(str);
  printf("\n###################################\n");
  printf("Enter pattern to be replaced\n");
  gets(pattern);
  printf("\n###################################\n");
  printf("Enter new pattern\n");
  gets(replace);

  l1 = strlen(str);
  l2 = strlen(pattern);
  l3 = strlen(replace);
 
  while(flag_eos == 0)
  {
    for(; i < l1; i++)
      // if any character of str = first character of the pattern
      if((str[i] == pattern[0]) && (i + l2 - 1 < l1))
      {
        for(j = i, k = 0; j < i + l2; j++)
          temp[k++] = str[j];
        temp[k] = '\0';
        break;
      }
    if(i == l1)
    {
      flag_eos = 1;
      temp[0] = '\0';
    }
    if(strcmp(temp, pattern) == 0)
    {
      pattern_found = 1;
      n = 0;
      for(j = 0; j < i; j++)
        new_str[n++] = str[j];
      for(j = 0; j < l3; j++)
        new_str[n++] = replace[j];
      for(j = i + l2; j < l1; j++)
        new_str[n++] = str[j];
      new_str[n] = '\0';
      // Recompute the new string length and
      // continue to look for more occurrences
      strcpy(str, new_str);
      l1 = strlen(str);
    }
    else
      i = i + 1;
  }
  if(pattern_found == 1)
  {
    printf("\n###################################\n");
    printf("The modified string is \n");
    puts(str);
    printf("###################################\n");
  }
  else
  {
    printf("\n###################################\n");
    printf("Pattern not found in the string !!!\n");
    printf("###################################\n");
  }
}

Written by