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

graphics_lab2

The document contains multiple C++ code snippets demonstrating various graphics functions using the graphics.h library. It includes examples of text functions, color functions, 2D transformations (translation, rotation, scaling), clipping algorithms, Bezier curves, and simple drawings of a car and a man. Additionally, it features a car animation that moves across the screen.

Uploaded by

Bot Arc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

graphics_lab2

The document contains multiple C++ code snippets demonstrating various graphics functions using the graphics.h library. It includes examples of text functions, color functions, 2D transformations (translation, rotation, scaling), clipping algorithms, Bezier curves, and simple drawings of a car and a man. Additionally, it features a car animation that moves across the screen.

Uploaded by

Bot Arc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

8) Text Functions

#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

outtext("This is outtext()");

outtextxy(100, 100, "This is outtextxy() at (100, 100)");

settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);

outtextxy(100, 150, "Text with settextstyle()");

getch();

closegraph();

return 0;

}
9) Color Functions
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

setbkcolor(BLUE);

cleardevice();

setcolor(YELLOW);

outtextxy(100, 100, "Text in Yellow");

int bg = getbkcolor();

cout << "Current background color code: " << bg << endl;

int fg = getcolor();

cout << "Current text color code: " << fg << endl;

getch();

closegraph();

return 0;

}
10(a) 2D Translation
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

int x1 = 100, y1 = 100, x2 = 200, y2 = 150;

int tx = 50, ty = 30;

rectangle(x1, y1, x2, y2);

x1 += tx; y1 += ty;

x2 += tx; y2 += ty;

rectangle(x1, y1, x2, y2);

getch();

closegraph();

return 0;

}
10(b) 2D Rotation
#include <graphics.h>

#include <iostream>

#include <math.h>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

int x = 100, y = 100;

int length = 100;

int angle = 45;

float rad = angle * 3.14159 / 180;

int x2 = x + length * cos(rad);

int y2 = y + length * sin(rad);

line(x, y, x2, y2);

getch();

closegraph();

return 0;

}
10(c) 2D Scaling
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

int x1 = 100, y1 = 100, x2 = 150, y2 = 150;

int sx = 2, sy = 2;

rectangle(x1, y1, x2, y2);

x2 = x1 + (x2 - x1) * sx;

y2 = y1 + (y2 - y1) * sy;

rectangle(x1, y1, x2, y2);

getch();

closegraph();

return 0;

}
11) Cohen-Sutherland Clipping (Basic Demo)
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

int xmin = 100, ymin = 100, xmax = 300, ymax = 300;

rectangle(xmin, ymin, xmax, ymax);

line(50, 50, 350, 350);

getch();

closegraph();

return 0;

}
12) Midpoint Subdivision Clipping (Basic Demo)
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

rectangle(100, 100, 300, 300);

line(50, 200, 350, 200);

getch();

closegraph();

return 0;

}
13) Bezier Curve
#include <graphics.h>

#include <iostream>

#include <math.h>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

int x[4] = {100, 150, 250, 300};

int y[4] = {300, 100, 100, 300};

for (float t = 0; t <= 1; t += 0.001) {

float 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];

float 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);

getch();

closegraph();

return 0;

}
14(a) Car Drawing
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

rectangle(100, 300, 300, 350);

rectangle(150, 250, 250, 300);

circle(130, 360, 10);

circle(270, 360, 10);

getch();

closegraph();

return 0;

}
14(b) Man Drawing
#include <graphics.h>

#include <iostream>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

circle(200, 100, 10);

line(200, 110, 200, 160);

line(200, 120, 180, 140);

line(200, 120, 220, 140);

line(200, 160, 180, 190);

line(200, 160, 220, 190);

getch();

closegraph();

return 0;

}
15) Car Animation
#include <graphics.h>

#include <iostream>

#include <dos.h>

using namespace std;

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, (char*)” “);

for (int i = 0; i < 200; i++) {

cleardevice();

rectangle(100 + i, 300, 300 + i, 350);

rectangle(150 + i, 250, 250 + i, 300);

circle(130 + i, 360, 10);

circle(270 + i, 360, 10);

delay(30);

getch();

closegraph();

return 0;

You might also like