Showing posts with label computer graphics. Show all posts
Showing posts with label computer graphics. Show all posts

Saturday, September 1, 2007

tic tac toe (tic-tac-toe) in C

tic tac toe in C
============


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


int rowcol[9]={1,2,3,4,5,6,7,8,9};

void main(){

int gdriver,gmode,errorcode;
int is;
int i,temp;
int bool;
char ch[2];
bool=0;
gdriver=EGA;
gmode=EGAHI;
/*initialize the graphics driver*/
initgraph(&gdriver,&gmode,"");
/* error handling for graphics driver */
errorcode = graphresult();
if ( errorcode != grOk ) {
printf("Graphics Error \n %s",grapherrormsg(errorcode));
printf("\n Press any key to halt:");
getch();
exit(1);
}

setbkcolor(11);
setcolor(1);
settextstyle(0,0,2);
outtextxy(40,40,"WELCOME TO THE GAME OF SUNCHOKRI");
/*
for(is=1000;is>500;is--){
sound(is);
delay(250);
}
nosound();
*/

setcolor(12);

outtextxy(150,120,"BY ----");
/* preloader logic in c programming */
setcolor(1);
i=0;
while(i!=600){
preloader(i);
delay(300);
i++;
}


sleep(2);
cleardevice();
mainscreen();
positionofx();
chanceofplayer(bool);

/*if q is pressed quit the game */
while((*ch=getch())!='q') {
/*if n is pressed new game*/
if(*ch=='n'){
for(is=0;is<9;is++){
rowcol[is]=is+1;
}
bool=0;
cleardevice();
mainscreen();
positionofx();
chanceofplayer(bool);
}

temp=atoi(ch) ;
if (temp==0){
}
else {

if (rowcol[temp-1]==11 rowcol[temp-1]==22){
}
else {
if(bool==0){
rowcol[temp-1]=11;
}
else
{
rowcol[temp-1]=22;
}
cleardevice();
mainscreen();
positionofx();
if(bool==0){
bool=1;
}
else
{
bool=0;
}
chanceofplayer(bool);
}
}
}
/*
getch(); */

/*closing or deallocating graphic driver*/
closegraph();
}

/*main background screen*/
mainscreen() {
setbkcolor(12);
rectangle(10,10,620,250);
setcolor(14);
settextstyle(0,0,4);
outtextxy(35,300,"S U N C H O K R I");
setcolor(9);
settextstyle(0,0,2);
outtextxy(105,220,"Player 1 :11 Player 2 :22");
setcolor(15);
settextstyle(0,0,1);
outtextxy(0,0,"Author: ----SG");
/* small rectagle for sunchokri.. */
setcolor(WHITE);
rectangle(20,20,300,200);
rectangle(50,50,90,80);
rectangle(100,50,140,80);
rectangle(150,50,190,80);

rectangle(50,120,90,90);
rectangle(100,120,140,90);
rectangle(150,120,190,90);

rectangle(50,160,90,130);
rectangle(100,160,140,130);
rectangle(150,160,190,130);


outtextxy(399,60,"q TO QUIT GAME");
outtextxy(399,80,"n FOR NEW GAME");
}

/* logic of various functions in c */
preloader(int i) {
outtextxy(i,300,"");
}

positionofx(){
char x[10];
setcolor(YELLOW);

itoa(rowcol[8],x,10);
/*last row */
outtextxy(165,140,x);
itoa(rowcol[7],x,10);
outtextxy(115,140,x);
itoa(rowcol[6],x,10);
outtextxy(65,140,x);

/*second last row */
itoa(rowcol[5],x,10);
outtextxy(165,100,x);
itoa(rowcol[4],x,10);
outtextxy(115,100,x);
itoa(rowcol[3],x,10);
outtextxy(65,100,x);

/*first row */
itoa(rowcol[2],x,10);
outtextxy(165,60,x);
itoa(rowcol[1],x,10);
outtextxy(115,60,x);
itoa(rowcol[0],x,10);
outtextxy(65,60,x);
}

