Friday, 21 September 2012


Geometric Transformations

A) Translation, Rotation Scaling
B) bresenham line
C) Mid-Point Circle

(A) Translation, Rotation Scaling

Code
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
    int val,x1,y1,x2,y2,x1dash,x2dash,y1dash,y2dash,tx,ty,sx,sy,x,y,theta;
    float rad;
    char cont;
    int gdriver=DETECT,gmode,errorcode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

    printf("Select the following option\n");
    printf("1.Translation\n");
    printf("2.Scaling\n");
    printf("3.Rotation\n");
    scanf("%d",&val);
    switch (val)
    {
        case 1:
        printf("You have selected translation\n");
        printf("Enter the first co-ordinates\n");
        scanf("%d%d",&x1,&y1);
        printf("Enter the second co-ordinates\n");
        scanf("%d%d",&x2,&y2);
        line(x1,y1,x2,y2);
        printf("Enter the translation vector in x direction\n");
        scanf("%d",&tx);
        printf("Enter the translation vector in y direction\n");
        scanf("%d",&ty);
        x1dash=x1+tx;
        y1dash=y1+ty;
        x2dash=x2+tx;
        y2dash=y2+ty;
        line(x1dash,y1dash,x2dash,y2dash);
        break;
case 2:
        printf("You have selected Scaling\n");
        printf("Enter the radius of the circle\n");
        scanf("%d",&x1);
        circle(0,0,x1);
        printf("Enter the scaling vector \n");
        scanf("%d",&sx);
        x1dash=x1*sx;
        circle(0,0,x1dash);
        break;   
case 3:
        printf("You have entered Rotation\n");
        printf("Enter the co-ordinates of the line about the origin\n");
        scanf("%d%d",&x1,&y1);
        line(0,0,x1,y1);
        printf("Enter the angle by which the line is to be rotated\n");
        scanf("%d",&theta);
        rad=theta*(3.14/180);
        x=(x1*cos(rad))-(y1*sin(rad));
        y=(y1*cos(rad))+(x1*sin(rad));
        printf("this is new x  co-ordinate %d",x);
        printf("\nthis is new y co-ordinate %d",y);
line(0,0,x,y);
        break;
default:
        printf("You have selected invalid option\n");
        break;
    }
    getch();
}

Output for TRANSLATION



Output for SCALING



Output for ROTATION




(B) bresenham line

Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
    int x1,y1,x2,y2,dx,dy,xdiff,ydiff,i;
    float m,e,X,Y;
    int gdriver=DETECT,gmode,errorcode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    printf("enter the first co-ordinates\n");
    scanf("%d%d",&x1,&y1);
    printf("\n");
    printf("Enter the second co-ordinates\n");
    scanf("%d%d",&x2,&y2);
    if(x1==x2 && y1==y2)
    {
        putpixel(x1,y1,15);
        printf("You have entered same value for both co-ordinates");
    }
    else
    {
        X=x1;
        Y=y1;
        dx=x2-x1;
        dy=y2-y1;
        m=dy/dx;
        e=-1/2;
        e=e+m;
        for(i=0;i<=dx;i++)
        {
            putpixel(X,Y,15);
            while(e>0)
            {
                Y=Y+1;
                e=e-1;
            }
            X=X+1;
            e=e+m;
        }
    }
getch();
}

Output Of An bresenham line





(C) Mid-Point Circle

Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
    int xc,yc,d,r;
    int x=0,y;
    int gdriver=DETECT,gmode,errorcode;
    initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
    printf("Enter the co-ordinates of the circle\n");
    scanf("%d%d",&xc,&yc);
    putpixel(xc,yc,15);
    printf("Enter the radius of the circle\n");
    scanf("%d",&r);
    y=r;
    d=1-r;
    while(x<=y)
    {
        putpixel(xc+x,yc+y,15);
        putpixel(xc+x,yc-y,15);
        putpixel(xc-x,yc+y,15);
        putpixel(xc-x,yc-y,15);
        putpixel(xc+y,yc+x,15);
        putpixel(xc+y,yc-x,15);
        putpixel(xc-y,yc+x,15);
        putpixel(xc-y,yc-x,15);
        if(d<0)
            d=d+(2*x)+1;
        else
{
d=d+(2*(x-y))+1;
y--;
        }
        x++;
    }
    getch(); 
}

OUTPUT FOR MID-POINT CIRCLE DRAWING


Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment