Sunday, 11 September 2011

Logo Package Drawing Using C (CODE)

This is a logo package by which you can draw different object using
keyboard. This program uses number of colour patterns and object movement
at the time drawing. This logo manages the cicle by which you can move into
different directions. Also can increase or decrease the radious of the circle
as you wish.
Code is given below.

#include <dos.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>

/* ------------------------------OPENING PAGE---------------------------
             this function is designed to create a
          graphical opening sequence of the program
-----------------------------------------------------------------------*/

void front()
{
    int x,y,h;
    for( x=0; x<=100; x++)
    {
        settextjustify( CENTER_TEXT, CENTER_TEXT);
        setcolor(1);
        settextstyle(1, 0, 4);
        setusercharsize(x, 6, x, 3);
        setcolor(14);
        moveto(310, 135);
        outtext(". .");
        setcolor(6);
        moveto(310, 130);
        outtext("I  I");
        if(x<50)
            delay(30);
        else
        delay(20);
    }
    for( y=0; y<101; y++)
    {
        setcolor(0);
        settextstyle(1, 0, 4);
        setusercharsize(y, 6, y, 3);
        moveto(310, 135);
        outtext(". .");
        moveto(310, 130);
        outtext("I  I");
        if(y>20)
        {
            settextstyle(7, 0, 3);
            setusercharsize(y-20, 35, y-20, 20);
            setcolor(9);
            outtext("LOGO II");
            setcolor(2);
            outtext("________");
            delay(15);
            if(y<100)
            {
                settextstyle(7, 0, 3);
                setusercharsize(y-20, 35, y-20, 20);
                setcolor(0);
                outtext("LOGO II");
            } //end if
        }   //end for
    }    //end for
    for( h=400; h>=0; h--)
    {
        setcolor(0);
        line(0, h, 640, h);
        delay(6);
    } //end for
}   //end function

/*------------------------------DECLARATIONS----------------------------
this program segment declares the various points and variables required
----------------------------------------------------------------------*/

const CX=320;
const CY=320;
const R=100;
int col=WHITE;

// Structure to draw points
struct point1
{
    int x;
    int y;
};

// Structure to draw circle
struct circl
{
    int x;
    int y;
    int r;
};

typedef struct circl circ;
typedef struct point1 point;

/*------------------------------BASIC FUNTIONS------------------------------
this program segment has the basic functions used throughout the program to
---------------------------perform all the functions-----------------------*/

void putcirc(circ c)
{
    circle(c.x,c.y,c.r);
}

void erasecirc(circ c)
{
    setcolor(BLACK);
    putcirc(c);
    setcolor(col);
}

// Place the pixel point at a specified position on the screen
void putp(point p)
{
    putpixel(p.x,p.y,col);
}

circ shiftcu(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.y=c1.y-1;
    putcirc(c1);
    return (c1);
}

circ shiftcr(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.x=c1.x+1;
    putcirc(c1);
    return (c1);
}

circ shiftcl(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.x=c1.x-1;
    putcirc(c1);
    return(c1);
}

circ shiftcb(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.y=c1.y+1;
    putcirc(c1);
    return (c1);
}

circ increaseradius(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.r=c1.r+1;
    putcirc(c1);
    return (c1);
}

circ reduceradius(circ c1)
{
    circ temp;
    temp=c1;
    erasecirc(temp);
    c1.r=c1.r-1;
    putcirc(c1);
    return (c1);
}

/*-------------------------ENDING GRAPHICAL SEQUENCE------------------------
the following function generates the ending graphical output .
---------------------------------------------------------------------------*/

