Parabola – Midpoint Approach 1st order


#include<iostream.h>

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

void put_pixel(int x, int y)

{

putpixel((x+300),(240-y),15);

putpixel((x+300),(240+y),15);

}

void para(int cx, int cy, double a)

{

setcolor(BLUE);

line(300,0,300,479);

setcolor(RED);

line(0,240,639,240);

double x=0,y=0;  /* initial coorodinates */

double d1;

d1 = (2*a) – 1;

put_pixel(x,y);

while(y<= (2*a*1.0))

{

if(d1<0)

{

d1+= 4*a-3-2*y;

x++;

y++;

}

else

{

d1-= 3 + 2*y;

y++;

}

put_pixel(x,y);

}

d1 = (4.0*a*(x+1) – (y+0.5)*(y+0.5) );

while( y < 220 )

{

if(d1<0)

{

d1+= 4*a;

x++;

}

else

{

d1+= 4.0*a – 2 – 2.0*y ;

x++;

y++;

}

put_pixel(x,y);

}

}

void main()

{

clrscr();

double a;

cout<<“Enter a : “;

cin>>a;

int gdriver = DETECT, gmode;

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

para(0,0,a);

getch();

closegraph();

}

Hyperbola- Bresenham’s Approach

#include<iostream.h>

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

 

void plotpixel(int x, int y)

{

putpixel((x+300),(240-y),15);

putpixel((-x+300),(240-y),15);

putpixel((x+300),(240+y),15);

putpixel((-x+300),(240+y),15);

}

 

void hyper(int cx, int cy, double a,double b)

{

setcolor(BLUE);

line(300,0,300,479);

setcolor(RED);

line(0,240,639,240);

double x=a,y=0;  /* initial coorodinates */

 

double d1 = (2*a*a) – (b*b) – (2*a*b*b);

plotpixel(x,y);

 

while((a*a*y) <= (b*b*x))

{

if(d1<= (-1*b*b*0.5))

{

d1+= 2*a*a*(2*y+3);

plotpixel(x,y);

y++;

}

else

{

d1+= 2*a*a*(2*y+3) – 4*b*b*(x+1);

plotpixel(x,y);

x++;

y++;

}

//plotpixel(x,y);

}

 

d1 = a*a*(y+1)*(y+1) + a*a*y*y + 2*a*a*b*b – 2*a*a*b*b*(x+1)*(x+1);

 

while(y<220)

{

if(d1<= (a*a*0.5))

{

d1+= a*a*4*(y+1) – 2-a*a*b*b*(2*x+3)*(2*x+3);

y++;

x++;

}

else

{

d1+= -2.0*b*b*a*a*(2*x+3);

x++;

}

plotpixel(x,y);

}

 

}

 

void main()

{

clrscr();

double a,b;

cout<<“Enter a and b : “;

cin>>a>>b;

 

int gdriver = DETECT, gmode;

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

hyper(0,0,a,b);

getch();

closegraph();

}

 

 

 

 

Hyperbola-midpoint approach 1st order

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<dos.h>

#include<stdlib.h>

 

void main()

{

int gd=DETECT;

int gm;

initgraph(&gd,&gm,”c:\\tc\\bgi”);

float x,y;

float xc,yc;

float d,fx,fy,b,a;

d=b*b*(a+0.5)*(a+0.5)-a*a-a*a*b*b;

printf(“enter centre (xc,yc)\n”);

scanf(“%f %f”,&xc,&yc);

printf(“enter a & b”);

scanf(“%f %f”,&a,&b);

x=a;

y=0;

fx=2*b*b*a;

fy=0;

setcolor(MAGENTA);

line(0,240,640,240);

line(320,0,320,480);

while(abs(fy)<=fx)

{

if(d>=0)

{

d=d-a*a*(2*y+3);

}

else

{

d=d-a*a*(2*y+3)+b*b*(2*x+2);

x++;

fx=fx+2*b*b;

}

y++;

fy=fy+2*a*a;

putpixel(x+320+xc,240-y-yc,GREEN);

putpixel(x+320+xc,240+y-yc,GREEN);

putpixel(-x+320+xc,240-y-yc,GREEN);

delay(20);

putpixel(-x+320+xc,240+y-yc,GREEN);

delay(20);

}

x=p/2;

y=p;

d=-p;

while(y<3*p)

{

x++;

if(d>=0)

{

d=d-2*p;

}

else

{

d=d+2*y+2-2*p;

y++;

}

putpixel(x+320+xc,240-y-yc,RED);

delay(20);

putpixel(x+320+xc,240+y-yc,RED);

delay(20);

}

getch();

}

