Cgrs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

This code is intended to perform various 2D geometric transformations (translation, rotation,

scaling, and rotation about an arbitrary point) on a shape using the graphics.h library in Turbo C.

However, there are a few issues and missing parts, so I’ll correct and complete the code and provide
an explanation.

EXP 6:

#include<graphics.h>

#include<conio.h>

#include<math.h>

#include<stdio.h>

int main()

int i, x, y, tx, ty, sx, sy, angle=10, xmax, ymax, xmid, ymid, op;

int gd, gm;

float p1[10] = {

50, 50,

100, 50,

100, 100,

50, 100,

50, 50

}; // Initial coordinates of the shape (square)

int pi[10];

float b[3][3] = {

1, 0, 0,

0, 1, 0,

0, 0, 1

}; // Transformation matrix

int c[1][2]; // Resulting coordinates after transformation

float a[1][2]; // For current coordinates being transformed


printf("\nSelect the transformation: ");

printf("\n1 : Translation");

printf("\n2 : Rotation");

printf("\n3 : Scaling");

printf("\n4 : Rotation about an arbitrary point");

printf("\nEnter the option: ");

scanf("%d", &op);

switch(op)

case 1:

printf("\nEnter x translation: ");

scanf("%d", &tx);

printf("\nEnter y translation: ");

scanf("%d", &ty);

b[0][0] = 1;

b[0][1] = 0;

b[0][2] = 0;

b[1][0] = 0;

b[1][1] = 1;

b[1][2] = 0;

b[2][0] = tx;

b[2][1] = ty;

b[2][2] = 1;

break;

case 2:

printf("\nEnter Rotation angle (in degrees): ");

scanf("%d", &angle);

b[0][0] = cos(angle * 3.14159 / 180);

b[0][1] = sin(angle * 3.14159 / 180);


b[0][2] = 0;

b[1][0] = -sin(angle * 3.14159 / 180);

b[1][1] = cos(angle * 3.14159 / 180);

b[1][2] = 0;

b[2][0] = 0;

b[2][1] = 0;

b[2][2] = 1;

break;

case 3:

printf("\nEnter x scaling factor: ");

scanf("%d", &sx);

printf("\nEnter y scaling factor: ");

scanf("%d", &sy);

b[0][0] = sx;

b[0][1] = 0;

b[0][2] = 0;

b[1][0] = 0;

b[1][1] = sy;

b[1][2] = 0;

b[2][0] = 0;

b[2][1] = 0;

b[2][2] = 1;

break;

case 4:

printf("\nEnter x coordinate of arbitrary point: ");

scanf("%d", &x);

printf("\nEnter y coordinate of arbitrary point: ");

scanf("%d", &y);

printf("\nEnter Rotation angle (in degrees): ");


scanf("%d", &angle);

tx = x;

ty = y;

b[0][0] = cos(angle * 3.14159 / 180);

b[0][1] = sin(angle * 3.14159 / 180);

b[0][2] = 0;

b[1][0] = -sin(angle * 3.14159 / 180);

b[1][1] = cos(angle * 3.14159 / 180);

b[1][2] = 0;

b[2][0] = -tx * cos(angle * 3.14159 / 180) + ty * sin(angle * 3.14159 / 180) + tx;

b[2][1] = -tx * sin(angle * 3.14159 / 180) - ty * cos(angle * 3.14159 / 180) + ty;

b[2][2] = 1;

break;

detectgraph(&gd, &gm);

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); // Initialize graphics

xmax = getmaxx(); // Get maximum x coordinate

ymax = getmaxy(); // Get maximum y coordinate

xmid = xmax / 2; // Get center x coordinate

ymid = ymax / 2; // Get center y coordinate

setcolor(1);

line(xmid, 0, xmid, ymax); // Draw y-axis

line(0, ymid, xmax, ymid); // Draw x-axis

setcolor(4);

for (i = 0; i < 8; i = i + 2)

line(p1[i] + xmid, ymid - p1[i + 1], xmid + p1[i + 2], ymid - p1[i + 3]);

}
for (i = 0; i < 9; i = i + 2)

a[0][0] = p1[i];

a[0][1] = p1[i + 1];

c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0] + b[2][0];

c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1] + b[2][1];

pi[i] = c[0][0];

pi[i + 1] = c[0][1];

setcolor(15);

for (i = 0; i < 8; i = i + 2)

line(xmid + pi[i], ymid - pi[i + 1], xmid + pi[i + 2], ymid - pi[i + 3]);

getch();

closegraph();

return 0;

}
Exp 7:

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <dos.h>

#include <math.h>

#include <graphics.h>

/* Defining structure for end point of line */

typedef struct coordinate

int x, y;

char code[4];

} PT;

void drawwindow();

void drawline(PT p1, PT p2, int cl);

PT setcode(PT p);

int visibility(PT p1, PT p2);

PT resetendpt(PT p1, PT p2);

int main()

int gd = DETECT, gm, v;

PT p1, p2;

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

cleardevice();

printf("\n\n\t\tENTER END-POINT 1 (x,y): ");

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


