Thursday, 26 February 2015

Scaling about arbitrary point

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
int n;

void reset (int h[][2])
{
    int i,j,val[50][2];
    printf("\n enter no of sides");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&val[i][j]);
    }
    }
    for (i=0; i<n; i++)
    {
    h[i][0] = val[i][0];
    h[i][1] = val[i][1];
    }
}

void draw (int h[][2])
{
    int i;
    setlinestyle (DOTTED_LINE, 0, 1);
    line (320, 0, 320, 480);
    line (0, 240, 640, 240);
    setlinestyle (SOLID_LINE, 0, 1);
    for (i=0; i<n-1; i++)
    line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]);
    line (320+h[0][0], 240-h[0][1], 320+h[n-1][0], 240-h[n-1][1]);
}

void scale (int h[][2], int sx, int sy)
{
    int i;
    for (i=0; i<n; i++)
    {
    h[i][0] *= sx;
    h[i][1] *= sy;
    }
}
void translate (int h[][2], int dx, int dy)
{
    int i;
    for (i=0; i<n; i++)
    {
    h[i][0] += dx;
    h[i][1] += dy;
    }
}
void ini()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,NULL);
}

void dini()
{
    getch();
    closegraph();
}

void main()
{
    int h[50][2],sx,sy,x,y;
    printf ("Enter the x- and y-scaling factors: ");
    scanf ("%d%d", &sx, &sy);
    printf ("Enter the x- and y-coordinates of the point: ");
    scanf ("%d%d", &x, &y);
    reset (h);               
    ini();
    putpixel(x,y,15);
    translate (h, x, y);// Go to arbitrary point
    draw(h); getch();//Show its arbitrary position
    cleardevice();
    translate(h,-x,-y);//Take it back to origin
    draw(h);
    getch();
    cleardevice();
    scale (h, sx, sy);//Now Scale it
    draw(h);
    getch();
    translate (h, x, y);//Back to Arbitrary point
    cleardevice();
    draw (h);
    putpixel (320+x, 240-y, WHITE);
    dini();
}

No comments:

Post a Comment