Square to Trapezium

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

struct vertex
{
int x;
int y;
}v1,v2,v3,v4;
float count;
void plot(vertex a,vertex b,vertex c, vertex d,int p)
{ if(p==0)
{
setcolor(5);
}
else{
setcolor(6);
}
/* draw a diagonal line */
line(a.x+150, a.y+150, b.x+150, b.y+150);
line(b.x+150, b.y+150, c.x+150, c.y+150);
line(c.x+150, c.y+150, d.x+150, d.y+150);
line(d.x+150, d.y+150, a.x+150 ,a.y+150);
/* clean up */
cout<<“/n”;
}
void scale(int p,float &count)
{
int flag=0;
float cdec;
vertex v,w,a1,a2;
int inc,dec;
v=v2;
w=v3;
//cout<<“flag”<<flag;
if(p==1)
{
count=count+0.05;
cout<<“count is”<<count;
v2.x=v3.x=v.x;
//cout<<“w.y is”<<w.y<<” count is”<<count<<“v1.y is”<<v1.y;
v3.y=w.y*count+(1-count)*v1.y;
//cout<<“y coordinate of third vertex is”<<v3.y;
inc=v3.y-w.y;
v2.y=v.y-inc;
//cout<<“y coordinate of 2nd vertex is”<<v2.y;
//cout<<“\n inc is”<<inc;

//cout<<“a1 “<<a1.x<<” “<<a1.y<<” a2″<<a2.x<<” “<<a2.y;
//cout<<“v “<<v2.x<<” “<<v2.y<<“w”<<v3.x<<” “<<v3.y;
}
if(p==0)
{v=v2;
w=v3;
cdec=1/count;
//cout<<” “<<cdec;
v2.x=v3.x=v.x;
v3.y=w.y*cdec+(1-cdec)*v1.y;
//cout<<” a1 calculated “<<a1.y;
dec=w.y-v3.y;
//cout<<“dec is”<<dec;
v2.y=v.y+dec;

count=count-0.05;
//cout<<“” <<a1.x <<“\t”<<a1.y<<” “<<a2.x<<” “<<a2.x<<” “<<a2.y;
//cout<<“\n values of v and w”;
//cout<<v.x<<” “<<v.y<<” “<<w.x<<” “<<w.y;
}
plot(v1,v2,v3,v4,p);
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* 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);
}
cout<<“enter 1 vertex x coordinate”;
cin>>v1.x;
cout<<“enter y”;
cin>>v1.y;
v2.x=v1.x+100;
v2.y=v1.y;
v3.x=v1.x+100;
v3.y=v1.y+100;
v4.x=v1.x;
v4.y=v1.y+100;

/* draw a diagonal line */
line(v1.x, v1.y, v2.x, v2.y);
line(v2.x, v2.y, v3.x, v3.y);
line(v3.x, v3.y, v4.x, v4.y);
line(v4.x, v4.y, v1.x,v1.y);
/* clean up */

char choice;
int flag2=0;
count=1.0;
do
{
if(flag2==0)
{
cout<<“enter w for increase”;
cout<<“\n enter s for decrease”;
cout<<“\n enter e for exit”;
}
flag2++;
choice=getch();
switch(choice)
{
case ‘w’: scale(1,count);
break;
case ‘s’: scale(0,count);
break;
}
}
while(choice!=’e’);

getch();
closegraph();
return 0;
}

Plotting Circle

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

