0% found this document useful (0 votes)
31 views

Project Foodie

The document contains code snippets for drawing basic graphics like lines, circles and plotting points using graphics.h library in C/C++. It includes midpoint circle algorithm, Bresenham's line drawing algorithm and Cohen-Sutherland line clipping algorithm to draw shapes within a defined clipping window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Project Foodie

The document contains code snippets for drawing basic graphics like lines, circles and plotting points using graphics.h library in C/C++. It includes midpoint circle algorithm, Bresenham's line drawing algorithm and Cohen-Sutherland line clipping algorithm to draw shapes within a defined clipping window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

1

#include <graphics.h>
#include <conio.h>
main()
{
int gd = DETECT, gm;

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

line(100, 100, 200, 200);

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

2
#include <graphics.h>
int main()
{

int gd = DETECT, gm;


initgraph(&gd, &gm, "");
circle(250, 200, 50);
getch();
closegraph();
return 0;
}
4

#include<graphics.h>
using namespace std;
int main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

cout<<"Enter the value of x1 and y1 : ";


cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;

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

if(dx>=dy)
step=dx;
else
step=dy;

dx=dx/step;
dy=dy/step;

x=x1;
y=y1;

i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
}
return 0;

}
5
#include<graphics.h>
using namespace std;
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0,
y0, x1, y1;
initgraph(&gdriver, &gmode, "");

cout<<"Enter co-ordinates of first point:


";
cin>>x0>>y0;
cout<<"Enter co-ordinates of second
point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);

getch();

return 0;
}

6
1. #include<graphics.h>
2. #include<conio.h>
3. #include<math.h>
4. voidsetPixel(int x, int y, int h, int k)
5. {
6. putpixel(x+h, y+k, RED);
7. putpixel(x+h, -y+k, RED);
8. putpixel(-x+h, -y+k, RED);
9. putpixel(-x+h, y+k, RED);
10. putpixel(y+h, x+k, RED);
11. putpixel(y+h, -x+k, RED);
12. putpixel(-y+h, -x+k, RED);
13. putpixel(-y+h, x+k, RED);
14. }
15. main()
16. {
17. intgd=0, gm,h,k,r;
18. double x,y,x2;
19. h=200, k=200, r=100;
20. initgraph(&gd, &gm, "C:\\TC\\BGI ");
21. setbkcolor(WHITE);
22. x=0,y=r;
23. x2 = r/sqrt(2);
24. while(x<=x2)
25. {
26. y = sqrt(r*r - x*x);
27. setPixel(floor(x), floor(y), h,k);
28. x += 1;
29. }
30. getch();
31. closegraph();
32. return 0;
33. }

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

void main()
{
int xc,yc,r,p,x,y;
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
clrscr();
printf("\nEnter the co-ordinates of center :
");
scanf("%d %d",&xc,&yc);
printf("\nEnter the radius: ");
scanf("%d",&r);
x = 0;
y = r;
p=3-(2*r);

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

putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
}

getch();
closegraph();
}

8
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int x,y,x_mid,y_mid,radius,dp;

int g_mode,g_driver=DETECT;

clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\
BGI");

printf("*********** MID POINT Circle drawing


algorithm ********\n\n");

printf("\nenter the coordinates= ");

scanf("%d %d",&x_mid,&y_mid);

printf("\n now enter the radius =");

scanf("%d",&radius);

x=0;

y=radius;

dp=1-radius;

do

putpixel(x_mid+x,y_mid+y,YELLOW);

putpixel(x_mid+y,y_mid+x,YELLOW);

putpixel(x_mid-y,y_mid+x,YELLOW);

putpixel(x_mid-x,y_mid+y,YELLOW);

putpixel(x_mid-x,y_mid-y,YELLOW);

putpixel(x_mid-y,y_mid-x,YELLOW);

putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);

if(dp<0) {

dp+=(2*x)+1;

else{

y=y-1;

dp+=(2*x)-(2*y)+1;

x=x+1;

}while(y>x);

getch();

}
9
#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<math.h>

void main()

int
rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4
];

int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;

float slope;

int x,y,x1,y1,i, xc,yc;

int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");

printf("\n****** Cohen Sutherlsnd Line Clipping algorithm


***********");

printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);

printf("\n First enter XMax, YMax =");

scanf("%d %d",&W_xmax,&W_ymax);

printf("\n Please enter intial point x and y= ");


scanf("%d %d",&x,&y);

printf("\n Now, enter final point x1 and y1= ");

scanf("%d %d",&x1,&y1);

cleardevice();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

line(x,y,x1,y1);

line(0,0,600,0);

line(0,0,0,600);

if(y>W_ymax) {

rcode_begin[0]=1; // Top

flag=1 ;

if(y<W_ymin) {

rcode_begin[1]=1; // Bottom

flag=1;

if(x>W_xmax) {

rcode_begin[2]=1; // Right

flag=1;

if(x<W_xmin) {

rcode_begin[3]=1; //Left

flag=1;
}

//end point of Line

if(y1>W_ymax){

rcode_end[0]=1; // Top

flag=1;

if(y1<W_ymin) {

rcode_end[1]=1; // Bottom

flag=1;

if(x1>W_xmax){

rcode_end[2]=1; // Right

flag=1;

if(x1<W_xmin){

rcode_end[3]=1; //Left

flag=1;

if(flag==0)

printf("No need of clipping as it is already in window");

}
flag=1;

for(i=0;i<4;i++){

region_code[i]= rcode_begin[i] && rcode_end[i] ;

if(region_code[i]==1)

flag=0;

if(flag==0)

printf("\n Line is completely outside the window");

else{

slope=(float)(y1-y)/(x1-x);

if(rcode_begin[2]==0 && rcode_begin[3]==1) //left

y=y+(float) (W_xmin-x)*slope ;

x=W_xmin;

if(rcode_begin[2]==1 && rcode_begin[3]==0) // right

y=y+(float) (W_xmax-x)*slope ;

x=W_xmax;
}

if(rcode_begin[0]==1 && rcode_begin[1]==0) // top

x=x+(float) (W_ymax-y)/slope ;

y=W_ymax;

if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom

x=x+(float) (W_ymin-y)/slope ;

y=W_ymin;

// end points

if(rcode_end[2]==0 && rcode_end[3]==1) //left

y1=y1+(float) (W_xmin-x1)*slope ;

x1=W_xmin;

if(rcode_end[2]==1 && rcode_end[3]==0) // right

y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

if(rcode_end[0]==1 && rcode_end[1]==0) // top

x1=x1+(float) (W_ymax-y1)/slope ;

y1=W_ymax;

if(rcode_end[0]==0 && rcode_end[1]==1) // bottom

x1=x1+(float) (W_ymin-y1)/slope ;

y1=W_ymin;

delay(1000);

clearviewport();

rectangle(W_xmin,W_ymin,W_xmax,W_ymax);

line(0,0,600,0);

line(0,0,0,600);

setcolor(RED);

line(x,y,x1,y1);
getch();

closegraph();

10
1. //Program to Implement Cyrus Beck Line Clipping Algorithm
2.
3. #include<conio.h>
4. #include<iostream.h>
5. #include<graphics.h>
6. #include<process.h>
7.
8. #define ROUND(a) ((int)(a + 0.5))
9.
10. struct _line
11. {
12. int x1 , y1;
13. int x2 , y2;
14. };
15.
16. int xmax,xmin,ymax,ymin;
17.
18. void clip(_line a)
19. {
20. int p[4],q[4],i,dx,dy,flag=1;
21. double u1=0,u2=1,temp;
22. dx=a.x2-a.x1;
23. dy=a.y2-a.y1;
24. p[0]=-dx; q[0]=a.x1-xmin;
25. p[1]=dx; q[1]=xmax-a.x1;
26. p[2]=-dy; q[2]=a.y1-ymin;
27. p[3]=dy; q[3]=ymax-a.y1;
28.
29. if(p[0]==0 && p[3]==0) //Point Clipping
30. {
31. if(a.x1>=xmin && a.x1<=xmax && a.y1>=ymin && a.y1<=ymax)
32. putpixel(a.x1,a.y1,GREEN);
33. else
34. return;
35. }
36.
37. if (p[0]==0)
38. if(q[0]*q[1]<=0)
39. return; //for Parallel lines
40. if(p[2]==0)
41. if(q[2]*q[3]<=0)
42. return;
43.
44. for (i=0;i<4;i++)
45. {
46. if(p[i]<0 && flag)
47. {
48. temp=(double)q[i]/(double)p[i];
49. if(temp>u2)
50. flag=0;
51. else
52. if(temp>u1)
53. u1=temp;
54. }
55. else
56. if(p[i]>0 && flag)
57. {
58. temp=(double)q[i]/(double)p[i];
59. if(temp<u1)
60. flag=0;
61. else
62. if(temp<u2)
63. u2=temp;
64. }
65. }
66. if (u1>=u2 || flag==0)
67. return;
68. temp=a.x1;
69. i=a.y1;
70. a.x1=temp+u1*dx;
71. a.x2=temp+u2*dx;
72. a.y1=i+u1*dy;
73. a.y2=i+u2*dy;
74. line(319+ROUND(a.x1),240-ROUND(a.y1),319+ROUND(a.x2),240-
ROUND(a.y2));
75. }
76.
77. void drawWindow(int xmin,int ymin,int xmax,int ymax)
78. {
79. line(319+xmin,240-ymax,319+xmax,240-ymax);// Top Edge
80. line(319+xmax,240-ymax,319+xmax,240-ymin);// Right Edge
81. line(319+xmax,240-ymin,319+xmin,240-ymin);// Bottom Edge
82. line(319+xmin,240-ymin,319+xmin,240-ymax);// Left Edge
83. }
84.
85. void main()
86. {
87. int driver=DETECT,mode,n,i;
88. _line *a;
89.
90. cout<<"Enter the window coordinates : \n";
91. cout<<"Lower Left Corner : ";
92. cin>>xmin>>ymin;
93.
94. cout<<"Upper Right Corner : ";
95. cin>>xmax>>ymax;
96. if (xmax<xmin||ymax<ymin)
97. {
98. cout<<"\nIncorrect Window";
99. getch();
100. exit(0);
101. }
102.
103. cout<<"How many lines do you want to draw : ";
104. cin>>n;
105. a=new _line[n];
106. cout<<"Enter Coordinates : \n";
107. for(i=0;i<n;i++)
108. {
109. cout<<"line "<<i+1<<" : ";
110. cout<<"Enter coordinates of Ist Vertex"<<endl;
111. cout<<"x1 =";
112. cin>>a[i].x1;
113. cout<<"y1 =";
114. cin>>a[i].y1;
115.
116. cout<<"Enter coordinates of IInd Vertex"<<endl;
117. cout<<"x2 ="; cin>>a[i].x2;
118. cout<<"y2 ="; cin>>a[i].y2;
119. }
120.
121. initgraph(&driver,&mode,"..\\bgi");
122. outtextxy(0,5,"The original Line is");
123.
124. for (i=0;i<n;i++)
125. line(319+a[i].x1,240-a[i].y1,319+a[i].x2,240-a[i].y2);
126. getch();
127.
128. setcolor(LIGHTGREEN);
129. outtextxy(0,20,"The Clipping Window is");
130.
131. drawWindow(xmin,ymin,xmax,ymax);
132. getch();
133. cleardevice();
134.
135. drawWindow(xmin,ymin,xmax,ymax);
136. setcolor(WHITE);
137. outtextxy(0,5,"The Clipped line is");
138. for(i=0;i<n;i++)
139. clip(a[i]);
140. getch();
141. closegraph();
142. restorecrtmode();
143. }

11
#include “stdio.h”
#include “conio.h”
#include “stdlib.h”
#include “dos.h”
#include “math.h”
#include “graphics.h”
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
{
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cleardevice();
printf(“ENTER END-POINT 1 (x,y): “);
scanf(“%d%d”,&p1.x,&p1.y);
printf(“\nENTER END-POINT 2 (x,y): “);
scanf(“%d%d”,&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
return(0);
}midsub(PT p1,PT p2)
{
PT mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
drawline(p1,p2);
break;
case 1:break;
case 2:
mid.x = p1.x + (p2.x-p1.x)/2;
mid.y = p1.y + (p2.y-p1.y)/2;
midsub(p1,mid);
mid.x = mid.x+1;
mid.y = mid.y+1;
midsub(mid,p2);
break;
}
}
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
}

void drawline (PT p1,PT p2)


{
setcolor(15);
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p)
{
PT ptemp;
if(p.y<=100) ptemp.code[0]=’1′; else ptemp.code[0]=’0′; if(p.y>=400)
ptemp.code[1]=’1′;
else
ptemp.code[1]=’0′;
if (p.x>=450)
ptemp.code[2]=’1′;
else
ptemp.code[2]=’0′;
if (p.x<=150)
ptemp.code[3]=’1′;
else
ptemp.code[3]=’0′;
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}

int visibility (PT p1,PT p2)


{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!=’0′)||(p2.code[i]!=’0′))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) &&(p1.code[i]==’1′))
flag=0;
}
if(flag==0)
return(1);
return(2);
}
12
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

void RectAngle(int x, int y, int Height, int Width);


void Translate(int x, int y, int Height, int Width);

void main()
{
int gd = DETECT, gm;
int x, y, Height, Width;

// Initialize the graphics system


initgraph(&gd, &gm, " ");

printf("Enter the First point for the Rectangle:");


scanf("%d%d", &x, &y);

printf("Enter the Height&Width for the Rectangle:");


scanf("%d%d", &Height, &Width);

// Draw the original rectangle


RectAngle(x, y, Height, Width);

getch();
cleardevice();

// Translate the rectangle


Translate(x, y, Height, Width);

// Draw the translated rectangle


RectAngle(x, y, Height, Width);

getch();
}

void RectAngle(int x, int y, int Height, int Width)


{
// Draw the four sides of the rectangle
line(x, y, x + Width, y); // Top side
line(x, y, x, y + Height); // Left side
line(x + Width, y, x + Width, y + Height); // Right side
line(x, y + Height, x + Width, y + Height); // Bottom side
}
void Translate(int x, int y, int Height, int Width)
{
int Newx, Newy, a, b;

printf("Enter the Translation coordinates:");


scanf("%d%d", &Newx, &Newy);

cleardevice();

// Calculate the new position of the rectangle after translation


a = x + Newx;
b = y + Newy;

// Draw the translated rectangle


RectAngle(a, b, Height, Width);
}

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

void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3);
void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle);

int main()
{
int gd = DETECT, gm;
int x1, y1, x2, y2, x3, y3;
float angle;

initgraph(&gd, &gm, "");

printf("Enter the 1st point for the triangle (x1 y1): ");
scanf("%d%d", &x1, &y1);

printf("Enter the 2nd point for the triangle (x2 y2): ");
scanf("%d%d", &x2, &y2);

printf("Enter the 3rd point for the triangle (x3 y3): ");
scanf("%d%d", &x3, &y3);

DrawTriangle(x1, y1, x2, y2, x3, y3);


printf("Enter the angle for rotation (in degrees): ");
scanf("%f", &angle);

RotateTriangle(x1, y1, x2, y2, x3, y3, angle);

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

void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
}

void RotateTriangle(int x1, int y1, int x2, int y2, int x3, int y3, float angle)
{
int p = x2, q = y2;
float radianAngle = (angle * 3.14) / 180.0;

int a1 = p + (x1 - p) * cos(radianAngle) - (y1 - q) * sin(radianAngle);


int b1 = q + (x1 - p) * sin(radianAngle) + (y1 - q) * cos(radianAngle);

int a2 = p + (x2 - p) * cos(radianAngle) - (y2 - q) * sin(radianAngle);


int b2 = q + (x2 - p) * sin(radianAngle) + (y2 - q) * cos(radianAngle);

int a3 = p + (x3 - p) * cos(radianAngle) - (y3 - q) * sin(radianAngle);


int b3 = q + (x3 - p) * sin(radianAngle) + (y3 - q) * cos(radianAngle);

setcolor(1);
DrawTriangle(a1, b1, a2, b2, a3, b3);
}

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

int gd = DETECT, gm;


int n, x[100], y[100], i;
float sfx, sfy;
void draw();
void scale();
using namespace std;

int main()
{
cout << "Enter no. of sides in polygon: ";
cin >> n;
cout << "Enter coordinates x, y for each vertex: ";
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
cout << "Enter scale factors: sfx and sfy : ";
cin >> sfx >> sfy;
initgraph(&gd, &gm, (char*)"");
cleardevice();
setcolor(RED);
draw();
scale();
setcolor(YELLOW);
draw();
getch();
closegraph();
return 0;
}
void draw()
{
for (i = 0; i < n; i++) {
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
}
}
void scale()
{
for (i = 0; i < n; i++) {
x[i] = x[0] + (int)((float)(x[i] - x[0]) * sfx);
y[i] = y[0] + (int)((float)(y[i] - y[0]) * sfx);
}
}

16
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<iostream>
#include<math.h>
int gd= DETECT, gm;
int n,x[100],y[100],i;
float sfx, sfy;
void draw();
void scale();
using namespace std;
int main(){
cout<<"Enter no. of sides in polygon: ";
cin>>n;
cout<<"Enter coordinates x, y for each vertex: ";
for(i=0;i<n;i++){
cin>>x[i]>>y[i];}
cout<<"Enter scale factors: sfx and sfy : ";
cin>>sfx>>sfy;
initgraph(&gd, &gm, (char*)"");
cleardevice();
setcolor(RED);
draw();
scale();
setcolor(YELLOW);
draw();
getch();
closegraph();
return 0;
}
void draw(){
for(i=0; i<n; i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);}
}
void scale(){
for(i=0; i<n; i++){
x[i]=x[0]+(int)((float)(x[i]-x[0])*sfx);
y[i]=y[0]+(int)((float)(y[i]-y[0])*sfx);
}
}

17
// C program for the above approach

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

// Driver Code
void main()
{
// Initialize the drivers
int gm, gd = DETECT, ax, x1 =
100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100;

// Add in your BGI folder path


// like below initgraph(&gd, &gm,
// "C:\\TURBOC3\\BGI");
initgraph(&gd, &gm, "");
cleardevice();

// Draw the graph


line(getmaxx() / 2, 0, getmaxx() / 2,
getmaxy());
line(0, getmaxy() / 2, getmaxx(),
getmaxy() / 2);

// Object initially at 2nd quadrant


printf("Before Reflection Object"
" in 2nd Quadrant");

// Set the color


setcolor(14);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();

// After reflection
printf("\nAfter Reflection");

// Reflection along origin i.e.,


// in 4th quadrant
setcolor(4);
line(getmaxx() - x1, getmaxy() -
y1,
getmaxx() - x2, getmaxy() - y2);

line(getmaxx() - x2, getmaxy() -


y2,
getmaxx() - x3, getmaxy() - y3);

line(getmaxx() - x3, getmaxy() -


y3,
getmaxx() - x1, getmaxy() - y1);
// Reflection along x-axis i.e.,
// in 1st quadrant
setcolor(3);
line(getmaxx() - x1, y1,
getmaxx() - x2, y2);
line(getmaxx() - x2, y2,
getmaxx() - x3, y3);
line(getmaxx() - x3, y3,
getmaxx() - x1, y1);

// Reflection along y-axis i.e.,


// in 3rd quadrant
setcolor(2);
line(x1, getmaxy() - y1, x2,
getmaxy() - y2);
line(x2, getmaxy() - y2, x3,
getmaxy() - y3);
line(x3, getmaxy() - y3, x1,
getmaxy() - y1);
getch();

// Close the graphics


closegraph();
}
18
// C program to implement
// Window to ViewPort
Transformation

#include <stdio.h>

// Function for window to viewport


transformation
void WindowtoViewport(int x_w, int
y_w, int x_wmax,
int y_wmax, int
x_wmin, int y_wmin,
int x_vmax, int
y_vmax, int x_vmin,
int y_vmin)
{
// point on viewport
int x_v, y_v;

// scaling factors for x coordinate


and y coordinate
float sx, sy;

// calculating Sx and Sy
sx = (float)(x_vmax - x_vmin) /
(x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) /
(y_wmax - y_wmin);

// calculating the point on viewport


x_v = x_vmin + (float)((x_w -
x_wmin) * sx);
y_v = y_vmin + (float)((y_w -
y_wmin) * sy);

printf("The point on viewport: (%d,


%d )\n ", x_v, y_v);
}
// Driver Code
void main()
{
// boundary values for window
int x_wmax = 80, y_wmax = 80,
x_wmin = 20, y_wmin = 40;

// boundary values for viewport


int x_vmax = 60, y_vmax = 60,
x_vmin = 30, y_vmin = 40;

// point on window
int x_w = 30, y_w = 80;

WindowtoViewport(30, 80, 80, 80,


20, 40, 60, 60, 30, 40);
}

19
#include<graphics.h>

#include<math.h>

#include<conio.h>

#include<stdio.h>

void main()

int x[4],y[4],i;

double put_x,put_y,t;

int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");

printf("\n****** Bezier Curve ***********");


printf("\n Please enter x and y coordinates ");

for(i=0;i<4;i++)

scanf("%d%d",&x[i],&y[i]);

putpixel(x[i],y[i],3); // Control Points

for(t=0.0;t<=1.0;t=t+0.001) // t always lies


between 0 and 1

put_x = pow(1-t,3)*x[0] + 3*t*pow(1-t,2)*x[1] + 3*t*t*(1-


t)*x[2] + pow(t,3)*x[3]; // Formula to draw curve

put_y = pow(1-t,3)*y[0] + 3*t*pow(1-t,2)*y[1] + 3*t*t*(1-


t)*y[2] + pow(t,3)*y[3];

putpixel(put_x,put_y, WHITE); // putting pixel

getch();

closegraph();

20

You might also like