printf("\n\n\t\tENTER END-POINT 2 (x,y): ");

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

cleardevice();

drawwindow(); // Draw the clipping window

getch();

drawline(p1, p2, 15); // Draw the original line

getch();

p1 = setcode(p1); // Set region codes for the endpoints

p2 = setcode(p2);

v = visibility(p1, p2); // Check the visibility of the line

switch (v)

case 0:

cleardevice(); // Line completely visible

drawwindow();

drawline(p1, p2, 15);

break;

case 1:

cleardevice(); // Line completely invisible

drawwindow();

break;

case 2:

cleardevice(); // Line partly visible, clip the line

p1 = resetendpt(p1, p2); // Find new clipped endpoints

p2 = resetendpt(p2, p1);
drawwindow();

drawline(p1, p2, 15);

break;

getch();

closegraph();

return 0;

/* Function to draw window */

void drawwindow()

setcolor(RED);

line(150, 100, 450, 100); // Top edge

line(450, 100, 450, 350); // Right edge

line(450, 350, 150, 350); // Bottom edge

line(150, 350, 150, 100); // Left edge

/* Function to draw line between two points */

void drawline(PT p1, PT p2, int cl)

setcolor(cl);

line(p1.x, p1.y, p2.x, p2.y);

/* Function to set code of the coordinates */

PT setcode(PT p)

PT ptemp;
if (p.y < 100)

ptemp.code[0] = '1'; // TOP

else

ptemp.code[0] = '0';

if (p.y > 350)

ptemp.code[1] = '1'; // BOTTOM

else

ptemp.code[1] = '0';

if (p.x > 450)

ptemp.code[2] = '1'; // RIGHT

else

ptemp.code[2] = '0';

if (p.x < 150)

ptemp.code[3] = '1'; // LEFT

else

ptemp.code[3] = '0';

ptemp.x = p.x;

ptemp.y = p.y;

return ptemp;

/* Function to determine visibility of line */

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; // Line completely visible

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

if ((p1.code[i] == p2.code[i]) && (p1.code[i] == '1'))

return 1; // Line completely invisible

return 2; // Line partly visible

/* Function to find new end points */

PT resetendpt(PT p1, PT p2)

PT temp;

int x, y, i;

float m, k;

// Cutting LEFT or RIGHT Edge

if (p1.code[3] == '1') // Left edge

x = 150;

if (p1.code[2] == '1') // Right edge

x = 450;

if (p1.code[3] == '1' || p1.code[2] == '1')


{

m = (float)(p2.y - p1.y) / (p2.x - p1.x); // Slope

k = p1.y + m * (x - p1.x); // New y-coordinate

temp.y = k;

temp.x = x;

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

temp.code[i] = p1.code[i];

if (temp.y <= 350 && temp.y >= 100)

return temp;

// Cutting TOP or BOTTOM Edge

if (p1.code[0] == '1') // Top edge

y = 100;

if (p1.code[1] == '1') // Bottom edge

y = 350;

if (p1.code[0] == '1' || p1.code[1] == '1')

m = (float)(p2.y - p1.y) / (p2.x - p1.x); // Slope

k = p1.x + (y - p1.y) / m; // New x-coordinate

temp.x = k;

temp.y = y;

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

temp.code[i] = p1.code[i];
return temp;

return p1;

}
Exp 8

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

#include <process.h>

int gd, gm;

float xxx[4][2];

/* Function to draw line from relative position specified in array xxx */