void plotcircle(int x,int y,int x1,int y1, int n)
{
delay(50);
putpixel(x1+x+320,240+y1+y,4);
putpixel(x1-x+320,240+y1-y,4);
putpixel(x1+x+320,240+y1-y,4);
putpixel(x1-x+320,240+y1+y,4);
putpixel(x1-y+320,240+y1+x,4);
putpixel(x1-y+320,240+y1-x,4);
putpixel(x1+y+320,240+y1-x,4);
putpixel(x1+y+320,240+y1+x,4);
}

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* 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);
}
int x1,y1,x,y,r,d;
cout<<“enter coordinates of centre”;
cin>>x1>>y1;
cout<<“\n enter radius”;
cin>>r;
d=1-r;
x=0;
y=r;
while(x<=y)
{
if(d<0)
{
d=d+2*x+3;
x++;
}
else
{
d=d+2*x-2*y+5;
x++;
y–;
}
plotcircle(x,y,x1,y1,5);
}
/* clean up */
getch();
closegraph();
return 0;
}

DDA line drawing Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<iostream.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* 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);
}
int x1,x2,y1,y2;
cout<<“enter start points”;
cin>>x1>>y1;
cout<<“enter end points”;
cin>>x2>>y2;
int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);
float inc,x,y;
if(dx>dy)
{
x=x1;
y=y1;
inc=1.0*dx/dy;
while(x2>x)
{
putpixel(x,y,4);
x=x+1;
y=y+inc;
}
}
/*
if(dx>dy)
{
x=x1;
y=y1;
inc=dx/dy;
while(x2>x)
{
putpixel(x,y,2);
x=x+1;
y=y+inc;
}
}*/
if(dx<=dy)
{
x=x1;
y=y1;
inc=1.0*dx/dy;
while(x2>x)
{
putpixel(x,y,4);
x=x+inc;
y=y+1;
}
}
/* clean up */
getch();
closegraph();
return 0;
}

 

 

 

Ellipse with Rotating Circle

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 360;
int xradius = 100, yradius = 50;

/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, “”);

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

 

/* draw ellipse */
ellipse(100,100, stangle, endangle,70,90 );
//putpixel(100,0,3);
//cleardevice();
//putpixel(170,0,3);
circle(180,0,10);
circle(100,190,10);
circle(100,0,10);
float x1,y1,x,y,a,b,fx,fy;
x1=100;
y1=100;
a=80;
b=100;
float d;
x=10;
y=b;

d = a*a*(0.25-b) + b*b;

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(x+100,y+100,10);
delay(200);
cleardevice();
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(x+100,y+100,10);
delay(200);
cleardevice();
}
x=10;
y=b;

d = a*a*(0.25-b) + b*b;

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(100-x,100-y,10);
delay(200);
cleardevice();
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
ellipse(100,100, stangle, endangle,70,90 );
circle(100-x,100-y,10);
delay(200);
cleardevice();
}

cleardevice();
ellipse(100,100,stangle,endangle,80,100);
/* delay(1000);
for(int i=0;i<100;i=i+10)
{ ellipse(100,100, stangle, endangle,70,90 );
circle(100+i,0+i,10);
delay(200);
cleardevice();
}
/* clean up */
getch();
closegraph();
return 0;
}

Ellipse- Computer Graphics

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

void plotpixel(int x, int y)
{
// delay(10);
putpixel((x+320),(240-y),15);
putpixel((-x+320),(240-y),15);
putpixel((x+320),(240+y),15);
putpixel((-x+320),(240+y),15);

}

void ell(float a, float b)
{
float x=0,y=b;

float d = a*a*(0.25-b) + b*b;

setcolor(RED);
line(320,0,320,480);
setcolor(BLUE);
line(0,240,640,240);
setcolor(WHITE);

plotpixel(x,y);

while((b*b*(x+1))<=(a*a*(y-0.5)))
{
if(d<0)
{
d+= 1.0*b*b*(3+2*x);
x++;
}
else
{
d+= b*b*(3+2*x)*1.0+2*1.0*a*a*(1-y);
x++;
y–;
}
plotpixel(x,y);
}

d = b*b*(1.0*x+0.5)*(1.0*x+0.5)+1.0*a*a*(y-1)*(y-1)-1.0*a*a*b*b;

while(y>=0)
{
if(d<0)
{
d+= 2.0*b*b*(1+x)+1.0*a*a*(3-2*y);
x++;
y–;
}
else
{
d+= a*a*(3-2*y)*1.0;
y–;
}
plotpixel(x,y);
}

}

