Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.
Showing posts with label Computer Graphics. Show all posts
Showing posts with label Computer Graphics. Show all posts

Thursday, 20 June 2013

Character generation in C using Bitmap method

Given below is a C program for character generation using Bitmap method.

Links that you may find important :

Computer Graphics And Multimedia By A.P.Godse, D.A.Godse

Code :
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
    int i,j,k,x,y;
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    /* ch1 ch2 ch3 ch4 are character arrays that display alphabets */
    int ch1[][10]={ {1,1,1,1,1,1,1,1,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,0,0,0,1,1,0,0,0,0},
                    {0,1,1,0,1,1,0,0,0,0},
                    {0,1,1,0,1,1,0,0,0,0},
                    {0,0,1,1,1,0,0,0,0,0}};
    int ch2[][10]={ {0,0,0,1,1,1,1,0,0,0},
                    {0,0,1,1,1,1,1,1,0,0},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {0,0,1,1,1,1,1,1,0,0},
                    {0,0,0,1,1,1,1,0,0,0}};
    int ch3[][10]={ {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {1,1,1,1,1,1,1,1,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1},
                    {1,1,0,0,0,0,0,0,1,1}};
    int ch4[][10]={ {1,1,0,0,0,0,0,0,1,1},
                    {1,1,1,1,0,0,0,0,1,1},
                    {1,1,0,1,1,0,0,0,1,1},
                    {1,1,0,1,1,0,0,0,1,1},
                    {1,1,0,0,1,1,0,0,1,1},
                    {1,1,0,0,1,1,0,0,1,1},
                    {1,1,0,0,0,1,1,0,1,1},
                    {1,1,0,0,0,1,1,0,1,1},
                    {1,1,0,0,0,0,1,1,1,1},
                    {1,1,0,0,0,0,0,0,1,1}};
    initgraph(&gd,&gm,"D:\\TC\\BGI");//initialize graphic mode
    setbkcolor(LIGHTGRAY);//set color of background to darkgray
    for(k=0;k<4;k++)
    { 
        for(i=0;i<10;i++)
        {
            for(j=0;j<10;j++)
            {
                if(k==0)
                { 
                    if(ch1[i][j]==1)
                    putpixel(j+250,i+230,RED);
                }
                if(k==1)
                { 
                    if(ch2[i][j]==1)
                    putpixel(j+300,i+230,RED);
                }
                if(k==2)
                { 
                    if(ch3[i][j]==1)
                    putpixel(j+350,i+230,RED);
                }
                if(k==3)
                { 
                    if(ch4[i][j]==1)
                    putpixel(j+400,i+230,RED);
                }
            }
            delay(200);
        }
    }
    getch();
    closegraph();
}
Output :


Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Sunday, 16 June 2013

C Program for Bresenham’s Line Drawing Algorithm

Given below is a C program to draw a line using Bresenham’s Line Drawing Algorithm.

Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
void main() 
{
    int x1,x2,y1,y2;
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    initgraph(&gd,&gm,"d:\\tc\\bgi");//initialize graphic mode
    printf("Enter values of x1 and y1: ");
    scanf("%d %d",&x1,&y1);
    printf("Enter values of x2 and y2 : ");
    scanf("%d %d",&x2,&y2);
    draw(x1,y1,x2,y2);//call to function that draws the line
    getch();
}
void draw(int x1,int y1,int x2,int y2)
{
    float dx,dy,p;
    int i,x,y,xend;
    dx=x2-x1;
    dy=y2-y1;
    p=2*dy-dx;
    if(x1>x2)
    {
        x-x2;
        y=y2;
        xend=x1;
    }
    else
    {
        x=x1;
        y=y1;
        xend=x2;
    }
    putpixel(x,y,10);
    while(x<xend)
    {
        if(p<0)
        {
            x=x+1;
            putpixel(x,y,10);
            p=p+(2*dy);
        }
        else
        {
            x=x+1;
            y=y+1;
            putpixel(x,y,10);
            p=p+(2*dy)-(2*dx);
        }
    }
}
Output :


Note : In the above screenshot the line is hardly visible. The image above is a grayscale of original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Saturday, 15 June 2013

C Program for DDA (graphics algorithm)

Given below is a C program to draw a line using DDA algorithm.

Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int sign(int nd)
{
    if(nd>0)
        return +1;
    else if(nd<0)
        return -1;
    else
        return 0;
}
void main()
{
    int gd=DETECT,gm,i=1,j;//DETECT is macro defined in graphics.h
    int x1,x2,y1,y2,length;
    float dx,dy,ndx,ndy,x,y;
    initgraph(&gd,&gm,"d:\\tc\\bgi");//initialize graphic mode
    setbkcolor(DARKGRAY);//set the background color
    printf("Enter x1:");
    scanf("%d",&x1);
    printf("Enter y1:");
    scanf("%d",&y1);
    printf("Enter x2:");
    scanf("%d",&x2);
    printf("Enter y2:");
    scanf("%d",&y2);
    dx=x2-x1;
    dy=y2-y1;
    if(abs(dx)>=abs(dy))
        length=abs(dx);
    else
        length=abs(dy);
    ndx=dx/length;
    ndy=dy/length;
    x=x1+(0.5*sign(ndx));
    y=y1+(0.5*sign(ndy));
    while(i<=length)
    {
        putpixel(x,y,4);
        x=x+ndx;
        y=y+ndy;
        i=i+1;
    }
    getch();
    closegraph();
}
Output :


Note : In the above screenshot the line is hardly visible. The image above is a grayscale of original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

Monday, 10 June 2013

Draw a smiley in C

C program to draw a smiley in C using graphics.

Code :
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main()
{
    int gd=DETECT,gm;//DETECT is macro defined in graphics.h
    int color,pixel,maxx,maxy;
    initgraph(&gd,&gm,"C:\\TC\\BGI");//initialize graphic mode
    setbkcolor(DARKGRAY);//set the background color
    maxx=getmaxx();//get maximum value for x co-ordinate
    maxy=getmaxy();//get maximum value for y co-ordinate
    setcolor(YELLOW);//color for drawing shapes
    circle(maxx/2,maxy/2,20);//draw a circle
    setfillstyle(1,YELLOW);//the style to fill the area
    fillellipse(maxx/2,maxy/2,100,100);//fill the ellipse with color(face)
    pixel=getpixel(1,1);
    setfillstyle(1,pixel);
    setcolor(pixel);
    fillellipse(maxx/2-50,maxy/2-30,10,10);//fill the ellipse with color(eye)
    fillellipse(maxx/2+50,maxy/2-30,10,10);//fill the ellipse with color(eye)
    ellipse(maxx/2,maxy/2,220,320,60,60);//draw an ellipse(mouth)
    line(maxx/2,maxy/2-10,maxx/2,maxy/2+20);//draw a line(nose)
    getch();
    closegraph();//close graphic mode
}
Output :


Note : The image above is a graysace of the original image.

Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.