Showing posts with label scaling. Show all posts
Showing posts with label scaling. Show all posts

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