GraphicsLab
GraphicsLab
Code -
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
char sName[50], nRoll[15];
void scanInfo() {
prin ("Enter your name: ");
fgets(sName, 49, stdin);
prin ("Enter your enrollment number: ");
fgets(nRoll, 14, stdin);
}
void printInfo() {
setbkcolor(BLACK); // Set background color for be er contrast
setcolor(WHITE); // Set text color
se extstyle(SANS_SERIF_FONT, HORIZ_DIR, 3); // Larger font size
tt
tf
tf
fi
tt
tt
int x = 10, y = 10; // Place at the top-le for be er visibility
ou extxy(x, y, (char *)"Student Name:"); // Explicit cast to avoid warning
ou extxy(x + 150, y, sName);
ou extxy(x, y + 30, (char *)"Enrollment No:");
ou extxy(x + 150, y + 30, nRoll);
}
void drawRandomPixels() {
for (int i = 0; i < 20000; i++) {
int x = rand() % getmaxx();
int y = rand() % getmaxy();
int color = rand() % 16;
putpixel(x, y, color);
}
}
void saveScreenshot(const char * lename) {
writeimage le( lename, 0, 0, getmaxx(), getmaxy());
}
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
if (graphresult() != grOk) {
prin ("Graphics ini aliza on failed!\n");
return 1;
}
setbkcolor(BLACK); // Set background color
cleardevice(); // Apply the background color
scanInfo();
drawRandomPixels();
printInfo();
tt
tt
tt
tt
tf
fi
fi
ti
ti
fi
ft
tt
while (1) {
if (GetAsyncKeyState(VK_CONTROL) & 0x8000) {
if (GetAsyncKeyState('S') & 0x8000) {
saveScreenshot("EXP001.BMP");
prin ("\nScreenshot saved as EXP001.BMP");
}
if (GetAsyncKeyState('C') & 0x8000) {
break;
}
}
}
getch();
closegraph();
return 0;
}
Output -
tf
Assignment 2 .
1 . create an almost maximum sized window /screen
2. create a view port by taking informa on from the user . By default the
screen size and viewport are equal on Pressing 'v' or 'V' the program should ask
for the viewport coordinates
3. write your name and enrolment number at the bo om of the window
4. set pixels in blocks of 3x3 at random within the de ned viewport and in
random colors ( at least 20000 )
5. on Pressing 'C' or 'c' the screen should be cleared
6. save the le on clicking of CTRL + S as EXP002.BMP
7. close the the program on Clicking CTRL +C
8. write your Name and enroment on the Lower right of the screen
9. Explain working of your program
Code –
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
typedef struct { int L, T, R, B; } irect;
irect S, V;
char sName[50], nRoll[15];
void scanfInfo() {
prin ("Enter your name: "); fgets(sName, 49, stdin);
prin ("Enter your Enrollment number: "); fgets(nRoll, 12, stdin);
}
tf
tf
fi
ti
fi
tt
void printInfo(unsigned q) {
char str1[70], str2[70];
sprin (str1, "Student Name: %s", sName);
sprin (str2, "Student Roll: %s", nRoll);
int x = (q % 2) ? getmaxx() - textwidth(str1) - 5 : 5;
int y = (q < 2) ? textheight("X") / 3 : getmaxy() - 2.2 * textheight("X");
ou extxy(x, y, str1);
ou extxy(x, y + 1.1 * textheight("X"), str2);
}
void OnPaint() {
for (int i = 0; i < 20000; i++) {
int x = rand() % (V.R - V.L + 1) + V.L;
int y = rand() % (V.B - V.T + 1) + V.T;
putpixel(x, y, COLOR(0, 0, rand() % 64 + 191));
}
printInfo(2);
}
void clearScreen() { cleardevice(); printInfo(2); }
int readViewport() {
prin ("Enter LEFT TOP RIGHT BOTTOM: ");
scanf("%d %d %d %d", &V.L, &V.T, &V.R, &V.B);
return (V.L < S.L || V.R > S.R || V.T < S.T || V.B > S.B || V.L >= V.R || V.T >= V.B) ? 0 : 1;
}
int main() {
scanfInfo();
initwindow(getmaxwidth() / 2, getmaxheight() / 2, "Assignment 2");
S = V = {0, 0, getmaxx(), getmaxy()};
OnPaint();
char c;
tt
tt
tf
tf
tf
while ((c = getch()) != 3) {
if (c == 'C' || c == 'c') clearScreen();
else if ((c == 'V' || c == 'v') && readViewport()) { cleardevice(); OnPaint(); }
else if (c == 19) writeimage le("EXP002.BMP", 0, 0, getmaxx(), getmaxy());
}
closegraph();
return 0;
}
Output –
fi
Assignment 3 .
2. Allow the user to resize the window dynamically by entering
width and height values.
3. Draw X and Y axes at the center of the window.
4. Label the X and Y axes properly.
5. Display a scale box at the top right indica ng a 100m scale.
6. Draw major grid lines at every 100 pixels.
7. Draw minor grid lines at every 10 pixels, except on mul ples of
100.
8. Con nuously update the coordinate system to match the
window size.
Code –
#include<iostream>
#include <graphics.h>
#include <conio.h>
#include <windows.h> // For Sleep func on
using namespace std;
void drawCoordinateSystem(int width, int height) {
cleardevice();
int originx = width / 2;
int originy = height / 2;
setcolor(WHITE); // Draw X and Y axes
line(0, originy, width, originy);
line(originx, 0, originx, height);
setcolor(GREEN);// Label Axes
ou extxy(width - 30, originy + 10, (char*)"X");
tt
ti
ti
ti
ti
ou extxy(originx + 10, 10, (char*)"Y");
int boxWidth = width / 6; // Responsive Scale Box (Top Right)
int boxHeight = height / 10;
int boxX1 = width - boxWidth - 10;
int boxY1 = 10;
int boxX2 = width - 10;
int boxY2 = boxY1 + boxHeight;
rectangle(boxX1, boxY1, boxX2, boxY2);
ou extxy(boxX1 + 5, boxY1 - 5, (char*)"Scale: 100m");
ou extxy((boxX1 + boxX2) / 2 + 5, boxY1 + boxHeight / 2, (char*)"X : 100m");
ou extxy(boxX1 + 5, (boxY1 + boxY2) / 2 - 10, (char*)"Y : 100m");
for (int i = 0; i < width; i += 100) {
line(i, originy - 10, i, originy + 10);// Draw major grid lines (every 100 units)
}
for (int i = 0; i < height; i += 100) {
line(originx - 10, i, originx + 10, i);
}
for (int i = 0; i < width; i += 10) { // Draw minor grid lines (every 10 units, except
mul ples of 100)
if (i % 100 != 0) {
line(i, originy - 5, i, originy + 5);
}
}
for (int i = 0; i < height; i += 10) {
if (i % 100 != 0) {
line(originx - 5, i, originx + 5, i);
}
}
}
ti
tt
tt
tt
tt
int main() {
int width, height; // Default window size
int n;
cout<<"enter the number of mes you want to change the screen size : ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"enter the screen width : ";
cin>>width;
cout<<"enter the screen height : ";
cin>>height;
// Ini alize resizable graphics window
initwindow(width, height, "Resizable Coordinate System");
while (!kbhit()) { // Run un l a key is pressed
width = getmaxx();
height = getmaxy();
drawCoordinateSystem(width, height);
Sleep(100); // Add slight delay for e ciency
}
}
closegraph();
getch();
return 0;
}
ti
ti
ti
ffi
Output –