CG Practical Ayush
CG Practical Ayush
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
float x = x1;
float y = y1;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
getch();
return 0;
}
Output DDA
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
int x, y, p, xEnd;
putpixel(x, y, WHITE);
if (p < 0)
p += twoDy;
else {
y++;
p += twoDyMinusDx;
}
putpixel(x, y, WHITE);
}
}
// For lines with slope greater than 1
else {
while (y < y2) {
y++;
if (p < 0)
p += 2 * dx;
else {
x++;
p += twoDyMinusDx;
}
putpixel(x, y, WHITE);
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
int x1, y1, x2, y2;
getch();
closegraph();
return 0;
}
Output
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
int x, y, p, xEnd;
putpixel(x, y, WHITE);
if (p < 0)
p += twoDy;
else {
y++;
p += twoDyMinusDx;
}
putpixel(x, y, WHITE);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
getch();
return 0;
}
Output
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <graphics.h>
// Driver code
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
getch();
return 0;
Output
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
while (x >= y) {
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);
y++;
if (p <= 0)
p = p + 2*y + 1;
else {
x--;
p = p + 2*y - 2*x + 1;
}
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
getch();
closegraph();
return 0;
}
Boundary Fill Algorithm (Using recursive approcah)
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
getch();
closegraph();
return 0;
}
Output
Stack Based
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
getch();
closegraph();
return 0;
}
Flood Fill
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
getch();
closegraph();
return 0;
}
Output
Cohen Sutherland Clipping Algorithm
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
if (x < xmin)
code |= LEFT;
else if (x > xmax)
code |= RIGHT;
if (y < ymin)
code |= BOTTOM;
else if (y > ymax)
code |= TOP;
return code;
}
do {
if (!(code1 | code2)) {
accept = 1;
done = 1;
} else if (code1 & code2) {
done = 1;
} else {
int x, y;
if (code == code1) {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
} else {
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
} while (!done);
if (accept) {
line(x1, y1, x2, y2);
}
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Line coordinates
int x1 = 50, y1 = 150;
int x2 = 200, y2 = 250;
// Draw the original line
line(x1, y1, x2, y2);
getch();
closegraph();
return 0;
}
Output
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
void midpointSubdivision(int x1, int y1, int x2, int y2, int depth) {
if (depth == 0) {
drawLine(x1, y1, x2, y2);
} else {
int xm = (x1 + x2) / 2;
int ym = (y1 + y2) / 2;
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C://Turboc3/bgi");
getch();
closegraph();
return 0;
}
Output
Cyrus Beck
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
typedef struct {
float x, y;
} Point;
typedef struct {
float dx, dy;
} Vector;
typedef struct {
Point p1, p2;
} Line;
int computeRegionCode(Point p, int xmin, int ymin, int xmax, int ymax);
float dotProduct(Vector v1, Vector v2);
int clipTest(float p, float q, float *t1, float *t2);
void cyrusBeckClipping(Line line, int xmin, int ymin, int xmax, int ymax);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
getch();
closegraph();
return 0;
}
int computeRegionCode(Point p, int xmin, int ymin, int xmax, int ymax) {
int code = 0;
return code;
}
if (p < 0.0) {
r = q / p;
if (r > *t2)
return 0;
if (r > *t1)
*t1 = r;
} else if (p > 0.0) {
r = q / p;
if (r < *t1)
return 0;
if (r < *t2)
*t2 = r;
} else if (q < 0.0)
return 0;
return 1;
}
void cyrusBeckClipping(Line line, int xmin, int ymin, int xmax, int ymax) {
Vector D, W;
Point P1, P2;
P1 = line.p1;
P2 = line.p2;
Output
Liang Barsky
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
typedef struct {
float x, y;
} Point;
typedef struct {
float dx, dy;
} Vector;
typedef struct {
Point p1, p2;
} Line;
int clipLineLiangBarsky(Line line, float xmin, float ymin, float xmax, float ymax, float *t1,
float *t2);
void drawLine(float x1, float y1, float x2, float y2);
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
float xmin = 100, ymin = 100, xmax = 400, ymax = 300;
getch();
closegraph();
return 0;
}
int clipLineLiangBarsky(Line line, float xmin, float ymin, float xmax, float ymax, float *t1,
float *t2) {
float dx = line.p2.x - line.p1.x;
float dy = line.p2.y - line.p1.y;
*t1 = 0.0;
*t2 = 1.0;
return 1;
}
Output