gurpreet:- write('Enter number'),nl,read(X),Z is X mod 10, X1 is X/10, R is (Z*10)+(X1),write(R),!.
CLOUD COMPUTING | DATA MINING | ARTIFICIAL INTELLIGENCE | APPLICATION PROGRAMMING IN C/C++/Java
Wednesday, May 4, 2011
PROGRAM TO REVERSE A NUMBER IN PROLOG
gurpreet:- write('Enter number'),nl,read(X),Z is X mod 10, X1 is X/10, R is (Z*10)+(X1),write(R),!.
PROGRAM TO REVERSE A NUMBER IN PROLOG
gurpreet:- write('Enter number'),nl,read(X),Z is X mod 10, X1 is X/10, R is (Z*10)+(X1),write(R),!.
PARSER IN PROLOG
% IT WORKS FOR THE PARTICULAR SENTENCE " The, dog, barked , at ,the,cow" (give it in list).
% you can write in your own facts to implements the rules given by me.
sentence(A,C):-nounphrase(A,B),verbphrase(B,C).
nounphrase(A,C):-article(A,B),noun(B,C).
verbphrase(A,C):-verb(A,B),prepositionphrase(B,C).
prepositionphrase(A,C):-preposition(A,B),nounphrase(B,C).
preposition([at|X],X).
article([a|X],X).
article([the|X],X).
noun([dog|X],X).
noun([cow|X],X).
noun([moon|X],X).
verb([barked|X],X).
verb([wrinked|X],X).
% you can write in your own facts to implements the rules given by me.
sentence(A,C):-nounphrase(A,B),verbphrase(B,C).
nounphrase(A,C):-article(A,B),noun(B,C).
verbphrase(A,C):-verb(A,B),prepositionphrase(B,C).
prepositionphrase(A,C):-preposition(A,B),nounphrase(B,C).
preposition([at|X],X).
article([a|X],X).
article([the|X],X).
noun([dog|X],X).
noun([cow|X],X).
noun([moon|X],X).
verb([barked|X],X).
verb([wrinked|X],X).
FIBONACCI SERIES IN PROLOG
fibonacci(0,1).
fibonacci(1,1).
fibonacci(A,B) :- A > 1, A1 is A - 1, A2 is A - 2, fibonacci(A1,B1), fibonacciA2,B2), B is B1 + B2.
fibonacci(1,1).
fibonacci(A,B) :- A > 1, A1 is A - 1, A2 is A - 2, fibonacci(A1,B1), fibonacciA2,B2), B is B1 + B2.
PROGRAM FOR THE INTERSECTION OF TWO LISTS
int([X|Y],Z,[X|D]):-member(X,Z),int(Y,Z,D).
int([X|Y],Z,D):- \+member(X,Z),int(Y,Z,D).
int([],Z,[]).
TOWER OF HANOI PROLOG IMPLEMENTATION
towerofhanoi(1,X,Y,_):- write(' move the disk from'),write(X),write('TO') ,write(Y),nl.
towerofhanoi(N,X,Y,Z):- N >1, M is N-1, towerofhanoi(M , X, Z,Y), towerofhanoi(1,X,Y,_),towerofhanoi(M,Z,Y,X).
PROGRAM TO REVERSE THE LIST
reverse_garry([],A,A).
reverse_garry([A|B],Z,W):- reverse_garry(B,[A|Z],W).
PROGRAM TO FIND THE PRODUCT OF ALL THE ELEMENTS IN LIST
pro([],1).
pro([H|T],P):- pro(T,HT), P is H*HT.
PROGRAM TO SEE THE PERMUTATIONS IN LIST ENTERED
garry(X,[X|R],R).
garry(X,[C|R],[C|S]):- garry(X,R,S).
permu_garry([],[]).
permu_garry(Z,[A|B]):- perm(B,W), garry(A,Z,W).
A PROLOG PROGRAM TO CALCULATE MAXIMUM AND MINIMUM IN LIST
maxlist([A],A).
maxlist([A|LIST],MAX):- maxlist(LIST,MAX0),(A>=MAX0, MAX=A ; A<MAX0, MAX=MAX0).
minlist([A],A).
minlist([A|LIST],MIN):- minlist(LIST,MIN0),(A=<MIN0, MIN=A ; A>MIN0, MIN=MIN0).
PROGRAM TO CALCULATE UNION OF 2 LISTS
gurpreet_union([X|Y],Z,W) :- member(X,Z), gurpreet_union(Y,Z,W).
gurpreet_union([X|Y],Z,[X|W]) :- \+ member(X,Z), gurpreet_union(Y,Z,W).
gurpreet_union([],Z,Z).
PROGRAM TO PRINT Kth ELEMENT FROM LIST
extractk(X,[X|_],1).
extractk(X,[_|T],K):- K>1,K1 is K-1, extractk(T,X,K1).
extractk(X,[_|T],K):- K>1,K1 is K-1, extractk(T,X,K1).
PROGRAM TO PRINT NUMBERS IN DESCENDING ORDER
print1(0):-write('0'),!.
print1(X):-write(X),nl,N is X-1,print1(N).
print1(X):-write(X),nl,N is X-1,print1(N).
PROGRAM TO ADD 10 ELEMENTS OF LIST
% IT WILL WORK FOR 10 ELEMNTS AND TO MAKE IT WORK FOR ANY NUMBER PLACE A %NUMBER IN PLACE OF 10 WHATR U WANT.
addlist(_,0,0).
addlist([H|T],C,X):-C1 is C-1,addlist(T,C1,X1),X is X1+H.
sum([H|T],X):-addlist([H|T],10,X).
PROGRAM TO ADD 2 NUMBERS IN PROLOG
match(10).
manage:-nl,write('Do u wanna enter another value:'),nl,write('Enter 10 for yes:'),nl,write('Press any key for no:'),read(K),match(K).
add:- write(' *** welcome to program ***'),nl, write('Enter the value of variable1:'),read(X),write('\t\t\t Enter the value of variable2:'),read(Y),Z is X+Y,nl, write( Z), manage.
Program to print 10 numebrs in ASCENDING ORDER
print_my(10):-write(10),!.
print_my(X):-write(X),nl,N is X+1,print_my(N).
print_my(X):-write(X),nl,N is X+1,print_my(N).
PROGRAM TO CALCULATE THE CUBE OF A NUMBER
% START WITH WRITING CUDE ON COMMOND PROMPT
match(10).
manage:-nl,write('Do u wanna enter another value:'),nl,write('Enter 10 for yes:'),nl,write('Press any key for no:'),read(K),match(K).
cube:- write(' *** welcome to program ***'),nl, write('Enter the value of variable1:'),read(X),Z is X**3,nl, write( Z), manage,cube.
match(10).
manage:-nl,write('Do u wanna enter another value:'),nl,write('Enter 10 for yes:'),nl,write('Press any key for no:'),read(K),match(K).
cube:- write(' *** welcome to program ***'),nl, write('Enter the value of variable1:'),read(X),Z is X**3,nl, write( Z), manage,cube.
PROGRAM TO SEE THE GREATEST COMMON DIVISIOR (GCD)
gcd(A, 0, A).
gcd(A, B, D) :- (A>B),(B>0),R is A mod B,gcd(B,R,D).
gcd(A, B, D) :- (A<B), gcd(B,A,D).
gcd(A, B, D) :- (A>B),(B>0),R is A mod B,gcd(B,R,D).
gcd(A, B, D) :- (A<B), gcd(B,A,D).
PROGRAM TO Check a number is PRIME OR NOT
div(X,A):- N is A*A , N=<X, X mod A =:=0.
div(X,A):- A<X, A1 is A+1,div(X,A1).
check_prime(X):- A is 2, X>1, \+div(X,A).
Monday, May 2, 2011
2D TRANSFORAMTIONS (TRANSLATION,SHEARING,ROTATION,SCALING) ---- MODIFIED CHECK IT .........
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void translating_line();
void translating_rectangle();
void scaling();
void rotation_line();
void rotation_rectangle();
void shearing();
int main()
{
int choice;
int gd=DETECT ,gm;
initgraph(&gd,&gm," ");
initwindow(1500,700);
while(1)
{
printf("\n\n\t\t\t Enter Into Main Menu");
printf("\n\n\t\t 1. For Translating A Line\n\t\t 2.For Translating A rectangle\n\t\t 3.For Scaling in x and y \n\t\t 4.For Rotating A Line\n\t\t 5.For Rotating a RECTangle\n\t\t6. Shearing\n\t\t Enter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
translating_line();
break;
case 2:
translating_rectangle();
break;
case 3:
scaling();
break;
case 4:
rotation_line();
break;
case 5:
rotation_rectangle();
break;
case 6:
shearing();
break;
default:
printf("\n\n ***** Invalid choice *****");
break;
}
}
getch();
closegraph();
return 0;
}
void translating_line()
{
int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(1000,200,"ORIGINAL LINE");
line(x1,y1,x2,y2);
// getch();
// cleardevice();
printf("\n\n Do u want to translate \n 1.for yes \n anything for no");
scanf("%d",&choice1);
if(choice1==1)
{
printf("\n\n --------------- Enter translation coordinates ---------");
printf("\n\n Enter tx:");
scanf("%d",&tx);
printf("\n\n Enter ty:");
scanf("%d",&ty);
xn=x1+tx;
yn=y1+ty;
xn1=x2+tx;
yn1=y2+ty;
// getch();
cleardevice();
outtextxy(1000,200,"AFTER TRANSLATION LINE");
line(xn,yn,xn1,yn1);
}
else
{
printf("\n\n ****** Your choice sir ji *****");
}
}
void translating_rectangle()
{
int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(1000,200,"ORIGINAL RECTANGLE");
rectangle(x1,y1,x2,y2);
// getch();
// cleardevice();
printf("\n\n Do u want to translate \n 1.for yes \n anything for no");
scanf("%d",&choice1);
if(choice1==1)
{
printf("\n\n --------------- Enter translation coordinates ---------");
printf("\n\n Enter tx:");
scanf("%d",&tx);
printf("\n\n Enter ty:");
scanf("%d",&ty);
xn=x1+tx;
yn=y1+ty;
xn1=x2+tx;
yn1=y2+ty;
// getch();
cleardevice();
outtextxy(1000,200,"AFTER TRANSLATION LINE");
rectangle(xn,yn,xn1,yn1);
}
else
{
printf("\n\n ****** Your choice sir ji *****");
}
}
void scaling()
{
int sx,sy,choice;
int xmin,xmax,ymin,ymax,xmid,ymid;
printf("\n\n\t\t Enter rectangle or square coordinates:");
printf("\n\n Enter xmin:");
scanf("%d",&xmin);
printf("\n\n Enter ymin:");
scanf("%d",&ymin);
printf("\n\n Enter xmax:");
scanf("%d",&xmax);
printf("\n\n Enter ymax:");
scanf("%d",&ymax);
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
xmid=xmin+xmax/2;
ymid=ymin+ymax/2;
getche();
xmin=xmin-xmid;
xmax=xmax-xmid;
ymin=ymin-ymid;
ymax=ymax-ymid;
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
printf("\n\n 1 for scaling in x and y");
scanf("%d",&choice);
if(choice==1)
{
printf("\n Enter sx:");
scanf("%d",&sx);
printf("\n Enter sy:");
scanf("%d",&sy);
xmin=xmin*sx;
xmax=xmax*sx;
ymin=ymin*sy;
ymax=ymax*sy;
}
outtextxy(300,200,"AFTER SCALING");
xmin=xmin+xmid;
xmax=xmax+xmid;
ymin=ymin+ymid;
ymax=ymax+ymid;
cleardevice();
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
}
void rotation_rectangle()
{
float my;
int x1,y1,x2,y2,x3,y3,x4,y4;
int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
// int temp,temp1;
printf("\n\n ----Enter Rectanlge Coordinates----");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(800,200,"RECTANGLE");
rectangle(x1,y1,x2,y2);
printf("\n\n Enter the Rotation Angle:");
scanf("%f",&my);
tempx=x1; // i have made fixed point as x1 and y1 you can make fixed acc to your choice
tempy=y1;
//x3=x2;
//y3=y2;
//x4=x1;
//y4=y2;
my= my*(3.14/180);
//printf("\n\n%d",my);
tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
tempx4=tempx+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
line(tempx1,tempy1,tempx2,tempy2);
// getch();
line(tempx2,tempy2,tempx3,tempy3);
//getch();
line(tempx3,tempy3,tempx4,tempy4);//getch();
line(tempx4,tempy4,tempx1,tempy1);
}
void rotation_line()
{
float my;
int x1,y1,x2,y2,x3,y3,x4,y4;
int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
// int temp,temp1;
printf("\n\n ----Enter line Coordinates----");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(800,200,"LINE");
line(x1,y1,x2,y2);
printf("\n\n Enter the Rotation Angle:");
scanf("%f",&my);
// rotating about the point x1 and y1
tempx=x1;
tempy=y1;
my= my*(3.14/180);
printf("\n\n%f",my);// my=angle
tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
//tempx4=tempy+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
//tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
line(tempx1,tempy1,tempx2,tempy2);
// getch();
line(tempx2,tempy2,tempx3,tempy3);
//getch();
//line(tempx3,tempy3,tempx4,tempy4);//getch();
//line(tempx4,tempy4,tempx1,tempy1);
}
void shearing()
{
int xa,ya,xb,yb,shx,choice,shy;
settextstyle(8,HORIZ_DIR,8);
cleardevice();
outtextxy(300,300," EVALUATION");
getche();
cleardevice();
printf("\n\n Enter xa:");
scanf("%d",&xa);
printf("\n\n Enter ya:");
scanf("%d",&ya);
printf("\n\n Enter xb:");
scanf("%d",&xb);
printf("\n\n Enter yb:");
scanf("%d",&yb);
rectangle(xa,ya,xb,yb);
printf("\n\t\t 1.For shearing in X direction\n\t\t 2.For Shearing In Y Direction\n\t\t Enter Your Choice:");
scanf("%d",&choice);
cleardevice();
if(choice==1)
{
printf("\n\n Enter shearing in x:");
scanf("%d",&shx);
outtextxy(600,200,"After Shearing");
line(xa,ya,xb,ya);
line(xa,ya,xa+shx,yb);
line(xb,ya,xb+shx,yb);
line(xa+shx,yb,xb+shx,yb);
}
else if (choice==2)
{
printf("\n\n Enter shearing in y:");
scanf("%d",­);
outtextxy(800,200,"After Shearing");
line(xa,ya,xb,ya+shy);
line(xa,ya,xa,yb);
line(xb,ya+shy,xb,yb+shy);
line(xa,yb,xb,yb+shy);
}
else
{
printf(" **** invalid choice ****");
}
}
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void translating_line();
void translating_rectangle();
void scaling();
void rotation_line();
void rotation_rectangle();
void shearing();
int main()
{
int choice;
int gd=DETECT ,gm;
initgraph(&gd,&gm," ");
initwindow(1500,700);
while(1)
{
printf("\n\n\t\t\t Enter Into Main Menu");
printf("\n\n\t\t 1. For Translating A Line\n\t\t 2.For Translating A rectangle\n\t\t 3.For Scaling in x and y \n\t\t 4.For Rotating A Line\n\t\t 5.For Rotating a RECTangle\n\t\t6. Shearing\n\t\t Enter Your Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
translating_line();
break;
case 2:
translating_rectangle();
break;
case 3:
scaling();
break;
case 4:
rotation_line();
break;
case 5:
rotation_rectangle();
break;
case 6:
shearing();
break;
default:
printf("\n\n ***** Invalid choice *****");
break;
}
}
getch();
closegraph();
return 0;
}
void translating_line()
{
int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(1000,200,"ORIGINAL LINE");
line(x1,y1,x2,y2);
// getch();
// cleardevice();
printf("\n\n Do u want to translate \n 1.for yes \n anything for no");
scanf("%d",&choice1);
if(choice1==1)
{
printf("\n\n --------------- Enter translation coordinates ---------");
printf("\n\n Enter tx:");
scanf("%d",&tx);
printf("\n\n Enter ty:");
scanf("%d",&ty);
xn=x1+tx;
yn=y1+ty;
xn1=x2+tx;
yn1=y2+ty;
// getch();
cleardevice();
outtextxy(1000,200,"AFTER TRANSLATION LINE");
line(xn,yn,xn1,yn1);
}
else
{
printf("\n\n ****** Your choice sir ji *****");
}
}
void translating_rectangle()
{
int x1,y1,x2,y2,tx,ty,xn,yn,xn1,yn1,choice1;
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(1000,200,"ORIGINAL RECTANGLE");
rectangle(x1,y1,x2,y2);
// getch();
// cleardevice();
printf("\n\n Do u want to translate \n 1.for yes \n anything for no");
scanf("%d",&choice1);
if(choice1==1)
{
printf("\n\n --------------- Enter translation coordinates ---------");
printf("\n\n Enter tx:");
scanf("%d",&tx);
printf("\n\n Enter ty:");
scanf("%d",&ty);
xn=x1+tx;
yn=y1+ty;
xn1=x2+tx;
yn1=y2+ty;
// getch();
cleardevice();
outtextxy(1000,200,"AFTER TRANSLATION LINE");
rectangle(xn,yn,xn1,yn1);
}
else
{
printf("\n\n ****** Your choice sir ji *****");
}
}
void scaling()
{
int sx,sy,choice;
int xmin,xmax,ymin,ymax,xmid,ymid;
printf("\n\n\t\t Enter rectangle or square coordinates:");
printf("\n\n Enter xmin:");
scanf("%d",&xmin);
printf("\n\n Enter ymin:");
scanf("%d",&ymin);
printf("\n\n Enter xmax:");
scanf("%d",&xmax);
printf("\n\n Enter ymax:");
scanf("%d",&ymax);
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
xmid=xmin+xmax/2;
ymid=ymin+ymax/2;
getche();
xmin=xmin-xmid;
xmax=xmax-xmid;
ymin=ymin-ymid;
ymax=ymax-ymid;
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
printf("\n\n 1 for scaling in x and y");
scanf("%d",&choice);
if(choice==1)
{
printf("\n Enter sx:");
scanf("%d",&sx);
printf("\n Enter sy:");
scanf("%d",&sy);
xmin=xmin*sx;
xmax=xmax*sx;
ymin=ymin*sy;
ymax=ymax*sy;
}
outtextxy(300,200,"AFTER SCALING");
xmin=xmin+xmid;
xmax=xmax+xmid;
ymin=ymin+ymid;
ymax=ymax+ymid;
cleardevice();
line(xmin,ymin,xmax,ymin);
line(xmin,ymin,xmin,ymax);
line(xmin,ymax,xmax,ymax);
line(xmax,ymin,xmax,ymax);
}
void rotation_rectangle()
{
float my;
int x1,y1,x2,y2,x3,y3,x4,y4;
int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
// int temp,temp1;
printf("\n\n ----Enter Rectanlge Coordinates----");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(800,200,"RECTANGLE");
rectangle(x1,y1,x2,y2);
printf("\n\n Enter the Rotation Angle:");
scanf("%f",&my);
tempx=x1; // i have made fixed point as x1 and y1 you can make fixed acc to your choice
tempy=y1;
//x3=x2;
//y3=y2;
//x4=x1;
//y4=y2;
my= my*(3.14/180);
//printf("\n\n%d",my);
tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
tempx4=tempx+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
line(tempx1,tempy1,tempx2,tempy2);
// getch();
line(tempx2,tempy2,tempx3,tempy3);
//getch();
line(tempx3,tempy3,tempx4,tempy4);//getch();
line(tempx4,tempy4,tempx1,tempy1);
}
void rotation_line()
{
float my;
int x1,y1,x2,y2,x3,y3,x4,y4;
int tempx1,tempx2,tempx3,tempx4,tempy1,tempy2,tempy3,tempy4,tempx,tempy;
// int temp,temp1;
printf("\n\n ----Enter line Coordinates----");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
outtextxy(800,200,"LINE");
line(x1,y1,x2,y2);
printf("\n\n Enter the Rotation Angle:");
scanf("%f",&my);
// rotating about the point x1 and y1
tempx=x1;
tempy=y1;
my= my*(3.14/180);
printf("\n\n%f",my);// my=angle
tempx1=tempx+((x1-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy1=tempy+((x1-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx2=tempx+((x2-tempx)*cos(my)) - ((y1-tempy)*sin(my));
tempy2=tempy+((x2-tempx)*sin(my)) + ((y1-tempy)*cos(my));
tempx3=tempx+((x2-tempx)*cos(my)) - ((y2-tempy)*sin(my));
tempy3=tempy+((x2-tempx)*sin(my)) + ((y2-tempy)*cos(my));
//tempx4=tempy+((x1-tempx)*cos(my)) - ((y2-tempy)*sin(my));
//tempy4=tempy+((x1-tempx)*sin(my)) +((y2-tempy)*cos(my));
line(tempx1,tempy1,tempx2,tempy2);
// getch();
line(tempx2,tempy2,tempx3,tempy3);
//getch();
//line(tempx3,tempy3,tempx4,tempy4);//getch();
//line(tempx4,tempy4,tempx1,tempy1);
}
void shearing()
{
int xa,ya,xb,yb,shx,choice,shy;
settextstyle(8,HORIZ_DIR,8);
cleardevice();
outtextxy(300,300," EVALUATION");
getche();
cleardevice();
printf("\n\n Enter xa:");
scanf("%d",&xa);
printf("\n\n Enter ya:");
scanf("%d",&ya);
printf("\n\n Enter xb:");
scanf("%d",&xb);
printf("\n\n Enter yb:");
scanf("%d",&yb);
rectangle(xa,ya,xb,yb);
printf("\n\t\t 1.For shearing in X direction\n\t\t 2.For Shearing In Y Direction\n\t\t Enter Your Choice:");
scanf("%d",&choice);
cleardevice();
if(choice==1)
{
printf("\n\n Enter shearing in x:");
scanf("%d",&shx);
outtextxy(600,200,"After Shearing");
line(xa,ya,xb,ya);
line(xa,ya,xa+shx,yb);
line(xb,ya,xb+shx,yb);
line(xa+shx,yb,xb+shx,yb);
}
else if (choice==2)
{
printf("\n\n Enter shearing in y:");
scanf("%d",­);
outtextxy(800,200,"After Shearing");
line(xa,ya,xb,ya+shy);
line(xa,ya,xa,yb);
line(xb,ya+shy,xb,yb+shy);
line(xa,yb,xb,yb+shy);
}
else
{
printf(" **** invalid choice ****");
}
}
LINE CLIPPING COHEN SUTHERLAND ( not complete !!!)
/***** IT TELLS UPTO THE REGION CODES AND IDENTIFIES THE POINTS TO BE CLIPPED ****/
int main()
{
int bit[4],bit1[4],temp[4];
char str[4],str1[4],str2[4];int i;
char temp5[4]={0000};
int choice3,choice4;
int check=0,choice1,choice2;
int x1,y1,x2,y2,xmin,ymin,xmax,ymax;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("\n\n\t\t --------------CAUTION --------------");
printf("\n\t\t *FINALLY CLIPPING REQUIRED MEANS* WE HAVE TO\n\t\t\t DO CLIPPING FOR THAT POINT --\n\n");
printf("\n\n\t\t **** Enter Line Cooridnates ****");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
printf("\n\n\t\t **** Enter window coordinates ****");
printf("\n\n Enter xmin:");
scanf("%d",&xmin);
printf("\n\n Enter ymin:");
scanf("%d",&ymin);
printf("\n\n Enter xmax:");
scanf("%d",&xmax);
printf("\n\n Enter ymax:");
scanf("%d",&ymax);
outtextxy(700,200,"ORIGINAL LINE");
line(x1,y1,x2,y2);
outtextxy(700,200,"ORIGINAL LINE");
rectangle(xmin,ymin,xmax,ymax);
if((x1 &&x2)>xmax || ((x1 && x2)<xmin))
{
if((y1 &&y2)>ymax || ((y1 && y2)<ymin))
printf("\n\n do clipping");
bit[0]= ymax-y1;
printf("\n\n%d",bit[0]);
if(bit[0]<0)
{
bit[0]=1; }
else
{
bit[0]=0;
}
bit[1]= y1-ymin;
printf("\n\n%d",bit[1]);
if(bit[1]<0)
{
bit[1]=1; }
else
{
bit[1]=0;
}
bit[2]=xmax-x1;
printf("\n\n%d",bit[2]);
if(bit[2]<0)
{
bit[2]=1; }
else
{
bit[2]=0;
}
bit[3]=x1-xmin;
printf("\n\n%d",bit[3]);
if(bit[3]<0)
{
bit[3]=1; }
else
{
bit[3]=0;
}
bit1[0]=ymax-y2;
printf("\n\n%d",bit[0]);
if(bit1[0]<0)
{
bit1[0]=1; }
else
{
bit1[0]=0;
}
bit1[1]=y2-ymin;
printf("\n\n%d",bit1[1]);
if(bit1[1]<0)
{
bit1[1]=1; }
else
{
bit1[1]=0;
}
bit1[2]=xmax-x2;
printf("\n\n%d",bit1[2]);
if(bit1[2]<0)
{
bit1[2]=1; }
else
{
bit1[2]=0;
}
bit1[3]=x2-xmin;
printf("\n\n%d",bit1[3]);
if(bit1[3]<0)
{
bit1[3]=1; }
else
{
bit1[3]=0;
}
// int str[4];
for(i=0;i<4;i++)
{
str[i]=0;
}
printf("\n\n\t\t ----- Region codes for point 1 -------\n\n");
for(i=0;i<4;i++)
{
str[i]=bit[i];
printf("\nregion code for point1 bit[%d]= %d",i,str[i]);
}
// int str1[4];
for(i=0;i<4;i++)
{
if( str[i]!=temp5[i])
{
check=1;
printf("\n\n FINally clipping required");
choice1=1;
choice3=1;
}
else
{
printf("\n\n no clipping required");
}
}
for(i=0;i<4;i++)
{
str1[i]=0;
}
printf("\n\n\t\t ----- Region codes for point 2 -------\n\n");
for(i=0;i<4;i++)
{
str1[i]=bit1[i];
printf("\n region code for point2 (bit[%d])=%d",i, str1[i]);
}
// int str2[4];
for(i=0;i<4;i++)
{
if( str1[i]!=temp5[i])
{
check=1;
printf("\n\n clipping required");
choice2=1;
choice4=1;
}
else
{
printf("\n\n no clipping required");
//goto x1;
}
}
}
else
{
printf("\n\n\n no clipping is required");
}
getch();
closegraph();
return 0;
}
int main()
{
int bit[4],bit1[4],temp[4];
char str[4],str1[4],str2[4];int i;
char temp5[4]={0000};
int choice3,choice4;
int check=0,choice1,choice2;
int x1,y1,x2,y2,xmin,ymin,xmax,ymax;
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
printf("\n\n\t\t --------------CAUTION --------------");
printf("\n\t\t *FINALLY CLIPPING REQUIRED MEANS* WE HAVE TO\n\t\t\t DO CLIPPING FOR THAT POINT --\n\n");
printf("\n\n\t\t **** Enter Line Cooridnates ****");
printf("\n\n Enter x1:");
scanf("%d",&x1);
printf("\n\n Enter y1:");
scanf("%d",&y1);
printf("\n\n Enter x2:");
scanf("%d",&x2);
printf("\n\n Enter y2:");
scanf("%d",&y2);
printf("\n\n\t\t **** Enter window coordinates ****");
printf("\n\n Enter xmin:");
scanf("%d",&xmin);
printf("\n\n Enter ymin:");
scanf("%d",&ymin);
printf("\n\n Enter xmax:");
scanf("%d",&xmax);
printf("\n\n Enter ymax:");
scanf("%d",&ymax);
outtextxy(700,200,"ORIGINAL LINE");
line(x1,y1,x2,y2);
outtextxy(700,200,"ORIGINAL LINE");
rectangle(xmin,ymin,xmax,ymax);
if((x1 &&x2)>xmax || ((x1 && x2)<xmin))
{
if((y1 &&y2)>ymax || ((y1 && y2)<ymin))
printf("\n\n do clipping");
bit[0]= ymax-y1;
printf("\n\n%d",bit[0]);
if(bit[0]<0)
{
bit[0]=1; }
else
{
bit[0]=0;
}
bit[1]= y1-ymin;
printf("\n\n%d",bit[1]);
if(bit[1]<0)
{
bit[1]=1; }
else
{
bit[1]=0;
}
bit[2]=xmax-x1;
printf("\n\n%d",bit[2]);
if(bit[2]<0)
{
bit[2]=1; }
else
{
bit[2]=0;
}
bit[3]=x1-xmin;
printf("\n\n%d",bit[3]);
if(bit[3]<0)
{
bit[3]=1; }
else
{
bit[3]=0;
}
bit1[0]=ymax-y2;
printf("\n\n%d",bit[0]);
if(bit1[0]<0)
{
bit1[0]=1; }
else
{
bit1[0]=0;
}
bit1[1]=y2-ymin;
printf("\n\n%d",bit1[1]);
if(bit1[1]<0)
{
bit1[1]=1; }
else
{
bit1[1]=0;
}
bit1[2]=xmax-x2;
printf("\n\n%d",bit1[2]);
if(bit1[2]<0)
{
bit1[2]=1; }
else
{
bit1[2]=0;
}
bit1[3]=x2-xmin;
printf("\n\n%d",bit1[3]);
if(bit1[3]<0)
{
bit1[3]=1; }
else
{
bit1[3]=0;
}
// int str[4];
for(i=0;i<4;i++)
{
str[i]=0;
}
printf("\n\n\t\t ----- Region codes for point 1 -------\n\n");
for(i=0;i<4;i++)
{
str[i]=bit[i];
printf("\nregion code for point1 bit[%d]= %d",i,str[i]);
}
// int str1[4];
for(i=0;i<4;i++)
{
if( str[i]!=temp5[i])
{
check=1;
printf("\n\n FINally clipping required");
choice1=1;
choice3=1;
}
else
{
printf("\n\n no clipping required");
}
}
for(i=0;i<4;i++)
{
str1[i]=0;
}
printf("\n\n\t\t ----- Region codes for point 2 -------\n\n");
for(i=0;i<4;i++)
{
str1[i]=bit1[i];
printf("\n region code for point2 (bit[%d])=%d",i, str1[i]);
}
// int str2[4];
for(i=0;i<4;i++)
{
if( str1[i]!=temp5[i])
{
check=1;
printf("\n\n clipping required");
choice2=1;
choice4=1;
}
else
{
printf("\n\n no clipping required");
//goto x1;
}
}
}
else
{
printf("\n\n\n no clipping is required");
}
getch();
closegraph();
return 0;
}
CIRCLE PROGRAM USING BRESENHAMS METHOD (GRAPHICS)
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void plotcircle(int,int,int,int);
void main()
{
int gd = DETECT, gm;
int xc,yc,x,y,r,d;
initgraph(&gd, &gm, "");
printf("\n Enter the values\n xc:\nyc:\nr:\n");
scanf("%d%d%d",&xc,&yc,&r);
x=0;
y=r;
d=3-(2*r);
plotcircle(xc,yc,x,y);
while(x<y)
{
if(d<0)
{
x=x+1;
d=d+(4*x)+6;
}
else
{
x=x+1;
y=y-1;
d=d+(4*(x-y))+10;
}
plotcircle(xc,yc,x,y);
}
getch();
}
void plotcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc-x,2);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-x,yc-y,2);
}
CIRCLE PROGRAM USING TRIGONOMETRIC METHOD (GRAPHICS)
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void plotcircle(int,int,int,int);
void main()
{
int gd = DETECT, gm;
int xc,yc,x,y,c,s,r,d,temp;
initgraph(&gd, &gm, "");
printf("\n Enter the values xc: \nyc:\nr:\n");
scanf("%d%d%d",&xc,&yc,&r);
x=0;
y=r;
d=1/r;
c=cos(d);
s=sin(d);
plotcircle(xc,yc,x,y);
while(x<=y)
{
plotcircle(xc,yc,x,y);
temp=x;
x=((x*c)-(y*s));
y=((y*c)+(temp*s));
}
getch();
}
void plotcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc-x,2);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-x,yc-y,2);
}
Circle using polynomial method
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void plotcircle(int,int,int,int);
void main()
{
int gd = DETECT, gm;;
int xc,yc,x,y,xend,r,d;
initgraph(&gd, &gm, "");
printf("\n\n Enter xc:");
scanf("%d",&xc);
printf("\n\n Enter yc:");
scanf("%d",&yc);
printf("\n\n Enter r:");
scanf("%d",&r);
scanf("%d",&xc);
printf("\n\n Enter yc:");
scanf("%d",&yc);
printf("\n\n Enter r:");
scanf("%d",&r);
x=0;
xend=r/sqrt(2);
plotcircle(xc,yc,x,y);
while(x<xend)
{
x=x+1;
y=sqrt((r*r)-(x*x));
plotcircle(xc,yc,x,y);
}
getch();
}
void plotcircle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc-x,2);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-x,yc-y,2);
}
Subscribe to:
Posts (Atom)