chanceofplayer(int bool1 ) {
char s[1];
if(bool1==0){
bool1=1; }
else
{
bool1=2; }

itoa(bool1,s,10);
setcolor(4);
outtextxy(33,33,"Turn of player : ");
outtextxy(173,33,s);

/* logic for winning the game */
if((rowcol[0]==11 && rowcol[1]==11 && rowcol[2]==11)(rowcol[3]==11 && rowcol[4]==11 && rowcol[5]==11)(rowcol[6]==11&&rowcol[7]==11&&rowcol[8]==11)(rowcol[0]==11&&rowcol[4]==11&&rowcol[8]==11)(rowcol[2]==11&&rowcol[4]==11&&rowcol[6]==11)){
outtextxy(399,100, "1 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==22&&rowcol[1]==22&&rowcol[2]==22)(rowcol[3]==22&&rowcol[4]==22&&rowcol[5]==22)(rowcol[6]==22&&rowcol[7]==22&&rowcol[8]==22)(rowcol[0]==22&&rowcol[4]==22&&rowcol[8]==22)(rowcol[2]==22&&rowcol[4]==22&&rowcol[6]==22)){
outtextxy(399,100, "2 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==22&&rowcol[3]==22&&rowcol[6]==22)(rowcol[1]==22&&rowcol[4]==22&&rowcol[7]==22)(rowcol[2]==22&&rowcol[5]==22&&rowcol[8]==22)){
outtextxy(399,100, "2 WINS");
sound(350);
delay(2000);
nosound();
}
else if((rowcol[0]==11&&rowcol[3]==11&&rowcol[6]==11)(rowcol[1]==11&&rowcol[4]==11&&rowcol[7]==11)(rowcol[2]==11&&rowcol[5]==11&&rowcol[8]==11)){
outtextxy(399,100, "1 WINS");
sound(350);
delay(2000);
nosound();
}
else{
outtextxy(399,100,"NO RESULT");
}
}

screen saver in C random circles lines - c program

screen saver in C random circles lines - c program

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


char ch;
/****************************************/
/* for line */
int lrandx, lrandy, lrandxx, lrandyy;
/* for rectangle */
int rrandx, rrandy, rrandxx, rrandyy;
/* for circle*/
int crandx, crandy, cradius;
/* random colour and shape generation */
int shape;
int col;

int gdriver,gmode,errorcode;
/*****************************************/
void main(){
gdriver=EGA;
gmode=EGAHI;
/*initialize the graphics driver*/
initgraph(&gdriver,&gmode,"");
/* error handling for graphics driver */
errorcode = graphresult();
if (errorcode != grOk ) {
printf("Graphics Error \n %s",grapherrormsg(errorcode));
printf("\n Press any key to halt:");
getch();
exit(1);
}
/* setting initiation of text and background color */
initialscreen();
while (!kbhit()) /* do nothing */ {
sleep(0.5);
mainscreen();
/*
outtextxy(133,44,"Sss");
*/
}
/*closing or deallocating graphic driver*/
closegraph();
}


/*main background screen*/
mainscreen() {
shape=rand()%3;
col=rand() % 14;
if (shape==0) {
rrandx=rand() % 600;
rrandy=rand() % 600;
rrandxx=rand() % 800;
rrandyy=rand() % 800;
setcolor(col);
rectangle(rrandx, rrandy, rrandxx, rrandyy);
}
else if (shape==1) {
lrandx=rand() % 200;
lrandy=rand() % 200;
lrandxx=rand() % 200;
lrandyy=rand() % 200;
setcolor(col);
line(lrandx, lrandy, lrandxx, lrandyy);
}
else {
crandx=rand() % 600;
crandy=rand() % 600;
cradius=rand() % 100;
setcolor(col);
circle(crandx, crandy, cradius);
}
return 0;
}

initialscreen() {
setbkcolor(4);
setcolor(1);
settextstyle(0,0,2);
outtextxy(40,40,"SCREEN SAVER...");
setcolor(11);
outtextxy(150,120,"---- computer bapu");
sleep(2);
cleardevice();
return 0;
}

character generation in C - program

Character generation in C - program

#include <graphics.h>
#include <conio.h>
#include <math.h>
void generatecharback();
void generatechar(char);
void main()
{
int gdriver, gmode;

gdriver=DETECT;
gmode=EGAHI;
initgraph(&gdriver,&gmode,"");
setbkcolor(4);
generatecharback();
generatechar('B');
outtextxy(203, 200, "char generation");
getch();
closegraph();
}

void generatecharback(){
int i=0;
int j=0;
for(i=0;i<=7;i++){
for(j=0;j<=7;j++){
putpixel(i+100,j+100,8);
}
}
}
void generatechar() {
int charray[1][65]={{1,1,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0}};
int i,j,k;
k=0;
for(i=0;i<=7;i++){
for(j=0;j<=7;j++){
if(charray[0][k]==1){
putpixel(j+100,i+100,WHITE);
}
k++;
}
}
}

Monday, August 27, 2007

PROGRAMME TO ENTER GRAPHICS MODE AND PRINT SOMETHING

PROGRAMME TO ENTER GRAPHICS MODE AND PRINT SOMETHING

#include<stdio.h>
#include<graphics.h>
main()
{
int ga=DETECT,gm;
initgraph(&ga,&gm," ");
settextstyle(0,0,100);
outtextxy(70,150,"PRAGATI PRAKASHAN");
getch();
closegraph();
}

Demonstrate use of mouse in C (assembly)

Demonstrate use of mouse in C (assembly)

/* PROGRAMME TO DEMONSTRATE USE OF MOUSE IN C LANGUAGE */
/* FORMING OUR OWN CURSOR */

#include<stdio.h>
#include<graphics.h>
#include<dos.h>
union REGS inregs;
struct SREGS sinregs;
static int mask[]={
0xe1ff,
0xe1ff,
0xe1ff,
0xe1ff,
0xe1ff,
0xe000,
0xe000,
0xe000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x0000,
0x1e00,
0x1200,
0x1200,
0x1200,
0x13ff,
0x1249,
0x1249,
0xf249,
0x9001,
0x9001,
0x9001,
0x8001,
0x8001,
0x8001,
0xffff
};
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
inregs.x.ax=0;
int86(0x33,&inregs,&inregs);
inregs.x.ax=1;
int86(0x33,&inregs,&inregs);
inregs.x.ax=9;
inregs.x.bx=5;
inregs.x.cx=0;
inregs.x.dx=(int)mask;
segread(&sinregs);
sinregs.es=sinregs.ds;
int86x(0x33,&inregs,&inregs,&sinregs);
getch();
closegraph();
}

Saturday, August 25, 2007

point clipping algorithm in C

point clipping algorithm in C


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

void initgraph1() {
int gd, gm;

gd=DETECT;
initgraph(&gd,&gm,"");
}

void main() {
int xmin, xmax, ymin, ymax;
int x[20],y[20],n,i;
clrscr();
printf("\tProgram for point clipping\n");
printf("\nEnter xmin, xmax, ymin, ymax");

scanf("\n %d", &xmin);
scanf("\n %d", &xmax);
scanf("\n %d", &ymin);
scanf("\n %d", &ymax);
printf("\n Enter no of point to be clipped");
scanf("\n %d",&n);
for(i=0;i<n;i++){
printf("Enter x for point %d :",i+1);
scanf("%d",&x[i]);
printf("\nEnter y for point %d :",i+1);
scanf("%d",&y[i]);
}

initgraph1();
/*before clipping */
outtextxy(0,0, "BEFORE CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
putpixel(x[i],y[i],1);
}
outtextxy(50,50, "presss a key to continue");
getch();
/*after clipping */
cleardevice();
outtextxy(0,0, "AFTER CLIPPING");
rectangle(xmin, ymin, xmax, ymax);
for(i=0;i<n;i++) {
if ((x[i] >= xmin && x[i] <=xmax) ( y[i] >=ymin && y[i] <=ymax)) {
putpixel(x[i],y[i],1);

}
else {
/*clipping */

}
}

getch();
closegraph();

}

textclip / lineclip /polygon clip program in C

textclip / lineclip/ polygon clip program in C
================= =========== =========

#include <graphics.h>
#include <conio.h>
#include <string.h>

#define MIN 100
#define MAX 300
#define STRINGS 3
#define Char_Width 8
#define Char_Height 10

void draw_basic();
void text_clipping_1 ();
void text_clipping_2 ();

struct textstring
{
int x;
int y;
char string[10];
}s[STRINGS];

void main()
{
int gd=0, gm;
initgraph ( &gd, &gm, "c:\\tc\\bgi" );
draw_basic(); text_clipping_1(); getch();
draw_basic(); text_clipping_2(); getch();
closegraph();
}
void draw_basic()
{
int i;
s[0].x=77; s[0].y=175; strcpy(s[0].string,"HELLO");
s[1].x=50; s[1].y=50; strcpy(s[1].string,"HELLO");
s[2].x=150; s[2].y=150; strcpy(s[2].string,"HELLO");
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
outtextxy(s[i].x,s[i].y,s[i].string);
getch();
}
void text_clipping_1 ()
{
int i, length;
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
{ if ( (s[i].y<MIN) (s[i].y>MAX) )
continue;
length = strlen(s[i].string) * Char_Width;
if ( (s[i].x<MIN) (s[i].x>MAX) )
continue;
outtextxy(s[i].x,s[i].y,s[i].string);
}
outtextxy(10,10,"Clipping 1");
}
void text_clipping_2 ()
{
int i, length;
cleardevice();
rectangle(MIN,MIN,MAX,MAX);
for ( i=0; i<STRINGS; i++ )
{ if ( (s[i].y<MIN) (s[i].y>MAX) )
continue;
length = strlen(s[i].string) * Char_Width;
if ( (s[i].x+length<MIN) (s[i].x>MAX) )
continue;
if ( (s[i].x+length>MIN) && (s[i].x<MIN) )
{ int words,ii,jj;
char newstring[10];
words = ( MIN - s[i].x ) / Char_Width;
for ( ii=(strlen(s[i].string)-words),jj=0; ii<=strlen(s[i].string); ii++)
newstring[jj++] = s[i].string[ii];
newstring[jj] = '\0';
outtextxy(MIN+1,s[i].y,newstring);
continue;
}
outtextxy(s[i].x,s[i].y,s[i].string);
}
outtextxy(10,10,"Clipping 2");
}


lineclip
======



#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <process.h>
#include <math.h>
#define ML 5
int Xmin,Xmax,Ymin,Ymax, ii[4][2]={0};
int l[5][4]={ {10,150,175,200},
{225,50,250,200},
{500,400,200,175},
{150,150,250,250},
{10,400,475,180}};
void dr();
void dl();
void cs();
void drawcsline(int, int, int, int, int);
void main()
{
int gd=0, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
dr(); getch();
dl(); getch();
cs(); getch();
closegraph();
}
void dr()
{ Xmin = 100; Xmax = 400; Ymin = 100; Ymax = 300;
rectangle(Xmin,Ymin,Xmax,Ymax);
}
void dl()
{ int i;
for(i=0; i<ML; i++)
line(l[i][0],l[i][1],l[i][2],l[i][3]);
}
void cs()
{ int i, j, x1, y1, x2, y2;
for(i=0; i<ML; i++)
{
x1=l[i][0]; y1=l[i][1]; x2=l[i][2]; y2=l[i][3];
drawcsline(x1,y1,x2,y2,i);
}
cleardevice(); dr(); dl(); getch();
}
void drawcsline(int x1, int y1, int x2, int y2, int iii)
{ int pt1, pt2, k, finalx, finaly, minx, miny, maxx, maxy, checkxy(int, int);
void setintersection(int, int, int, int);
minx = ( x1 > x2 ) ? x2 : x1;
miny = ( y1 > y2 ) ? y2 : y1;
maxx = ( x1 > x2 ) ? x1 : x2;
maxy = ( y1 > y2 ) ? y1 : y2;
pt1 = checkxy(x1,y1);
pt2 = checkxy(x2,y2);
if ( pt1 == 0 && pt2 == 0 ) //both inside
return;
if ( pt1 == 0 && pt2 != 0 ) //only pt1 inside
{ setintersection(x1,y1,x2,y2);
for ( int k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][2] = finalx;
l[iii][3] = finaly;
}
if ( pt1 != 0 && pt2 == 0 ) //only pt2 inside
{ setintersection(x1,y1,x2,y2);
for ( int k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][0] = finalx;
l[iii][1] = finaly;
}
if ( pt1 != 0 && pt2 != 0 ) //both outside
{ setintersection(x1,y1,x2,y2);
for ( k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
break;
else
continue;
}
l[iii][0] = finalx;
l[iii][1] = finaly;
for ( k=0; k<4; k++)
{ finalx = ii[k][0];
finaly = ii[k][1];
if ( ( finalx > Xmin && finalx < Xmax ) && ( finaly > Ymin && finaly < Ymax ) )
if ( ( finalx > minx && finalx < maxx ) && ( finaly > miny && finaly < maxy ) )
if ( finalx != l[iii][0] &&amp;amp; finaly != l[iii][1] )
break;
else
continue;
}
l[iii][2] = finalx;
l[iii][3] = finaly;

}
}
void setintersection(int x1, int y1, int x2, int y2)
{ int xxmin = 100, yymin = 100, xxmax = 400, yymax = 300;
float slope = (y2-y1)* 1.0 / (x2-x1);
ii[0][0] = x1 + ( yymin - y1 ) / slope;
ii[0][1] = yymin;
ii[1][0] = x1 + ( yymax - y1 ) / slope;
ii[1][1] = yymax;
ii[2][0] = xxmin;
ii[2][1] = y1 + slope * ( xxmin - x1 );
ii[3][0] = xxmax;
ii[3][1] = y1 + slope * ( xxmax - x1 );
}
int checkxy(int x, int y)
{ /* Check where x1,y1 lies Above Below Right Left
1 0 0 1
1001 1000 1010
--------------------
0001 0000 0010
-------------------
0101 0100 0110 */
int a,b,r,l;
a = ( y < Ymin ) ? 1 : 0;
b = ( y > Ymax ) ? 1 : 0;
r = ( x > Xmax ) ? 1 : 0;
l = ( x < Xmin ) ? 1 : 0;
return ( a*1000 + b*100 + r*10 + l );
}


Polygon Slip

//polygon clipping...
#include <graphics.h>
#include <conio.h>
#include <string.h>

int minx, miny, maxx, maxy;

int mypoly[22] = {200,50,250,100,300,100,250,150,250,200,200,150,
150,200,150,150,100,100,150,100,200,50};
int newpoly[50];
int cutx, cuty;

void clippoly(int, int []);
void set_intersection(char *, int, int, int, int);

void main()
{
int gd=0, gm;
initgraph (&gd, &gm,"c:\\tc\\bgi");

minx = 125; miny = 75; maxx=275; maxy=175;

rectangle(minx, miny, maxx, maxy);
getch();
drawpoly(11,mypoly);
getch();

clippoly(11, mypoly);
getch();

closegraph();
}

void clippoly(int edges, int mypoly[])
{
int temppoly[40];
int i, counter=0;
for (i=0; i<edges*2; i+=2)
{
// left clip...
if ( mypoly[i] > minx )
{
temppoly[counter++] = mypoly[i];
temppoly[counter++] = mypoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[edges*2-4],mypoly[edges*2-3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
else
{
set_intersection("left",mypoly[i-2],mypoly[i-1],mypoly[i],mypoly[i+1]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("left",mypoly[i],mypoly[i+1],mypoly[i+2],mypoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// right clip...
if ( temppoly[i] < maxx )
{
newpoly[counter++] = temppoly[i];
newpoly[counter++] = temppoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
else
{
set_intersection("right",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("right",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// bottom clip...
if ( newpoly[i+1] < maxy )
{
temppoly[counter++] = newpoly[i];
temppoly[counter++] = newpoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[edges*2-4],newpoly[edges*2-3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
else
{
set_intersection("bottom",newpoly[i-2],newpoly[i-1],newpoly[i],newpoly[i+1]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
set_intersection("bottom",newpoly[i],newpoly[i+1],newpoly[i+2],newpoly[i+3]);
temppoly[counter++] = cutx;
temppoly[counter++] = cuty;
}
}
}
edges = counter/2;
counter = 0;
for (i=0; i<edges*2; i+=2)
{
// top clip...
if ( temppoly[i+1] > miny )
{
newpoly[counter++] = temppoly[i];
newpoly[counter++] = temppoly[i+1];
}
else
{
if ( i==0 )
{
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[edges*2-4],temppoly[edges*2-3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
else
{
set_intersection("top",temppoly[i-2],temppoly[i-1],temppoly[i],temppoly[i+1]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
set_intersection("top",temppoly[i],temppoly[i+1],temppoly[i+2],temppoly[i+3]);
newpoly[counter++] = cutx;
newpoly[counter++] = cuty;
}
}
}
cleardevice();
rectangle(minx, miny, maxx, maxy);
setcolor(2);
drawpoly(counter/2, newpoly);
setcolor(15);
getch();
drawpoly(11,mypoly);
}

void set_intersection(char * clipside, int x1, int y1, int x2, int y2)
{
float slope;
if ( x1 != x2 )
slope = (y2-y1)*1.0/(x2-x1);

if ( strcmp(clipside,"left")==0 )
{ cutx = minx; cuty = y1 + slope * ( minx - x1 ); }
if ( strcmp(clipside,"right")==0 )
{ cutx = maxx; cuty = y1 + slope * ( maxx - x1 ); }
if ( strcmp(clipside,"bottom")==0 )
{
if ( x1 != x2 )
cutx = x1 + ( maxy - y1 ) / slope;
else
cutx = x1;
cuty = maxy;
}
if ( strcmp(clipside,"top")==0 )
{
if ( x1 != x2 )
cutx = x1 + ( miny - y1 ) / slope;
else
cutx = x1;
cuty = miny;
}
}

Transformation, Rotation and Scaling in C - computer graphics algorithm

/* Graphics program for the different types of transformations such as
1.Translation
2.Rotation
3.Scaling */


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

double finmat[3][3];
double identity[3][3];
int origmat[3][3]={{320,190,1},
{370,290,1},
{270,290,1}};
int transform[3][3];
int drawmat[8];

void multiply();
void setfin();
void transmat();

void main()
{
int gd=DETECT,gm,ch,x,y,i,j,k,errorcode,refx=320,refy=240;
double thita;
double costhita,sinthita;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
do
{
drawmat[0] = origmat[0][0];
drawmat[1] = origmat[0][1]; //drawing triangle
drawmat[2] = origmat[1][0];
drawmat[3] = origmat[1][1];
drawmat[4] = origmat[2][0];
drawmat[5] = origmat[2][1];
drawmat[6] = origmat[0][0]; // connecting
drawmat[7] = origmat[0][1]; // to first
transmat();

drawpoly(4,drawmat);
printf("\n");
printf("1.To Translate an Object\n");
printf("2.To Rotate an Object\n");
printf("3.To Scale an Object\n");
printf("4.To Terminate the Program\n");
printf("Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter Translation Value For x : ");
scanf("%d",&x);
printf("Enter Translation Value For y : ");
scanf("%d",&y);
identity[0][0] = 1;
identity[0][1] = 0;
identity[0][2] = x; // 1 0 x
identity[1][0] = 0; // 0 1 y
identity[1][1] = 1; // 0 0 1
identity[1][2] = y;

identity[2][0] = 0;
identity[2][1] = 0;
identity[2][2] = 1;
multiply();
setfin();
getch();
clrscr();
cleardevice();
break;
case 2:
printf("Enter The Rotation Parameter 0: ");
scanf("%lf",&thita);
costhita = cos(thita*3.1428/180);
sinthita = sin(thita*3.1428/180);
//refx=320 refy=240
identity[0][0] = costhita;
identity[0][1] = -sinthita;
identity[0][2] = refx * ( 1 - costhita ) + refy * sinthita;
identity[1][0] = sinthita;
identity[1][1] = costhita;

identity[1][2] = refy * ( 1 - costhita ) - refx * sinthita;
identity[2][0] = 0;
identity[2][1] = 0;
identity[2][2] = 1;
multiply();
setfin();
getch();
clrscr();
cleardevice();
break;
case 3:
printf("Enter Scaling Value For x : ");
scanf("%d",&x);
printf("Enter Scaling Value For y : ");
scanf("%d",&y);
identity[0][0] = x;
identity[0][1] = 0;
identity[0][2] = ( 1 - x ) * refx;
identity[1][0] = 0;
identity[1][1] = y;

identity[1][2] = ( 1 - y ) * refy;
identity[2][0] = 0;
identity[2][1] = 0;
identity[2][2] = 1;
multiply();
setfin();
getch();
clrscr();
cleardevice();
break;
case 4:
getch();
cleardevice();
closegraph();
restorecrtmode();
exit(0);
default:
printf("Select your choice!\n");
getch();
break;
}
}while(ch!=4);
}

void multiply()
{
int i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
finmat[i][j]=0;
for(k=0;k<3;k++)
{
finmat[i][j] += identity[i][k] * transform[k][j];
}
}
}
}
void setfin()
{
drawmat[0] = int(finmat[0][0]);
drawmat[1] = int(finmat[1][0]);
drawmat[2] = int(finmat[0][1]); // 2x3
drawmat[3] = int(finmat[1][1]); // 0,0 0,1 0,2
drawmat[4] = int(finmat[0][2]); // 1,0 1,1 1,2
drawmat[5] = int(finmat[1][2]);
drawmat[6] = int(finmat[0][0]);
drawmat[7] = int(finmat[1][0]);
drawpoly(4,drawmat);
}
void transmat()
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
transform[j][i] = origmat[i][j];
}

program for shearing of object in C

/* program for shearing of object */

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

float matrix[3][3];
float Xref=200,Yref=200;

void draw_poly(float array[][2],int n)
{
int i;
for(i=0;i<n-1;i++)
line(array[i][0],array[i][1],array[i+1][0],array[i+1][1]);

line(array[i][0],array[i][1],array[0][0],array[0][1]);
}

void transpoint(float array[][2],int n)
{
float p[10][2],x,y;
int i;

for(i=0;i<n;i++)
{
x=matrix[0][0]*array[i][0]+matrix[0][1]*array[i][1]+matrix[0][2];
y=matrix[1][0]*array[i][0]+matrix[1][1]*array[i][1]+matrix[1][2];

p[i][0]=x;
p[i][1]=y;
}

draw_poly(p,n);
}

void translate(float shx,float shy)
{
matrix[0][0]=1;
matrix[0][1]=shx;
matrix[0][2]=-shx*Yref;
matrix[1][0]=shy;
matrix[1][1]=1;
matrix[1][2]=-shy*Xref;
matrix[2][0]=0;
matrix[2][1]=0;
matrix[2][2]=1;
}

void main()
{

int gd = DETECT, gm,n=4,i;
float pts[4][2]={{100.0,200.0},{200.0,200.0},{200.0,50.0},{100.0,50.0}},tx,ty;
initgraph(&gd, &gm, "c:\\tc\\bgi\\");
draw_poly(pts,n);
printf("\n\nEnter the 'shx' shy:");
scanf("%f%f",&tx,&ty);

translate(tx,ty);
transpoint(pts,n);
getch();
}

program for transtlation polygon using C

/* program for transtlation polygon */

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

float matrix[3][3];

void draw_poly(float array[][2],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(array[i][0],array[i][1],array[i+1][0],array[i+1][1]);
}

line(array[i][0],array[i][1],array[0][0],array[0][1]);
}

void transpoint(float array[][2],int n)
{
float p[3][2],x,y;
int i;

for(i=0;i<n;i++)
{
x=matrix[0][0]*array[i][0] + matrix[0][1]*array[i][1] + matrix[0][2];
y=matrix[1][0]*array[i][0] + matrix[1][1]*array[i][1] + matrix[1][2];
p[i][0]=x;
p[i][1]=y;
}
draw_poly(p,n);
}

void translate(float tx,float ty)
{
matrix[0][0]=1;
matrix[0][1]=0;
matrix[0][2]=tx;
matrix[1][0]=0;
matrix[1][1]=1;
matrix[1][2]=ty;
matrix[2][0]=0; //
matrix[2][1]=0; //not used in traslation
matrix[2][2]=1; //
}

void main()
{

int gd = DETECT, gm,n,i,j;

float pts[10][2];//={{320,50},{160,150},{480,150}}
float tx,ty;

initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter no of edges");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter X Of edge %d ",i+1);
scanf("%f",&pts[i][0]);
printf("Enter Y of edge %d ",i+1);
scanf("%f",&pts[i][1]);
}
draw_poly(pts,n);
printf("Enter the 'tx' and 'ty' :");
scanf("%f %f",&tx,&ty);
translate(tx,ty);
transpoint(pts,n);
getch();
cleardevice();
}

BOUNDARY-FILL algorithm in C

BOUNDARY-FILL algorithm in C

/*Graphics algorithm to impliment the BOUNDARY-FILL-4 Algorithm */


#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void boundfill4(int x,int y,int fill,int boundary);
void main()
{
int arr[10][2],n,x,y;
printf("Enter the number of edges : ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the X and Y position for %d edge : ",i+1);
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
printf("Enter X and Y co-ordinates of the point from where you want \n");
printf("to start BOUNDARY-FILL : ");
scanf("%d %d",&x,&y);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawpolygon(n,arr,RED);
getch();
boundfill4(x,y,BLUE,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
void boundfill4(int x,int y,int fill,int boundary) //boundary=RED
{ //fill=BLUE
int current;
current = getpixel(x,y);
if( (current != boundary) && (current != fill) )
{
delay(2);
putpixel(x,y,fill);
boundfill4(x+1,y,fill,boundary);
boundfill4(x-1,y,fill,boundary);
boundfill4(x,y+1,fill,boundary);
boundfill4(x,y-1,fill,boundary);
}
}




/*Graphics algorithm to impliment the BOUNDARY-FILL-8 Algorithm */

#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundfill8(int x,int y,int fill,int boundary);
void main()
{
int r,x1,y1,x2,y2;
printf("Enter Left and Top co-ordinates to draw a SQUARE : ");
scanf("%d %d",&x1,&y1);
printf("Enter Right and Bottom co-ordinates to draw a SQUARE : ");
scanf("%d %d",&x2,&y2);

int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(x1,y1,x2,y2);
getch();
printf("Enter X and Y co-ordinates to Fill a circle using BOUNDRY-FILL : ");
scanf("%d %d",&x1,&y1);
boundfill8(x1,y1,BLUE,RED);
getch();
closegraph();
restorecrtmode();
}
void boundfill8(int x,int y,int fill,int boundary)
{
int current;
current = getpixel(x,y);
if( (current != boundary) && (current != fill) )
{

putpixel(x,y,fill);
delay(2);
boundfill8(x-1,y-1,fill,boundary);
boundfill8(x,y-1,fill,boundary);
boundfill8(x+1,y-1,fill,boundary);
boundfill8(x-1,y,fill,boundary);
boundfill8(x+1,y,fill,boundary);
boundfill8(x-1,y+1,fill,boundary);
boundfill8(x,y+1,fill,boundary);
boundfill8(x+1,y+1,fill,boundary);

}
}

PROGRAM FOR DRAWING A CIRCLE USING MIDPOINT ALGORITHM

/*PROGRAM FOR DRAWING A CIRCLE USING MIDPOINT ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>

void main()
{
int xc,yc,dx,dy,i,k,r,p;
float xinc,yinc,x,y;
void plotpoint(int,int,int,int);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

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

cleardevice();

cout<<"ENTER THE VALUE OF COORDINATES OF THE CENTER OF THE CIRCLE\n";
cout<<"ENTER THE X COORDINATE"<<endl;
cin>>xc;
cout<<"ENTER THE Y COORDINATE"<<endl;
cin>>yc;
cout<<"ENTER THE RADIUS\n";
cin>>r;

cout<<"CIRCLE WITH GIVEN CENTER AND RADIUS IS "<<endl;
x=0;
y=r;
plotpoint(xc,yc,x,y);
p=1-r;

while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
plotpoint(xc,yc,x,y);
}

getch();

}


void plotpoint(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,5);
putpixel(xc-x,yc+y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}

Circle, line, Polygon - C++ program

Circle, line, Polygon - C++ program
============================



Circle
=========


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xcent=100,ycent=100,radius=100;
void plotpixel(int,int);
void main()
{
int gd=0,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");

int x=0,y=radius,p=0;
plotpixel(x,y);
p=1-radius;
while(x<y)
{
if(p<0)
{
x = x+1;
p = (p+2) * (x+1);
}
else
{
x = x+1;
y=y-1;
p= (p+2) * (x-y) +1;
}
plotpixel(x, y);
}
getch();
}
void plotpixel(int x, int y)
{
putpixel(xcent+x, ycent+y, 15);
putpixel(xcent-x, ycent+y, 15);
putpixel(xcent+x, ycent-y, 15);
putpixel(xcent-x, ycent-y, 15);
putpixel(xcent+y, ycent+x, 15);
putpixel(xcent-y, ycent+x, 15);
putpixel(xcent+y, ycent-x, 15);
putpixel(xcent-y, ycent-y, 15);
}

Line
======


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

void line1(int,int,int,int);
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

setcolor(getmaxcolor());
line1(5, 20, 145, 20);
getch();
closegraph();
return 0;
}


void line1(int x1,int y1,int x2,int y2)
{
int dx=x2-x1;
if(dx<0)
dx=-dx;
int dy=y2-y1;
if(dy<0)
dy=-dy;
int i,j;

for( i=0, j=0; i<=dx j<=dy; i++,j++)
{
putpixel(x1+i,y1+j,15);
}

}


Polygon
========


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void main()
{
int arr[10][2],n;
printf("Enter the number of edges : ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the X and Y position for %d edge : ",i+1);
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawpolygon(n,arr,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}

PROGRAM FOR DRAWING A DOTTED LINE AND DASHED LINE USING BRESENHAM'S ALGORITHM

/*PROGRAM FOR DRAWING A DOTTED LINE USING BRESENHAM'S ALGORITHM*/

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

void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

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

setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;

cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);

dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;

if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);

m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;

}
else
{
x=x2;
y=y2;

}


putpixel(x,y,1);

while(x < xend)
{
x=x+1;
if((cnt/2)%3 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();

}


/*PROGRAM FOR DRAWING A DASHED LINE USING BRESENHAM'S ALGORITHM*/

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

void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

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

setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;

cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);

dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;

if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);

m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;

}
else
{
x=x2;
y=y2;

}


putpixel(x,y,1);

while(x < xend)
{
x=x+1;
if((cnt/5)%2 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();

}




DDA LINE , BRESENHAM'S ALGORITHM

DDA LINE , BRESENHAM'S ALGORITHM

/*PROGRAM FOR DRAWING A LINE USING DDA ALGORITHM*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>

void main()
{
int x1,x2,y1,y2,dx,dy,i,k;
float xinc,yinc,x,y;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

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

cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
putpixel(x1,y1,1);
putpixel(x2,y2,1);
dx=x2-x1;
dy=y2-y1;

if(abs(dx) > abs(dy))
k=abs(dx);
else
k=abs(dy);
xinc=1.0*dx/k;
yinc=1.0*dy/k;
x=x2*1.0;
y=y2*1.0;
putpixel(round(x),round(y),1);

cleardevice();
cout<<"LINE GENERATED FROM THE GIVEN POINTS IS : ";
setbkcolor(6);
for(i=1;i<=k;i++)
{
x+=xinc;
y+=yinc;
putpixel(round(x),round(y),i);
}
getch();
}

int round(float p)
{
if (p-int(p)<0.5)
return floor(p);
else
return ceil(p);
}


/*PROGRAM FOR DRAWING A LINE USING BRESENHAM'S ALGORITHM*/

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

void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

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

setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;

cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();


dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;

if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);

m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;

}
else
{
x=x2;
y=y2;

}


putpixel(x,y,1);

while(x < xend)
{
x=x+1;
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();

}



Curve Fitting in C (exponent,Hyperbola,Straight line or regression line)

Curve Fitting in C
==============
(exponent,Hyperbola,Straight line or regression line)


Exponent
========


#include <stdio.h>
#include <math.h>
#include <conio.h>
#define size 10
void main ()
{
float a,b,av,d;
//double e = 2.7128128;
void reg (float,float);
clrscr ();
printf("\n\tFitting an Exponential Curve\n");
reg (av,a);
a = (float)exp(av);
//b = -a;
fflush(stdin);
getch();
}

void reg(float a0,float a1)
{
int n,i;
float xsum=0,ysum=0,xsqsum=0,xysum=0,denom;
float x[size],y[size];
printf("\n\nEnter the number of candidate values available(n): ");
scanf("%d",&n);
printf("\n\nEnter the available coordinate of the line:\n");
for (i=0;i<n;i++)
{
printf("\nPoint %2d x: ",i+1);
fflush(stdin);
scanf("%f",&x[i]);
printf("y: ");
fflush (stdin);
scanf("%f",&y[i]);
ysum += y[i];
xysum += x[i]*y[i];
xsqsum += x[i]*x[i];
}
printf("\n\t\tMethod of Linear Regression:\n");
printf("--------------------------------------------\n");
printf("\n\tINDEPENDENT DEPENDENT");
printf("\n\tvariable variable");
printf("\n--------------------------------------------\n");
for (i=0;i<n;i++)
printf("\n\t%7.4f %7.4f %7.4f %7.4f",x[i],y[i],x[i]*x[i],x[i]*y[i]);
printf("\nSUMs: %7.4f" " %7.4f" " %7.4f" " %7.4f",xsum,ysum,xsqsum,xysum);
printf("\n--------------------------------------------\n");
a1 = (((float)n+xysum)-(xsum+ysum))/denom;
return;
}


Hyperbola
=========


#include <stdio.h>
#include <conio.h>
#define max 10
#define EPS 0.000001
void main ()
{
int i,n;
float x[max],y[max],sa,sx=0.0,sxx=0.0,sdy=0.0,denom,a,b,sdxy=0.0;
clrscr ();
printf("\t\tHyperbola Regression\n");
printf("Enter total number of data values: ");
scanf("%d",&n);
printf("\nEnter x and y values:\n");
for (i=0;i<n;i++)
{
printf("x[%d]: ",i+1);
scanf("%f",&x[i]);
printf("y[%d]: ",i+1);
scanf("%f",&y[i]);
}
for(i=0;i<n;i++)
{
sx += x[i];
sxx += x[i]*x[i];
sdxy += x[i]/y[i];
sdy += 1/y[i];
}
denom = n*sxx - sx*sx;
if (fabs(denom) > EPS)
{
a = ((n*sdxy) - (sx*sdy))/denom;
b = ((sdy*sxx) - (sx*sdxy))/denom;
printf("\nHyperbola REgression Line y=1/a,bx,f=1/n,f=bx+a\n");
printf("a = %f\n",a);
printf("b = %f\n",b);
}
else
printf("No Solution");
getch ();
}

Straight line or regression line
=========================


#include <stdio.h>
#include <conio.h>
#define MAX 20
void main ()
{
float Sx=0,Sy=0,Sxy=0,Sx2=0;
float denom,a2,a1,x[MAX],y[MAX];
int i,n;
clrscr ();
printf("Enter total number of data: ");
scanf("%d",&n);
printf("Enter x and y one by one for %d times\n",n);
for (i=0;i<n;i++)
{
printf("\nx[%d]: ",i+1);
scanf("%f",&x[i]);
printf("y[%d]: ",i+1);
scanf("%f",&y[i]);
}
for (i=0;i<n;i++)
{
Sx = Sx + x[i];
Sx2 = Sx2 + x[i]*x[i];
Sy = Sy + y[i];
Sxy = Sxy + x[i]*y[i];
denom = n*Sx2 - Sx*Sx;
a2 = (n*Sxy - Sx*Sy)/denom;
a1 = (Sy*Sx2 - Sx*Sxy)/denom;
}
printf("\nRegression Coefficients are as under:\n");
printf("\nSlop of Regression line: %f\n",a2);
printf("\nIntercept for the line: %f",a1);
getch ();
}

Show Mouse Using C - snippet

Show Mouse Using C- snippet
========================

#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
initmouse();
void showmouseptr();

union REGS i,o;
struct SREGS s;
int button,x,y;
initmouse()
{
i.x.ax = 0;
int86( 0x33 , &i , &o );
return(o.x.ax);
}

void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void getmousepos(int *button, int *x, int *y)
{
union REGS i,o;
i.x.ax=3;
int86( 0x33 , &i, &o);

*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}

simple mouse programming example in C

simple mouse programming example in C

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

initmouse();
void initgraph1();
void getmousepos();
union REGS i,o;

int button,x,y;
int px, py;
initmouse()
{
i.x.ax = 0;
int86( 0x33 , &i , &amp;o );
/* return(o.x.ax);
*/
}

void showmouseptr()
{
i.x.ax=1;
int86(0x33,&i,&amp;o);
}
void initgraph1() {
int g, m;
g=EGA;
m=EGA;
initgraph(&g,&m,"c\\tc");
}

void getmousepos(int *button,int *x, int *y)
{
/*union REGS i,o;
*/
i.x.ax=3;
int86( 0x33 , &i, &amp;o);

*button = o.x.bx&3;
*x = o.x.cx;
*y = o.x.dx;
if(*button &1==1) {
/*printf("%d",*button);
printf("\n%d",*x);
printf("\n%d",*y);

putpixel(*x,*y,1);
*/
line(*x,*y,px,py);
px=*x;
py=*y;
}
}

void main() {
clrscr();
initmouse();
/*showmouseptr();
*/
initgraph1();
showmouseptr();

while(!kbhit()) {
getmousepos(&button, &amp;x,&y);

}
getch();
}