void end()
{

    int col=0;
    int x1=510;
    int xo,yo,jj;
    char ch;
    for( xo=0; xo<130; xo++)
    {
        cleardevice();
        yo=(xo*xo)/80+6;
        moveto(xo-25, yo);
        setcolor(14);
        settextjustify(LEFT_TEXT, TOP_TEXT);
        settextstyle(8, 0, 6);
        outtext("ARE");
        setcolor(13);
        moveto(260, yo);
        outtext("YOU");
        x1--;
        setcolor(2);
        moveto(x1+7, yo);
        outtext("SURE");
        delay(30);
     }

    ch=getche();
    if (ch=='y' || ch=='Y')
    {
        int hh=640;
        for( jj=0; jj<=320; jj++)
        {
            setcolor(0);
            line(jj, 0, jj, 480);
            line(hh, 0, hh, 480);
            hh--;
            delay(7);
        }
        exit(0);
    }
    else
    {
        cleardevice();
        clrscr();
        cleardevice();
    }
    setcolor(col);
}

/*---------------------------------MAIN------------------------------------
this is the command control of the program which generates the user screeen
--------------------------------------------------------------------------*/

void main()
{
    point center;
    circ circle1;
    char ch;  //used to get user input.
    /* request auto detection */
    int gdriver = DETECT, gmode, errorcode;

    /* initialize graphics and local variables */
    initgraph(&gdriver, &gmode, "\\tclite\\bgi");

    /* read result of initialization */
    errorcode = graphresult();
    if (errorcode != grOk)  /* an error occurred */
    {
        printf("Graphics error: %s\n");
        printf ("%d",grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* terminate with an error code */
    }

    front();  //calling the functionn to generate the first screen.

    /* the following program code generates the user screen */
    start:
    setcolor(col);
    printf("\n t: UP");
    printf("\n g: DOWN");
    printf("\n f: LEFT");
    printf("\n h: RIGHT");
    printf("\n w: CIRCLE UP");
    printf("\n s: CIRCLE DOWN");
    printf("\n a: CIRCLE LEFT");
    printf("\n d: CIRCLE RIGHT");
    printf("\n c: CREATE CIRCLE");
    printf("\n p: INCREASE RADIUS");
    printf("\n o: DECREASE RADIUS");
    printf("\n z: ERASER ON");
    printf("\n x: ERASER OFF");
    printf("\n e: EXIT");
    printf("\n l: CLEAR SCREEN");
    printf("\n Use number keys ");
    printf("\n for colours");

    rectangle(0,0,150,295);

    //initialising the center point
    center.x=CX;
    center.y=CY;
    putp(center);

    //initialising the primary circle
    circle1.x=CX;
    circle1.y=CY;
    circle1.r=R;

    label1:
    putp(center);
    ch=getche();
    switch (ch)
    {
        case 't' : center.y--;
            putp(center);
            break;
        case 'g' : center.y++;
            putp(center);
            break;
        case 'f' : center.x--;
            putp(center);
            break;
        case 'h' : center.x++;
            putp(center);
            break;
        case 'w' : circle1 = shiftcu(circle1);
            break;
        case 's' : circle1 =shiftcb(circle1);
            break;
        case 'a' : circle1 =shiftcl(circle1);
            break;
        case 'd' : circle1 =shiftcr(circle1);
            break;
        case 'c' : circle1.x=CX;
            circle1.y=CY;
            circle1.r=R;
            putcirc(circle1);
            break;
        case 'p' : circle1 =increaseradius(circle1);
            break;
        case 'o' : circle1 =reduceradius(circle1);
            break;
        case 'z' : col=BLACK;
            break;
        case 'x' : col=WHITE;
            break;
        case 'e' : end();
            goto start;
        case 'l' : cleardevice();
            clrscr();
            cleardevice();
            goto start;
        case '1' : col=1;
            break;
        case '2' : col=2;
            break;
        case '3' : col=3;
            break;
        case '4' : col=4;
            break;
        case '5' : col=5;
            break;
        case '6' : col=6;
            break;
        case '7' : col=7;
            break;
        case '8' : col=8;
            break;
        case '9' : col=9;
            break;
        case '0' : col=6;
            break;
    }
    setcolor(col);
    goto label1;
}

No comments:

Post a Comment