void main()
{
clrscr();

float a,b;
cout<<“Enter a & b : “;
cin>>a>>b;

int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, “c:\\tc\\bgi”);

ell(a,b);
getch();
closegraph();
}

Hermite Curve

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

struct pt
{
float x,y;
float mx,my;
};
struct pt g1,g2;

void main()
{
float outx,outy;
float u;
float gx[4],gy[4],tempx[4],tempy[4];

cout<<“Enter the x-co-ord of first point: “;
cin>>g1.x;
cout<<“\n\nEnter the y-co-ord of first point: “;
cin>>g1.y;
cout<<“\n\nEnter the x-co-ord of second point: “;
cin>>g2.x;
cout<<“\n\nEnter the y-co-ord of the second point: “;
cin>>g2.y;

cout<<“Enter the x-co-ord of the tangent vector at first end point”;
cin>>g1.mx;
cout<<“Enter the tangent vector’s y co-ord at first end point: “;
cin>>g1.my;
cout<<“Enter the tangent vector’s x-co-ord at second end point: “;
cin>>g2.mx;
cout<<“Enter the tangent vector’s y-co-ord at second end point: “;
cin>>g2.my;

int gdriver = DETECT, gmode, errorcode;

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

gx[0]=g1.x;
gx[1]=g2.x;
gx[2]=g1.mx;
gx[3]=g2.mx;

gy[0]=g1.y;
gy[1]=g2.y;
gy[2]=g1.my;
gy[3]=g2.my;

tempx[0] = 2*(gx[0]-gx[1]) + gx[2] + gx[3];
tempx[1] = -3*(gx[0]-gx[1]) – 2*gx[2] – gx[3];
tempx[2] = gx[2];
tempx[3] = gx[0];

tempy[0] = 2*(gy[0]-gy[1]) + gy[2] + gy[3];
tempy[1] = -3*(gy[0]-gy[1]) – 2*gy[2] – gy[3];
tempy[2] = gy[2];
tempy[3] = gy[0];

setcolor(RED);
line(0,240,640,240);
setcolor(BLUE);
line(320,0,320,480);
setcolor(WHITE);

for(u=0;u<=1;u+=0.0001)
{
outx=u*u*u*tempx[0]+u*u*tempx[1]+u*tempx[2]+tempx[3];
outy=u*u*u*tempy[0]+u*u*tempy[1]+u*tempy[2]+tempy[3];
putpixel(320+outx,240-outy,15);
}

getch();
}

Mouse Program for graphics

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
union REGS in,out;

int callmouse()
{
in.x.ax=1;
int86(51,&in,&out);
return 1;
}
void mouseposi(int &xpos,int &ypos,int &click)
{
in.x.ax=3;
int86(51,&in,&out);
click=out.x.bx;
xpos=out.x.cx;
ypos=out.x.dx;
}
int mousehide()
{
in.x.ax=2;
int86(51,&in,&out);
return 1;
}
void setposi(int &xpos,int &ypos)
{
in.x.ax=4;
in.x.cx=xpos;
in.x.dx=ypos;
int86(51,&in,&out);
}
int main()
{
int x,y,cl,a,b;
clrscr();
int g=DETECT,m;
initgraph(&g,&m,”c:\tc\bgi”);
a=100;
b=400;
setposi(a,b);
callmouse();
do
{
mouseposi(x,y,cl);
gotoxy(10,9);
printf(“\n\tMouse Position is: %d,%d”,x,y);
printf(“\n\tClick: %d”,cl);
printf(“\n\tPress any key to hide the mouse”);
}while(!kbhit());
getch();
mousehide();
printf(“\n\n\tPress any key to Exit”);
getch();
}

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives