Tuesday, 24 March 2015

Simple Bezier curve

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>  
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm,i;
double t;
initgraph (&gd, &gm, "");
for (t = 0.0; t < 1.0; t += 0.0005)
 {
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2] + pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2] + pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
 setcolor(RED);
 for (i=0; i<3; i++)
  {
    line (x[i], y[i], x[i+1], y[i+1]);
  }
     
 getch();
 closegraph();
 return;
 }  
void main()
{
 int x[4], y[4]; int i;
   printf ("Enter the x- and y-coordinates of the four control points.\n");
 for (i=0; i<4; i++)
  {
      scanf ("%d%d", &x[i], &y[i]);
  }
 bezier (x, y);
}

Monday, 23 March 2015

Bresenham's Circle Drawing algo

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

void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc,yc;

initgraph(&gd,&gm,"C:\\TC\\BGI");
cleardevice();


printf("Enter the radius ");
scanf("%d",&r);
printf("\n Enter the centre of circle");
scanf("%d%d",&xc,&yc);
line(320,0,320,480);
line(0,240,640,240);

x=0;
y=r;
putpixel(xc+x,yc-y,1);

p=3-(2*r);

for(x=0;x<=y;x++)
{
if (p<0)
{
y=y;
p=(p+(4*x)+6);
}
else
{
y=y-1;

p=p+((4*(x-y)+10));
}

putpixel(320+xc+x,240-yc-y,1);
putpixel(320+xc-x,240-yc-y,2);
putpixel(320+xc+x,240-yc+y,3);
putpixel(320+xc-x,240-yc+y,4);
putpixel(320+xc+y,240-yc-x,5);
putpixel(320+xc-y,240-yc-x,6);
putpixel(320+xc+y,240-yc+x,7);
putpixel(320+xc-y,240-yc+x,8);
sleep(1);

}
getch();
closegraph();
}

Tuesday, 17 March 2015

How to install Libgraph on UBUNTU

●This project is not part of the GNU Project. libgraph is an implementation of the TurboC graphics API (graphics.h) on GNU/Linux using SDL. The library requires SDL for primitive graphics and SDL_image (to blit fonts). Functions for text display are based heavily on code "borrowed" from Karl Bartel's SFont library.
● The library is not very powerful or flexible. It is probably not suitable for use in production-quality applications. I see it more as a simple, easy-to-use 2D graphics interface - could be used for simple prototyping, visualization or studying graphics algorithms. It is simplified library to run graphics programs in C.
It works efficiently with open source C compiler gcc. It is required to add the sdl-libgraph library to current existing gcc
Prerequisite: Internet Connection

You need to install some basic packages. Open terminal and exicute the following commands on terminal.
1) sudo apt-get update 
                 To update your basic packages
2) sudo apt-get install build-essential
                 For installing essential packages.
3) sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev
                 For installing lib packages
4) Now, download libgraph from: 
(http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz). Then copy the file libgraph-1.0.2.tar.gz to our home folder. Right click on the file and select Extract here.
5) Open a terminal and run the following commands, one by one.
cd libgraph-1.0.2
./configure
sudo make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib+

Now you are ready to compile your program!
● Write your program using any editor and save.
● Open the terminal for the specified folder and then run with:
gcc MyProg.c -lgraph (for C program)
g++ MyProg.cpp -lgraph (for C++ program)
./a.out
● Once all this installation process is done, you don't need to follow this process again and again. Just
compile and execute the program. 

Monday, 16 March 2015

Polygon Filling with Non recursive

#include<stdio.h>
#include<graphics.h>
struct stack
{
 long int x;
 long int y;
 struct stack *next;
}*p,*temp,*head=NULL;

typedef struct stack t1;
void push(int,int);
void pop();
int xf,yf;
void fill4nonrec(int,int,int,int);
int main()
{
 int gd=DETECT,gm,xc,yc,rx,ry,r,n,choice,ch,newcolor;
 int i,ax[200],ay[200];
 printf("\nEnter the fill color ");
 scanf("%d",&newcolor);
 printf("\nEnter the no of sides ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
     printf("\nEnter the %d co=ordinates ",i+1);
     scanf("%d%d",&ax[i],&ay[i]);
    }
    printf("\nEnter the inner point ");
    scanf("%d%d",&xf,&yf);
    initgraph(&gd,&gm,"");
    for(i=0;i<n;i++)
     line(ax[i],ay[i],ax[(i+1)%n],ay[(i+1)%n]);
fill4nonrec(xf,yf,newcolor,15);
getch();
return 0;
}

void fill4nonrec(int x,int y,int new1,int bdry)
{
 if(getpixel(x,y)==bdry)
 return;
 push(x,y);
 putpixel(x,y,new1);
 while(head!=NULL)
 {
  pop();
  x=p->x;
  y=p->y;
  if((getpixel(x+1,y)!=bdry)&&(getpixel(x+1,y)!=new1))
  {
   putpixel(x+1,y,new1);
   push(x+1,y);
  }
  if((getpixel(x-1,y)!=bdry)&&(getpixel(x-1,y)!=new1))
  {
   putpixel(x-1,y,new1);
   push(x-1,y);
  }
  if((getpixel(x,y+1)!=bdry)&&(getpixel(x,y+1)!=new1))
  {
   putpixel(x,y+1,new1);
   push(x,y+1);
  }
  if((getpixel(x,y-1)!=bdry)&&(getpixel(x,y-1)!=new1))
  {
   putpixel(x,y-1,new1);
   push(x,y-1);
  }
 }
}
void push(int x1,int y1)
{
 temp=(t1 *)malloc(sizeof(t1));
 temp->x=x1;
 temp->y=y1;
 if(head==NULL)
 {
  head=temp;
  temp->next=NULL;
 }
 else
 {
  temp->next=head;
  head=temp;
 }
}
void pop()
{
 p=head;
 head=head->next;
}

Reflection about X-axis, Y-axis, XY-axis

#include<graphics.h>
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int gd=DETECT,gm,ch;
int a,b[20],i,c[20],d[20],e[20];
void reflx();
void refly();
void reflxy();
void quad();

int main()
{
cout<<"enter no points of poly";
cin>>a;
cout<<"\n enter no of cordinates of polygon";
for (i=0;i<a*2;i++)
{
cin>>b[i];
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240-b[i];
}
c[i]=c[0];
c[i+1]=c[1];
cout<<"\n please enter choice 1:Reflection about X 2:Reflection about Y 3:Reflection about X and Y ";
cin>>ch;

switch(ch)
{
case 1:
quad();
reflx();
break;
case 2:
quad();
refly();
break;

case 3:
quad();
reflxy();
break;
}
getch();
closegraph();
return 0;
}

void quad()
{
initgraph(&gd,&gm,NULL);
line(0,240,640,240);
line(320,0,320,480);
drawpoly(a+1,c);
}
void reflx()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
c[i]=b[i]+320;
else
c[i]=240+b[i];
}
c[2*a]=c[0];
c[2*a+1]=c[1];
drawpoly(a+1,c);
}

void refly()
{
 for (i=0;i<a*2;i++)
{
if (i%2==0)
d[i]=(b[i]*-1)+320;
else
d[i]=240-b[i];
}
d[2*a]=d[0];
d[2*a+1]=d[1];
drawpoly(a+1,d);
 getch();
}

void reflxy()
{
 setcolor(3);
 for(i=0;i<a*2;i++)
{
e[i]=320-b[i];
i++;
e[i]=240+b[i];
}
e[2*a]=e[0];
e[2*a+1]=e[1];
drawpoly(a+1,e);
}