CG file
CG file
#include <graphics.h>
#include <stdio.h>
int main()
{
int gd, gm = DETECT;
// initialize graph
initgraph(&gd, &gm, "");
// rectangle coordinate
int top, left, bottom, right;
return 0;
}
Output:
2. Draw a circle and fill the color in it using boundary fill algorithm.
Input:
#include <graphics.h>
#include <iostream>
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 250, y = 200, radius = 50;
circle(x, y, radius);
boundaryFill4(x, y, 6, 15);
delay(10000);
getch();
closegraph();
return 0;
}
Output:
3. Draw 2 shapes and fill the color using 4-connected region and 8-connected region respectively.
Input:
#include <opencv2/opencv.hpp>
int main() {
Mat image = Mat::zeros(400, 600, CV_8UC3);
Input:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
int gd = DETECT, gm;
float tx = 100.0, ty = 200.0;
float angle = 90.0;
float radians;
float sx = 2.0, sy = 3.0;
float shx = 2.0;
int i = 0, n = 3;
/* Function prototypes */
void translate(float x[], float y[], float tx, float ty);
void rotate2D(float x[], float y[]);
void scale2D(float x[], float y[]);
void shearX(float x[], float y[]);
void reflectX(float x[], float y[]);
void reflectY(float x[], float y[]);
void reflectOrigin(float x[], float y[]);
void translate(float x[], float y[], float tx, float ty) {
for (i = 0; i < n; i++) {
x[i] = x[i] + tx;
y[i] = y[i] + ty;
}
}
void rotate2D(float x[], float y[]) {
for (i = 0; i < n; i++) {
float tempX = x[i] * cos(radians) - y[i] * sin(radians);
float tempY = x[i] * sin(radians) + y[i] * cos(radians);
x[i] = tempX;
y[i] = tempY;
}
}
void scale2D(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = x[i] * sx;
y[i] = y[i] * sy;
}
}
void shearX(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = x[i] + shx * y[i];
}
}
void reflectX(float x[], float y[]) {
for (i = 0; i < n; i++) {
y[i] = -y[i];
}
}
void reflectY(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = -x[i];
}
}
void reflectOrigin(float x[], float y[]) {
for (i = 0; i < n; i++) {
x[i] = -x[i];
y[i] = -y[i];
}
}
int main() {
float x[10], y[10];
float tx_x[10], tx_y[10];
float rot_x[10], rot_y[10];
float sc_x[10], sc_y[10];
float sh_x[10], sh_y[10];
float refx_x[10], refx_y[10];
float refy_x[10], refy_y[10];
float refO_x[10], refO_y[10];
/* Define a triangle */
x[0] = 100; y[0] = 100;
x[1] = 200; y[1] = 100;
x[2] = 150; y[2] = 200;
/* Convert angle to radians */
radians = angle * M_PI / 180.0;
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw original triangle */
setcolor(WHITE);
for (i = 0; i < n; i++)
line(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n]);
/* Translation */
for (i = 0; i < n; i++) {
tx_x[i] = x[i];
tx_y[i] = y[i];
}
translate(tx_x, tx_y, tx, ty);
setcolor(RED);
for (i = 0; i < n; i++)
line(tx_x[i], tx_y[i], tx_x[(i + 1) % n], tx_y[(i + 1) % n]);
/* Rotation */
for (i = 0; i < n; i++) {
rot_x[i] = x[i];
rot_y[i] = y[i];
}
rotate2D(rot_x, rot_y);
setcolor(GREEN);
for (i = 0; i < n; i++)
line(rot_x[i], rot_y[i], rot_x[(i + 1) % n], rot_y[(i + 1) % n]);
/* Scaling */
for (i = 0; i < n; i++) {
sc_x[i] = x[i];
sc_y[i] = y[i];
}
scale2D(sc_x, sc_y);
setcolor(BLUE);
for (i = 0; i < n; i++)
line(sc_x[i], sc_y[i], sc_x[(i + 1) % n], sc_y[(i + 1) % n]);
/* Shearing */
for (i = 0; i < n; i++) {
sh_x[i] = x[i];
sh_y[i] = y[i];
}
shearX(sh_x, sh_y);
setcolor(YELLOW);
for (i = 0; i < n; i++)
line(sh_x[i], sh_y[i], sh_x[(i + 1) % n], sh_y[(i + 1) % n]);
/* Reflection about X-axis */
for (i = 0; i < n; i++) {
refx_x[i] = x[i];
refx_y[i] = y[i];
}
reflectX(refx_x, refx_y);
setcolor(MAGENTA);
for (i = 0; i < n; i++)
line(refx_x[i], refx_y[i], refx_x[(i + 1) % n], refx_y[(i + 1) % n]);
/* Reflection about Y-axis */
for (i = 0; i < n; i++) {
refy_x[i] = x[i];
refy_y[i] = y[i];
}
reflectY(refy_x, refy_y);
setcolor(CYAN);
for (i = 0; i < n; i++)
line(refy_x[i], refy_y[i], refy_x[(i + 1) % n], refy_y[(i + 1) % n]);
/* Reflection about Origin (Z-axis equivalent) */
for (i = 0; i < n; i++) {
refO_x[i] = x[i];
refO_y[i] = y[i];
}
reflectOrigin(refO_x, refO_y);
setcolor(LIGHTGREEN);
for (i = 0; i < n; i++)
line(refO_x[i], refO_y[i], refO_x[(i + 1) % n], refO_y[(i + 1) % n]);
getch();
closegraph();
return 0;
}
Output:
Department of Computer Science & Technology
Course: B. Tech Semester: VI
Session: 2024-25 Subject: Computer Graphics and Multimedia CSH310-P
Input:
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int gd = DETECT, gm;
int i = 0;
int n = 3; /* Number of vertices (triangle) */
/* Original triangle coordinates */
float x_orig[3] = {100.0, 200.0, 150.0};
float y_orig[3] = {100.0, 100.0, 200.0};
/* Translation parameters */
float Tx1 = 50.0, Ty1 = 50.0;
float Tx2 = 100.0, Ty2 = 100.0;
/* Scaling parameters */
float Sx = 2.0, Sy = 2.0;
/* Temporary arrays for transformations */
float x_temp[3], y_temp[3];
float x_res[3], y_res[3];
/* Function prototypes */
void translate(float x[], float y[], float tx, float ty);
void scale2D(float x[], float y[], float sx, float sy);
void translate(float x[], float y[], float tx, float ty) {
for (i = 0; i < n; i++) {
x[i] = x[i] + tx;
y[i] = y[i] + ty;
}
}
void scale2D(float x[], float y[], float sx, float sy) {
for (i = 0; i < n; i++) {
x[i] = x[i] * sx;
y[i] = y[i] * sy;
}
}
int main() {
/* Initialize graphics mode */
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
/* Draw original triangle */
setcolor(WHITE);
for (i = 0; i < n; i++) {
line((int)x_orig[i], (int)y_orig[i], (int)x_orig[(i+1)%n], (int)y_orig[(i+1)%n]);
}
/* 1. Translation by (Tx1,Ty1) then (Tx2,Ty2) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
setcolor(RED);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
/* 2. Scaling by (Sx,Sy) then Translation by (Tx1,Ty1) and (Tx2,Ty2) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
scale2D(x_temp, y_temp, Sx, Sy);
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
setcolor(GREEN);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
/* 3. Translation by (Tx1,Ty1) and (Tx2,Ty2) then Scaling by (Sx,Sy) */
for (i = 0; i < n; i++) {
x_temp[i] = x_orig[i];
y_temp[i] = y_orig[i];
}
translate(x_temp, y_temp, Tx1, Ty1);
translate(x_temp, y_temp, Tx2, Ty2);
scale2D(x_temp, y_temp, Sx, Sy);
setcolor(BLUE);
for (i = 0; i < n; i++) {
line((int)x_temp[i], (int)y_temp[i], (int)x_temp[(i+1)%n], (int)y_temp[(i+1)%n]);
}
getch();
closegraph();
return 0;
}
Output:
Department of Computer Science & Technology
Input:
#include <graphics.h>
#include <conio.h>
#include <dos.h>
int gd2 = DETECT, gm2;
int x = 0, y = 350; /* Initial car position */
int i2 = 0;
void draw_car(int x_pos, int y_pos) {
/* Car body */
rectangle(x_pos, y_pos - 20, x_pos + 100, y_pos + 20);
/* Wheels */
circle(x_pos + 20, y_pos + 25, 10);
circle(x_pos + 80, y_pos + 25, 10);
/* Windows */
line(x_pos + 20, y_pos - 20, x_pos + 40, y_pos - 40);
line(x_pos + 60, y_pos - 20, x_pos + 40, y_pos - 40);
line(x_pos + 60, y_pos - 20, x_pos + 80, y_pos - 40);
}
int main() {
initgraph(&gd2, &gm2, "C:\\TURBOC3\\BGI");
for (i2 = 0; i2 <= getmaxx() + 100; i2 += 5) {
cleardevice();
draw_car(i2, y);
delay(100);
}
getch();
closegraph();
return 0;
}
Output: