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

Cgfil

The document discusses several programs to implement 2D computer graphics algorithms. It includes programs to implement DDA and Bresenham's line drawing algorithms, the midpoint circle drawing algorithm, Cohen-Sutherland and Sutherland-Hodgeman line clipping algorithms, a scanline polygon fill algorithm, and 2D transformations using homogeneous coordinates.

Uploaded by

pocago6232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Cgfil

The document discusses several programs to implement 2D computer graphics algorithms. It includes programs to implement DDA and Bresenham's line drawing algorithms, the midpoint circle drawing algorithm, Cohen-Sutherland and Sutherland-Hodgeman line clipping algorithms, a scanline polygon fill algorithm, and 2D transformations using homogeneous coordinates.

Uploaded by

pocago6232
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Q1) Write a program to implement DDA and Bresenham’s line drawing algorithm.

#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
// Function for finding absolute value
int abs(int n) {
return ((n > 0) ? n : (n * (-1)));
}
// DDA Function for Line Generation
void DDA(int X0, int Y0, int X1, int Y1) {
// calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;
// calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
// calculate increment in x & y for each step
float Xinc = dx / (float)steps;
float Yinc = dy / (float)steps;
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++) {
putpixel(X, Y, GREEN); // put pixel at (X,Y)
X += Xinc; // increment in X at each step
Y += Yinc; // increment in Y at each step
delay(100); // for visualization of line generation at each step
}
}
// Driver Program
int main() {
int gd = DETECT, gm;
// Initialize graphics function
initgraph(&gd, &gm, "C://TURBOC3//BGI");
int X0 = 20, Y0 = 20, X1 = 140, Y1 = 160;
// Function Call
DDA(X0, Y0, X1, Y1);
getch();

return 0;
}

.
.
.
.
.
.
.
.#include <stdio.h>
#include <conio.h>
#include <graphics.h>
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, RED);
y = y + 1;
p = p + 2 * dy - 2 * dx;
} else {

putpixel(x, y, RED);
p = p + 2 * dy;
}
x = x + 1;
}
}
int main() {
clrscr();
int gdriver = DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}

/250 350
/350 650

Q2) Write a program to implement mid-point circle drawing algorithm.

#include <math.h>
#include <stdlib.h>
#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <dos.h>
void drawCirclePoints(int x, int y, int val, int c_x, int c_y) {
putpixel(c_x + x, c_y + y, val);
putpixel(c_x + y, c_y + x, val);
putpixel(c_x + y, c_y - x, val);
putpixel(c_x + x, c_y - y, val);
putpixel(c_x - x, c_y - y, val);
putpixel(c_x - y, c_y - x, val);
putpixel(c_x - y, c_y + x, val);
putpixel(c_x - x, c_y + y, val);
return;
}
void midpointCircle(int r, int val, int c_x = 0, int c_y = 0) {
int x = 0;
int y = r;
int d = 1 - r;
drawCirclePoints(x, y, val, c_x, c_y);
while (y > x) {
if (d < 0) {
d += 2 * x + 3;
x += 1;
} else {
d += 2 * (x - y) + 5;
x += 1;
y -= 1;
}
drawCirclePoints(x, y, val, c_x, c_y);
}
return;
}
int main() {
clrscr();

int x, y, r;
cout << "Enter Centre (x y): ";
cin >> x >> y;
cout << "Enter Radius (r): ";
cin >> r;
cout << "Drawing Circle..." << endl;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c://turboc3//BGI");
midpointCircle(r, BLUE, x, y);
delay(1500);
closegraph();
getch();
cout << "Finished..." << endl;
return 0;
}

/200 150
/100

Q3) Write a program to clip a line using Cohen and Sutherland line clipping
Algorithm.

#include <conio.h>
#include <graphics.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int outcode;
enum {
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
outcode computeOutcode(double x, double y, double xmin, double xmax, double ymin,
double ymax) {
outcode code = 0;
if (y > ymax)
code |= TOP;
else if (y < ymin)
code |= BOTTOM;
if (x > xmax)
code |= RIGHT;
else if (x < xmin)
code |= LEFT;
return code;
}
void clipLine(double x0, double yo, double x1, double y1, double xmin, double xmax,
double ymin, double ymax) {
int accept = 0, done = 0;
outcode outcode0, outcode1, outcodeout;
outcode0 = computeOutcode(x0, yo, xmin, xmax, ymin, ymax);
outcode1 = computeOutcode(x1, y1, xmin, xmax, ymin, ymax);
do {
if (!(outcode0 | outcode1)) {
accept = 1;
done = 1;
} else if (outcode0 & outcode1) {
done = 1;
} else {
double x, y;
outcodeout = outcode0 ? outcode0 : outcode1;
if (outcodeout & TOP) {
x = x0 + (ymax - yo) * (x1 - x0) / (y1 - yo);
y = ymax;
} else if (outcodeout & BOTTOM) {
x = x0 + (ymin - yo) * (x1 - x0) / (y1 - yo);
y = ymin;
} else if (outcodeout & LEFT) {
y = yo + (xmin - x0) * (y1 - yo) / (x1 - x0);
x = xmin;
} else {
y = yo + (xmax - x0) * (y1 - yo) / (x1 - x0);
x = xmax;
}
if (outcodeout == outcode0) {
x0 = x;
yo = y;
outcode0 = computeOutcode(x0, yo, xmin, xmax, ymin, ymax);
} else {
x1 = x;

y1 = y;
outcode1 = computeOutcode(x1, y1, xmin, xmax, ymin, ymax);
}
}
} while (done == 0);
if (accept)
line(x0, yo, x1, y1);
}
int main(void) {
int gd = DETECT, gm;
double x0, x1, y0, y1;
double xmin, ymin, xmax, ymax;
initgraph(&gd, &gm, "..\\BGI");
cout << "Enter Point A (x0, y0): ";
cin >> x0 >> y0;
cout << "Enter Point B (x1, y1): ";
cin >> x1 >> y1;
cout << "Enter Bounds of Clipping Rectangle : ";
cout << "\n\txmin: ";
cin >> xmin;
cout << "\tymin: ";
cin >> ymin;
cout << "\txmax: ";
cin >> xmax;
cout << "\tymax: ";
cin >> ymax;
clrscr();
line(xmin, ymin, xmax, ymin);
line(xmin, ymin, xmin, ymax);
line(xmin, ymax, xmax, ymax);
line(xmax, ymin, xmax, ymax);
line(x0, y0, x1, y1);
getch();
cleardevice();
line(xmin, ymin, xmax, ymin);
line(xmin, ymin, xmin, ymax);
line(xmin, ymax, xmax, ymax);
line(xmax, ymin, xmax, ymax);
clipLine(x0, y0, x1, y1, xmin, xmax, ymin, ymax);
getch();
closegraph();

return 0;
}
/ 200 50
/200 90
/150
/20
/190
/90

4. Write a program to clip a polygon using Sutherland Hodgeman


algorithm.

#include <conio.h>
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int outcode;
outcode compOutcode(double x, double y);
enum {
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
double xmin, xmax, ymin, ymax;
outcode compOutcode(double x, double y) {
outcode code = 0;
if (y > ymax)

code |= TOP;
else if (y < ymin)
code |= BOTTOM;
if (x > xmax)
code |= RIGHT;
else if (x < xmin)
code |= LEFT;
return code;
}
void clipPolygon(int x0, int y0, int x1, int y1) {
int accept = 0, done = 0;
outcode outcode0, outcode1, outcodeOut;
outcode0 = compOutcode(x0, y0);
outcode1 = compOutcode(x1, y1);
do {
if (!(outcode0 | outcode1)) {
accept = 1;
done = 1;
} else if (outcode0 & outcode1)
done = 1;
else {
double x, y;
outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP) {
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
} else if (outcodeOut & BOTTOM) {
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
} else if (outcodeOut & RIGHT) {
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
} else {
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0) {
x0 = x;
y0 = y;
outcode0 = compOutcode(x0, y0);
} else {

x1 = x;
y1 = y;
outcode1 = compOutcode(x1, y1);
}
}
} while (done == 0);
if (accept)
line(x0, y0, x1, y1);
}
void main() {
int i, n;
int gd = DETECT, gm;
int poly[24];
initgraph(&gd, &gm, "..\\BGI");
cout << "Enter Bounds of Clipping Rectangle: ";
cout << "\n\txmin: ";
cin >> xmin;
cout << "\tymin: ";
cin >> ymin;
cout << "\txmax: ";
cin >> xmax;
cout << "\tymax: ";
cin >> ymax;
cout << "Enter Number of Edges in Polygon: ";
cin >> n;
cout << "Enter Coordinates of the Polygon: ";
for (i = 0; i < 2 * n; i++)
cin >> poly[i];
poly[2 * n] = poly[0];
poly[2 * n + 1] = poly[1];
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
drawpoly(n + 1, poly);
getch();
cleardevice();
rectangle(xmin, ymin, xmax, ymax);
for (i = 0; i < n; i++)
clipPolygon(poly[2 * i], poly[(2 * i) + 1], poly[(2 * i) + 2], poly[(2 * i) +
3]);
getch();
closegraph();

}
/150
/160
/170
/350
/3
130 150 300 190 290 350

5. Write a program to fill a polygon using Scan line fill algorithm.

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

#define MAX_POINTS 20

struct Point {
int x, y;
};

void drawPolygon(Point points[], int numPoints) {


for (int i = 0; i < numPoints; i++) {
int j = (i + 1) % numPoints;
line(points[i].x, points[i].y, points[j].x, points[j].y);
}
}

void scanLineFill(Point points[], int numPoints) {


int y, x1, x2;
for (y = 0; y < getmaxy(); y++) {
int intersections[MAX_POINTS], count = 0;
for (int i = 0; i < numPoints; i++) {
int j = (i + 1) % numPoints;
int y1 = points[i].y, y2 = points[j].y;
if ((y1 <= y && y < y2) || (y2 <= y && y < y1)) {
int dx = points[j].x - points[i].x;
if (dx != 0) {
int intersection = points[i].x + (double)(y - points[i].y) /
(y2 - y1) * dx;
intersections[count++] = intersection;
}
}
}
for (int k = 0; k < count - 1; k += 2) {
x1 = intersections[k];
x2 = intersections[k + 1];
line(x1, y, x2, y);
}
}
}

void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
if (graphresult() != grOk) {
cout << "Graphics initialization failed!" << endl;
return;
}

Point points[MAX_POINTS];
int numPoints;
cout << "Enter the number of vertices of the polygon: ";
cin >> numPoints;
cout << "Enter the coordinates of the vertices (x y):" << endl;
for (int i = 0; i < numPoints; i++) {
cout << "Vertex " << i + 1 << ": ";
cin >> points[i].x >> points[i].y;
}

cleardevice();
drawPolygon(points, numPoints);
scanLineFill(points, numPoints);
getch();
closegraph();
}

/3
/223 322
/350 450
/170 360

6. Write a program to apply various 2D transformations on a 2D


object (use homogenous Coordinates).

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm,s;
initgraph(&gd,&gm,"C://TURBOC3//BGI");
cout<<"1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing "<<endl;
cout<<"Selection:";
cin>>s;
switch(s)
{
case 1:
{
cleardevice();
int x1=200,y1=150,x2=300,y2=250;
int tx=50,ty=50;
cout<<"Rectangle before translation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(4);
cout<<"Rectangle after translation"<<endl;
rectangle(x1+tx,y1+ty,x2+tx,y2+ty);
getch();
break;
}
case 2:
{
cleardevice();
long x1=200,y1=200,x2=300,y2=300;
double a;
cout<<"Rectangle with rotation"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"Angle of rotation:";
cin>>a;
a=(a*3.14)/180;
long xr=x1+((x2-x1)*cos(a)-(y2-y1)*sin(a));
long yr=y1+((x2-x1)*sin(a)+(y2-y1)*cos(a));
setcolor(2);

rectangle(x1,y1,xr,yr);
getch();
break;
}
case 3:
{
cleardevice();
int x1=30,y1=30,x2=70,y2=70,y=2,x=2;
cout<<"Before scaling"<<endl;
setcolor(3);
rectangle(x1,y1,x2,y2);
cout<<"After scaling"<<endl;
setcolor(10);
rectangle(x1*x,y1*y,x2*x,y2*y);
getch();
break;
}
case 4:
{
cleardevice();
int x1=200,y1=300,x2=500,y2=300,x3=350,y3=400;
cout<<"triangle before reflection"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
cout<<"triangle after reflection"<<endl;
setcolor(5);
line(x1,-y1+500,x2,-y2+500);
line(x1,-y1+500,x3,-y3+500);
line(x2,-y2+500,x3,-y3+500);
getch();
break;
}
case 5:
{
cleardevice();
int x1=400,y1=100,x2=600,y2=100,x3=400,y3=200,x4=600,y4=200,shx=2;
cout<<"Before shearing of rectangle"<<endl;
setcolor(3);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);

line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
cout<<"After shearing of rectangle"<<endl;
x1=x1+shx*y1;
x2=x2+shx*y2;
x3=x3+shx*y3;
x4=x4+shx*y4;
setcolor(13);
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x3,y3,x4,y4);
line(x2,y2,x4,y4);
getch();
break;
}
default:
{
cout<<"Invalid Selection"<<endl;
break;
}
}
closegraph();
return 0;
}
/1
/2