void line1(float x2, float y2) {

line((int)xxx[0][0], (int)xxx[0][1], (int)x2, (int)y2);

xxx[0][0] = x2;

xxx[0][1] = y2;

/* Bezier function */

void bezier(float xb, float yb, float xc, float yc, float xd, float yd, int n) {

float xab, yab, xbc, ybc, xcd, ycd;

float xabc, yabc, xbcd, ybcd;

float xabcd, yabcd;

if (n == 0) {

line1(xb, yb);

line1(xc, yc);

line1(xd, yd);

} else {

xab = (xxx[0][0] + xb) / 2;

yab = (xxx[0][1] + yb) / 2;

xbc = (xb + xc) / 2;

ybc = (yb + yc) / 2;


xcd = (xc + xd) / 2;

ycd = (yc + yd) / 2;

xabc = (xab + xbc) / 2;

yabc = (yab + ybc) / 2;

xbcd = (xbc + xcd) / 2;

ybcd = (ybc + ycd) / 2;

xabcd = (xabc + xbcd) / 2;

yabcd = (yabc + ybcd) / 2;

n = n - 1;

bezier(xab, yab, xabc, yabc, xabcd, yabcd, n);

bezier(xbcd, ybcd, xcd, ycd, xd, yd, n);

/* Function to initialize graphics */

void igraph() {

gd = DETECT;

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

if (graphresult() != 0) {

puts("CANNOT DETECT A GRAPHICS CARD");

exit(1);

void main() {

int i;

float temp1, temp2;

igraph();

/* Read two end points and two control points of the curve */

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


printf("Enter (x,y) coordinates of point%d: ", i + 1);

scanf("%f,%f", &temp1, &temp2);

xxx[i][0] = temp1;

xxx[i][1] = temp2;

/* Draw the Bezier curve */

bezier(xxx[1][0], xxx[1][1], xxx[2][0], xxx[2][1], xxx[3][0], xxx[3][1], 8);

getch();

closegraph();

}
Exp9:

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#define SIN 0.86602540 // sin(60 degrees)

/* Function to draw the Koch curve */

void koch(int x1, int y1, int x2, int y2, int m) {

int xx, yy, x[5], y[5], lx, ly;

int offx = 50, offy = 300;

lx = (x2 - x1) / 3;

ly = (y2 - y1) / 3;

x[0] = x1; // Store point p0

y[0] = y1;

x[4] = x2; // Store point p4

y[4] = y2;

x[1] = x[0] + lx; // Store point p1

y[1] = y[0] + ly;

x[3] = x[0] + 2 * lx; // Store point p3

y[3] = y[0] + 2 * ly;

// Translate point p2 to origin

xx = x[3] - x[1];

yy = y[3] - y[1];

// Perform rotation for point p2

x[2] = (int)(xx * 0.5 + yy * SIN);

y[2] = (int)(-xx * SIN + yy * 0.5);


// Perform inverse translation

x[2] = x[2] + x[1];

y[2] = y[2] + y[1];

if (m > 0) {

// Recursive calls to draw parts of the Koch curve

koch(x[0], y[0], x[1], y[1], m - 1); // Part 1

koch(x[1], y[1], x[2], y[2], m - 1); // Part 2

koch(x[2], y[2], x[3], y[3], m - 1); // Part 3

koch(x[3], y[3], x[4], y[4], m - 1); // Part 4

} else {

// Draw the lines for the current level

line(offx + x[0], offy + y[0], offx + x[1], offy + y[1]);

line(offx + x[1], offy + y[1], offx + x[2], offy + y[2]);

line(offx + x[2], offy + y[2], offx + x[3], offy + y[3]);

line(offx + x[3], offy + y[3], offx + x[4], offy + y[4]);

void main() {

int n, gd, gm;

int x1 = 0, x2 = 550, y1 = 0, y2 = 0;

/* Initialize graphics mode */

printf("\nEnter the level of curve generation: ");

scanf("%d", &n);

gd = DETECT;

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

if (graphresult() != 0) {
printf("Error initializing graphics mode\n");

exit(1);

// Draw Koch curve

koch(x1, y1, x2, y2, n);

getch();

closegraph();

}
Exp 10:

#include <stdio.h>

#include <stdlib.h>

#include <graphics.h>

#include <conio.h>

void draw3d(int s, int x[20], int y[20], int d);

void main() {

int gd = DETECT, gm;

int x[20], y[20], i, s, d;

// Initialize graphics mode

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

// Input number of sides

printf("Enter the number of sides: ");

scanf("%d", &s);

// Clear input buffer

while (getchar() != '\n');

// Input coordinates for each vertex

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

printf("(x%d, y%d): ", i, i);

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

// Clear input buffer after each scanf

while (getchar() != '\n');

}
// Input depth for the 3D effect

printf("Depth: ");

scanf("%d", &d);

// Clear input buffer

while (getchar() != '\n');

// Draw the 3D shape

draw3d(s, x, y, d);

getch();

// Top view - shifted to the right

setcolor(14);

for (i = 0; i < s - 1; i++) {

line(x[i] + 200, y[i], x[i + 1] + 200, y[i + 1]);

line(x[i] + 200, y[i], x[0] + 200, y[0]);

getch(); // Top view

// Side view - horizontal lines with depth

for (i = 0; i < s - 1; i++) {

line(x[i], 300, x[i + 1], 300);

line(x[i], 300 + d * 2, x[i + 1], 300 + d * 2);

line(x[i], 300, x[i], 300 + d * 2);

line(x[i + 1], 300, x[i + 1], 300 + d * 2);

getch(); // Side view


// Front view - vertical lines with depth

for (i = 0; i < s - 1; i++) {

line(10, y[i], 10, y[i + 1]);

line(10 + d * 2, y[i], 10 + d * 2, y[i + 1]);

line(10, y[i], 10 + d * 2, y[i]);

line(10, y[i + 1], 10 + d * 2, y[i + 1]);

getch();

// Close graphics mode

closegraph();

// Function to draw a 3D shape

void draw3d(int s, int x[20], int y[20], int d) {

int i, j, k = 0;

// Draw the front and back faces of the shape

for (j = 0; j < 2; j++) {

for (i = 0; i < s - 1; i++) {

line(x[i] + k, y[i] - k, x[i + 1] + k, y[i + 1] - k);

line(x[i] + k, y[i] - k, x[0] + k, y[0] - k);

k = d; // Shift for the back face

// Draw the connecting lines for the 3D effect

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

line(x[i], y[i], x[i] + d, y[i] - d);

}}

You might also like