7. Write a program to apply various 3D transformations on a 3D


object and then apply parallel and perspective projection on it.

#include <iostream.h>
#include <direct.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <process.h>
int gd = DETECT, gm;
double x1, x2, y2;
void draw_cube(double edge[20][3]) {
double y1;
initgraph(&gd, &gm, NULL);
int i;
clearviewport();
for (i = 0; i < 19; i++) {
x1 = edge[i][0] + edge[i][2] * (cos(2.3562));
y1 = edge[i][1] - edge[i][2] * (sin(2.3562));
x2 = edge[i + 1][0] + edge[i + 1][2] * (cos(2.3562));
y2 = edge[i + 1][1] - edge[i + 1][2] * (sin(2.3562));
line(x1 + 320, 240 - y1, x2 + 320, 240 - y2);
}
line(320, 240, 320, 25);
line(320, 240, 550, 240);
line(320, 240, 150, 410);
getch();
closegraph();
}
void scale(double edge[20][3]) {
double a, b, c;
int i;
cout << "Enter The Scaling Factors: ";
cin >> a >> b >> c;
initgraph(&gd, &gm, NULL);
clearviewport();
for (i = 0; i < 20; i++) {
edge[i][0] = edge[i][0] * a;
edge[i][1] = edge[i][1] * b;
edge[i][2] = edge[i][2] * c;
}
draw_cube(edge);
closegraph();
}
void translate(double edge[20][3]) {
int a, b, c;
int i;
cout << "Enter The Translation Factors: ";
cin >> a >> b >> c;
initgraph(&gd, &gm, NULL);
clearviewport();
for (i = 0; i < 20; i++) {

edge[i][0] += a;
edge[i][1] += b;
edge[i][2] += c;
}
draw_cube(edge);
closegraph();
}
void rotate(double edge[20][3]) {
int ch;
int i;
double temp, theta, temp1;
cout << "-=[ Rotation About ]=-" << endl;
cout << "1:==> X-Axis " << endl;
cout << "2:==> Y-Axis" << endl;
cout << "3:==> Z-Axis " << endl;
cout << "Enter Your Choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter The Angle: ";
cin >> theta;
theta = (theta * 3.14) / 180;
for (i = 0; i < 20; i++) {
edge[i][0] = edge[i][0];
temp = edge[i][1];
temp1 = edge[i][2];
edge[i][1] = temp * cos(theta) - temp1 * sin(theta);
edge[i][2] = temp * sin(theta) + temp1 * cos(theta);
}
draw_cube(edge);
break;
case 2:
cout << "Enter The Angle: ";
cin >> theta;
theta = (theta * 3.14) / 180;
for (i = 0; i < 20; i++) {
edge[i][1] = edge[i][1];
temp = edge[i][0];
temp1 = edge[i][2];
edge[i][0] = temp * cos(theta) + temp1 * sin(theta);
edge[i][2] = -temp * sin(theta) + temp1 * cos(theta);

}
draw_cube(edge);
break;
case 3:
cout << "Enter The Angle: ";
cin >> theta;
theta = (theta * 3.14) / 180;
for (i = 0; i < 20; i++) {
edge[i][2] = edge[i][2];
temp = edge[i][0];
temp1 = edge[i][1];
edge[i][0] = temp * cos(theta) - temp1 * sin(theta);
edge[i][1] = temp * sin(theta) + temp1 * cos(theta);
}
draw_cube(edge);
break;
}
}
void reflect(double edge[20][3]) {
int ch;
int i;
cout << "-=[ Reflection About ]=-" << endl;
cout << "1:==> X-Axis" << endl;
cout << "2:==> Y-Axis " << endl;
cout << "3:==> Z-Axis " << endl;
cout << "Enter Your Choice: ";
cin >> ch;
switch (ch) {
case 1:
for (i = 0; i < 20; i++) {
edge[i][0] = edge[i][0];
edge[i][1] = -edge[i][1];
edge[i][2] = -edge[i][2];
}
draw_cube(edge);
break;
case 2:
for (i = 0; i < 20; i++) {
edge[i][1] = edge[i][1];
edge[i][0] = -edge[i][0];
edge[i][2] = -edge[i][2];

}
draw_cube(edge);
break;
case 3:
for (i = 0; i < 20; i++) {
edge[i][2] = edge[i][2];
edge[i][0] = -edge[i][0];
edge[i][1] = -edge[i][1];
}
draw_cube(edge);
break;
}
}
void perspect(double edge[20][3]) {
int ch;
int i;
double p, q, r;
cout << "-=[ Perspective Projection About ]=-" << endl;
cout << "1:==> X-Axis " << endl;
cout << "2:==> Y-Axis " << endl;
cout << "3:==> Z-Axis" << endl;
cout << "Enter Your Choice := ";
cin >> ch;
switch (ch) {
case 1:
cout << " Enter P := ";
cin >> p;
for (i = 0; i < 20; i++) {
edge[i][0] = edge[i][0] / (p * edge[i][0] + 1);
edge[i][1] = edge[i][1] / (p * edge[i][0] + 1);
edge[i][2] = edge[i][2] / (p * edge[i][0] + 1);
}
draw_cube(edge);
break;
case 2:
cout << " Enter Q := ";
cin >> q;
for (i = 0; i < 20; i++) {
edge[i][1] = edge[i][1] / (edge[i][1] * q + 1);
edge[i][0] = edge[i][0] / (edge[i][1] * q + 1);
edge[i][2] = edge[i][2] / (edge[i][1] * q + 1);

}
draw_cube(edge);
break;
case 3:
cout << " Enter R := ";
cin >> r;
for (i = 0; i < 20; i++) {
edge[i][2] = edge[i][2] / (edge[i][2] * r + 1);
edge[i][0] = edge[i][0] / (edge[i][2] * r + 1);
edge[i][1] = edge[i][1] / (edge[i][2] * r + 1);
}
draw_cube(edge);
break;
}
closegraph();
}
int main() {
initgraph(&gd, &gm, "C://TURBOC3//BGI");
int choice;
double edge[20][3] = {
100, 0, 0,
100, 100, 0,
0, 100, 0,
0, 100, 100,
0, 0, 100,
0, 0, 0,
100, 0, 0,
100, 0, 100,
100, 75, 100,
75, 100, 100,
100, 100, 75,
100, 100, 0,
100, 100, 75,
100, 75, 100,
75, 100, 100,
0, 100, 100,
0, 100, 0,
0, 0, 0,
0, 0, 100,
100, 0, 100
};

while (1) {
cout << "1:==> Draw Cube " << endl;
cout << "2:==> Scaling " << endl;
cout << "3:==> Rotation " << endl;
cout << "4:==> Reflection " << endl;
cout << "5:==> Translation " << endl;
cout << "6:==> Perspective Projection " << endl;
cout << "7:==> Exit " << endl;
cout << "Enter Your Choice := ";
cin >> choice;
switch (choice) {
case 1:
draw_cube(edge);
getch();
break;
case 2:
scale(edge);
getch();
break;
case 3:
rotate(edge);
getch();
break;
case 4:
reflect(edge);
getch();
break;
case 5:
translate(edge);
getch();
break;
case 6:
perspect(edge);
getch();
break;
case 7:
exit(0);
default:
cout << "\nPress A Valid Key...!!! ";
getch();
break;
}

closegraph();
}
}
/1
/2

8. Write a program to draw Hermite /Bezier curve.

BEZIER:
#include <conio.h>
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void bezier(int x[4], int y[4]) {
for (double t = 0.0; t < 1.0; t += 0.00005) {
double xt = pow(1 - t, 3) * x[0] + 3 * t * pow(1 - t, 2) * x[1] + 3 * pow(t, 2)
* (1 - t) * x[2] + pow(t, 3) * x[3];

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


* (1 - t) * y[2] + pow(t, 3) * y[3];
putpixel(xt, yt, WHITE);
}
for (int i = 0; i < 4; i++) {
circle(x[i], y[i], 3);
}
getch();
closegraph();
return;
}
void main() {
int i;
int x[4], y[4];
int gd = DETECT, gm, errorcode;
initgraph(&gd, &gm, "..\\bgi");
for (i = 0; i < 4; i++) {
cout << "Enter Point " << i + 1 << " (x, y): ";
cin >> x[i] >> y[i];
}
bezier(x, y);
return;
}

/ 100 100
/150 170
/200 210
/275 120

HERMITE:

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

#include <stdlib.h>
struct point {
int x, y;
};
void hermite(point p1, point p4, double r1, double r4) {
float x, y, t;
for (t = 0.0; t <= 1.0; t += 0.00005) {
x = (2 * pow(t, 3) - 3 * pow(t, 2) + 1) * p1.x +
(-2 * pow(t, 3) + 3 * pow(t, 2)) * p4.x +
(pow(t, 3) - 2 * pow(t, 2) + t) * r1 +
(pow(t, 3) - pow(t, 2)) * r4;
y = (2 * pow(t, 3) - 3 * pow(t, 2) + 1) * p1.y +
(-2 * pow(t, 3) + 3 * pow(t, 2)) * p4.y +
(pow(t, 3) - 2 * pow(t, 2) + 1) * r1 +
(pow(t, 3) - pow(t, 2)) * r4;
putpixel(x, y, WHITE);
}
circle(p1.x, p1.y, 3);
circle(p4.x, p4.y, 3);
}
void main() {
point p1, p4;
double r1, r4;
int gd = DETECT, gm;
initgraph(&gd, &gm, "..\\BGI");
cout << "Enter Point 1 (x, y): ";
cin >> p1.x >> p1.y;
cout << "Enter Point 2 (x, y): ";
cin >> p4.x >> p4.y;
cout << "Enter Tangent at Point 1: ";
cin >> r1;
cout << "Enter Tangent at Point 4: ";
cin >> r4;
hermite(p1, p4, r1, r4);
getch();
closegraph();
}
/100 100
/225 150
/0
/70

You might also like