--------------START---------------- Set-A Q1. WAP to implement features like changing background color, foreground color, and resizing window. ans: #include #include // Global colors float bgRed = 0.0f, bgGreen = 0.0f, bgBlue = 0.0f; // Background: Black float fgRed = 1.0f, fgGreen = 1.0f, fgBlue = 1.0f; // Foreground: White void display() { // 1. Change Background Color glClearColor(bgRed, bgGreen, bgBlue, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // 2. Change Foreground Color and draw a sample triangle glColor3f(fgRed, fgGreen, fgBlue); glBegin(GL_TRIANGLES); glVertex2f(-0.5f, -0.5f); glVertex2f(0.5f, -0.5f); glVertex2f(0.0f, 0.5f); glEnd(); glFlush(); } // 3. Handle Window Resizing void reshape(int width, int height) { // Prevent divide by zero if (height == 0) height = 1; // Set the viewport to cover the new window dimensions glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Maintain aspect ratio if (width >= height) { gluOrtho2D(-1.0 * width / height, 1.0 * width / height, -1.0, 1.0); } else { gluOrtho2D(-1.0, 1.0, -1.0 * height / width, 1.0 * height / width); } glMatrixMode(GL_MODELVIEW); } // Keyboard controls to change colors interactively void keyboard(unsigned char key, int x, int y) { if (key == 'b') { // Change background to Blue bgRed = 0.1f; bgGreen = 0.2f; bgBlue = 0.6f; } else if (key == 'r') { // Change foreground to Red fgRed = 1.0f; fgGreen = 0.0f; fgBlue = 0.0f; } else if (key == 'g') { // Change foreground to Green fgRed = 0.0f; fgGreen = 1.0f; fgBlue = 0.0f; } glutPostRedisplay(); // Redraw the window } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Initial window size and creation glutInitWindowSize(500, 500); glutCreateWindow("Set A: Resizing and Colors"); std::cout << "Controls:\nPress 'b' to change Background\nPress 'r' or 'g' to change Foreground color\n"; glutDisplayFunc(display); glutReshapeFunc(reshape); // Registers the resize callback glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } Q2.Explain polygon clipping and discuss the basic steps involved in polygon clipping. ans:Polygon clipping is the process of removing the portions of a polygon that lie outside a specific bounded region, known as the clipping window. Unlike line clipping, polygon clipping must output a closed shape (or multiple closed shapes) rather than just independent broken line segments. Basic Steps Involved in Polygon Clipping: 1. Inside to Inside: Both vertices are inside the clipping boundary. • Action: Save the destination vertex \(V_{2}\). 2. Inside to Outside: The path moves from inside the window to outside. • Action: Compute the intersection point \(I\) with the boundary line and save \(I\). 3. Outside to Outside: Both vertices are completely outside the clipping boundary. • Action: No vertices are saved. 4. Outside to Inside: The path moves from outside the window back inside. • Action: Compute the intersection point \(I\) and save both \(I\) and the destination vertex \(V_{2}\). ------------ Set-B Q1: DDA Line Drawing Algorithm (Program) ans: #include #include void drawLineDDA(int x1, int y1, int x2, int y2) { // Calculate differences int dx = x2 - x1; int dy = y2 - y1; // Determine the number of steps required int steps = std::max(std::abs(dx), std::abs(dy)); // Calculate increment in x & y for each step float xIncrement = (float)dx / (float)steps; float yIncrement = (float)dy / (float)steps; // Start coordinates float x = x1; float y = y1; std::cout << "Step\tX (Plotted)\tY (Plotted)\tExact (X, Y)\n"; std::cout << "--------------------------------------------------------\n"; for (int i = 0; i <= steps; i++) { // round() is used to get the nearest pixel coordinate std::cout << i << "\t" << std::round(x) << "\t\t" << std::round(y) << "\t\t(" << x << ", " << y << ")\n"; x += xIncrement; y += yIncrement; } } int main() { // Example: Draw a line from (2, 2) to (8, 6) int x1 = 2, y1 = 2, x2 = 8, y2 = 6; std::cout << "Generating points using DDA Line Algorithm:\n"; drawLineDDA(x1, y1, x2, y2); return 0; } Q2.Display technology and it's advantage, limitations and applications Are ye display technology led LCD ans: 1. Display Technology: Advantages, Limitations, and Applications Display technologies are categorized primarily into older CRT (Cathode Ray Tube) systems and modern Flat-Panel Displays (like LCD, LED, and OLED). Here is a summary of modern display systems: • Advantages: High resolution, crisp image sharpness, space-saving (thin profile), light weight, low power consumption, and zero radiation emissions compared to CRT monitors. • Limitations: Limited viewing angles on cheaper panels, potential color degradation over time, slower pixel response times (causing ghosting in fast-moving videos), and high replacement costs if a panel fractures. • Applications: Television screens, computer monitors, smartphone interfaces, medical imaging screens, automotive dashboard displays, and digital advertising billboards. 2. Are ye display technology LED LCD? Yes, LED displays are actually a specialized type of LCD. A traditional LCD (Liquid Crystal Display) uses liquid crystals to block or pass light to form an image, but it requires an external backlight source. • Older LCDs used CCFLs (Cold Cathode Fluorescent Lamps) as their backlight source. • Modern "LED" displays are simply LCD panels that swap out those bulky fluorescent lamps for LEDs (Light Emitting Diodes) as their backlight source. ----------------- Set-C Q1.Program of midpoint circle algorithm ans: #include // Function to print/plot pixels in all 8 octants void plotCirclePoints(int xc, int yc, int x, int y) { std::cout << "(" << xc + x << ", " << yc + y << ")\n"; std::cout << "(" << xc - x << ", " << yc + y << ")\n"; std::cout << "(" << xc + x << ", " << yc - y << ")\n"; std::cout << "(" << xc - x << ", " << yc - y << ")\n"; std::cout << "(" << xc + y << ", " << yc + x << ")\n"; std::cout << "(" << xc - y << ", " << yc + x << ")\n"; std::cout << "(" << xc + y << ", " << yc - x << ")\n"; std::cout << "(" << xc - y << ", " << yc - x << ")\n"; } void midpointCircle(int xc, int yc, int r) { int x = 0; int y = r; int p = 1 - r; // Initial decision parameter // Plot the initial point plotCirclePoints(xc, yc, x, y); while (x < y) { x++; if (p < 0) { p += 2 * x + 1; } else { y--; p += 2 * (x - y) + 1; } plotCirclePoints(xc, yc, x, y); } } int main() { int xc = 0, yc = 0, r = 5; std::cout << "Points for circle centered at (" << xc << "," << yc << ") with radius " << r << ":\n"; midpointCircle(xc, yc, r); return 0; } Q2.WAP to two dimensional transformation - translation, rotation, scaling ans: #include #include void translate(double &x, double &y, double tx, double ty) { x += tx; y += ty; } void scale(double &x, double &y, double sx, double sy) { x *= sx; y *= sy; } void rotate(double &x, double &y, double angleDegrees) { // Convert degrees to radians double radians = angleDegrees * M_PI / 180.0; double x_new = x * cos(radians) - y * sin(radians); double y_new = x * sin(radians) + y * cos(radians); x = x_new; y = y_new; } int main() { double x = 4.0, y = 3.0; std::cout << "Original coordinates: (" << x << ", " << y << ")\n\n"; // 1. Translation double tx = 2.0, ty = 5.0; translate(x, y, tx, ty); std::cout << "After Translation by (" << tx << "," << ty << "): (" << x << ", " << y << ")\n"; // 2. Scaling double sx = 2.0, sy = 2.0; scale(x, y, sx, sy); std::cout << "After Scaling by factor (" << sx << "," << sy << "): (" << x << ", " << y << ")\n"; // 3. Rotation double angle = 90.0; rotate(x, y, angle); std::cout << "After Rotation by " << angle << " degrees: (" << x << ", " << y << ")\n"; return 0; } -------------- Set-D Q1. WAP to implement the DDA Line Drawing Algorithm using the given coordinates ans: #include #include void drawLineDDA(int x1, int y1, int x2, int y2) { int dx = x2 - x1; int dy = y2 - y1; // The step count is determined by the greater absolute difference int steps = std::max(std::abs(dx), std::abs(dy)); float xIncrement = (float)dx / (float)steps; float yIncrement = (float)dy / (float)steps; float x = x1; float y = y1; std::cout << "\nGenerated Line Points:\n"; std::cout << "Step\tX (Rounded)\tY (Rounded)\n"; std::cout << "-----------------------------------\n"; for (int i = 0; i <= steps; i++) { std::cout << i << "\t" << std::round(x) << "\t\t" << std::round(y) << "\n"; x += xIncrement; y += yIncrement; } } int main() { int x1, y1, x2, y2; std::cout << "Enter Start Coordinates (x1 y1): "; std::cin >> x1 >> y1; std::cout << "Enter End Coordinates (x2 y2): "; std::cin >> x2 >> y2; drawLineDDA(x1, y1, x2, y2); return 0; } Q2. WAP to implement the Mid-Point Circle Drawing Algorithm to draw a circle with a given radius and centre coordinates ans: #include // Subroutine to print matching pixels across all 8 symmetric octants void plotSymmetricPoints(int xc, int yc, int x, int y) { std::cout << "(" << xc + x << ", " << yc + y << ")\n"; std::cout << "(" << xc - x << ", " << yc + y << ")\n"; std::cout << "(" << xc + x << ", " << yc - y << ")\n"; std::cout << "(" << xc - x << ", " << yc - y << ")\n"; std::cout << "(" << xc + y << ", " << yc + x << ")\n"; std::cout << "(" << xc - y << ", " << yc + x << ")\n"; std::cout << "(" << xc + y << ", " << yc - x << ")\n"; std::cout << "(" << xc - y << ", " << yc - x << ")\n"; } void midpointCircleDrawing(int xc, int yc, int r) { int x = 0; int y = r; int p = 1 - r; // Initial decision parameter std::cout << "\nGenerated Circle Boundary Points:\n"; plotSymmetricPoints(xc, yc, x, y); while (x < y) { x++; if (p < 0) { p += 2 * x + 1; } else { y--; p += 2 * (x - y) + 1; } plotSymmetricPoints(xc, yc, x, y); } } int main() { int xc, yc, radius; std::cout << "Enter Center Coordinates (xc yc): "; std::cin >> xc >> yc; std::cout << "Enter Radius: "; std::cin >> radius; midpointCircleDrawing(xc, yc, radius); return 0; } ----------------- Set-E Q1.Explain the Bresenham Line Algorithm in Detail ans: The Bresenham Line Drawing Algorithm is an efficient graphics algorithm that determines which pixels on a 2D raster grid should be selected to form a close approximation of a straight line between two given points. Core Mathematical Concept For a line with a slope \(0 \leq m \leq 1\), as we step along the \(X\)-axis by \(1\) unit (\(x_{k+1} = x_k + 1\)), the true line's \(Y\)-coordinate increases continuously. The pixel grid restricts us to discrete whole integers, forcing a choice between two pixel options: • Lower Pixel (\(S\)): Keep the same height \(\rightarrow y_{k}\) • Upper Pixel (\(T\)): Move up one row \(\rightarrow y_{k} + 1\) The decision is guided by a Decision Parameter (\(P_{k}\)), which represents the relative difference between the distances from the true line path to these two pixel options. • If \(P_k < 0\), the line is closer to the lower pixel (\(y_{k+1} = y_k\)). • If \(P_k \geq 0\), the line is closer to the upper pixel (\(y_{k+1} = y_k + 1\)). Algorithmic Steps (for \(0 \leq m \leq 1\)): 1. Input the two endpoints \((x_1, y_1)\) and \((x_2, y_2)\). 2. Calculate the differences: \(dx = x_2 - x_1\) and \(dy = y_2 - y_1\). 3. Compute the starting decision parameter: \(P_{0}=2dy-dx\) 4. At each step \(k\), starting at \(k = 0\), evaluate \(P_{k}\): • If \(P_k < 0\), the next point is \((x_k + 1, y_k)\) and: \(P_{k+1}=P_{k}+2dy\) • If \(P_k \geq 0\), the next point is \((x_k + 1, y_k + 1)\) and: \(P_{k+1}=P_{k}+2dy-2dx\) 5. Repeat step 4 until \(x\) reaches \(x_{2}\). Example Problem Using Decision Parameters Question: Digitize a line from \((0, 0)\) to \((5, 3)\) using Bresenham's algorithm. Given Data: • \((x_1, y_1) = (0, 0)\), \((x_2, y_2) = (5, 3)\) • \(dx = 5 - 0 = 5\) • \(dy = 3 - 0 = 3\) • Constants needed: \(2dy = 6\), \ \(2dy - 2dx = 6 - 10 = -4\) • Initial decision parameter: \(P_0 = 2dy - dx = 6 - 5 = 1\) Step-by-Step Calculation Table: \(k\) \(P_{k}\) Plot \((x_{k+1}, y_{k+1})\) Next \(P_{k+1}\) Calculation Initial — \((0,0)\) \(P_0 = 1\) 0 \(1 \ (\geq 0)\) \((1, 1)\) \(P_1 = 1 + (-4) = -3\) 1 \(-3 \ (< 0)\) \((2, 1)\) \(P_2 = -3 + 6 = 3\) 2 \(3 \ (\geq 0)\) \((3, 2)\) \(P_3 = 3 + (-4) = -1\) 3 \(-1 \ (< 0)\) \((4, 2)\) \(P_4 = -1 + 6 = 5\) 4 \(5 \ (\geq 0)\) \((5, 3)\) Stop (reached endpoint) Q2.Derivation of Composite Transformation Matrix: Rotation \(\rightarrow \) Scaling \(\rightarrow \) Translation ans: To combine multiple sequential 2D operations into a single composite transformation matrix, we must represent coordinates in Homogeneous Coordinates (3x3 matrices). This ensures all operations can be performed uniformly via matrix multiplication. A point is represented as a column vector: \(P=\left[\begin{matrix}x\\ y\\ 1\end{matrix}\right]\) Individual Transformation Matrices 1. Rotation (\(R\)) by an angle \(\theta \) around the origin: \(R=\left[\begin{matrix}\cos \theta &-\sin \theta &0\\ \sin \theta &\cos \theta &0\\ 0&0&1\end{matrix}\right]\) 2. Scaling (\(S\)) by factors \(s_{x}\) and \(s_{y}\): \(S=\left[\begin{matrix}s_{x}&0&0\\ 0&s_{y}&0\\ 0&0&1\end{matrix}\right]\) 3. Translation (\(T\)) by displacements \(t_{x}\) and \(t_{y}\): \(T=\left[\begin{matrix}1&0&t_{x}\\ 0&1&t_{y}\\ 0&0&1\end{matrix}\right]\) Composite Matrix Multiplication Sequence When transformations are specified in the sequence Rotation, then Scaling, then Translation, the matrices are applied from right to left to the coordinate vector: \(P^{\prime }=T\cdot S\cdot R\cdot P\) Let's compute the composite matrix \(M = T \cdot S \cdot R\) in steps. Step 1: Compute \((S \cdot R)\) \(S\cdot R=\left[\begin{matrix}s_{x}&0&0\\ 0&s_{y}&0\\ 0&0&1\end{matrix}\right]\left[\begin{matrix}\cos \theta &-\sin \theta &0\\ \sin \theta &\cos \theta &0\\ 0&0&1\end{matrix}\right]\) \(S\cdot R=\left[\begin{matrix}s_{x}\cos \theta &-s_{x}\sin \theta &0\\ s_{y}\sin \theta &s_{y}\cos \theta &0\\ 0&0&1\end{matrix}\right]\) Step 2: Multiply by Translation \(T\) on the left, \(M = T \cdot (S \cdot R)\) \(M=\left[\begin{matrix}1&0&t_{x}\\ 0&1&t_{y}\\ 0&0&1\end{matrix}\right]\left[\begin{matrix}s_{x}\cos \theta &-s_{x}\sin \theta &0\\ s_{y}\sin \theta &s_{y}\cos \theta &0\\ 0&0&1\end{matrix}\right]\) Performing row-by-column multiplication yields the Final Composite Matrix (\(M\)): \(M=\left[\begin{matrix}s_{x}\cos \theta &-s_{x}\sin \theta &t_{x}\\ s_{y}\sin \theta &s_{y}\cos \theta &t_{y}\\ 0&0&1\end{matrix}\right]\) ----------- Set-F Q1. Write the program to locate the pixel/points on a graph sheet ans: #include #include const int GRID_SIZE = 11; // Must be odd to have a balanced center origin void drawGraphSheet(int pointX, int pointY) { // Determine the center origin of our graph sheet matrix int originX = GRID_SIZE / 2; int originY = GRID_SIZE / 2; // Convert the user's graph coordinates to matrix index indices // In matrices, row index goes downwards, so we subtract Y int targetRow = originY - pointY; int targetCol = originX + pointX; std::cout << "\n--- Graph Sheet Simulation (" << GRID_SIZE << "x" << GRID_SIZE << ") ---\n\n"; for (int r = 0; r < GRID_SIZE; r++) { for (int c = 0; c < GRID_SIZE; c++) { // 1. Locate and plot the target user pixel point if (r == targetRow && c == targetCol) { std::col << "P "; // 'P' denotes the located point } // 2. Draw the Origin center crosshair else if (r == originY && c == originX) { std::cout << "+ "; } // 3. Draw the Y-axis else if (c == originX) { std::cout << "| "; } // 4. Draw the X-axis else if (r == originY) { std::cout << "- "; } // 5. Draw empty background grid cells else { std::cout << ". "; } } std::cout << "\n"; } std::cout << "\nLegend: (+) Origin, (.) Grid Intersection, (P) Your Point\n"; } int main() { int x, y; int maxLimit = GRID_SIZE / 2; std::cout << "Enter custom point coordinates to locate on the graph sheet.\n"; std::cout << "Values must be between -" << maxLimit << " and " << maxLimit << ".\n"; std::cout << "Enter X and Y: "; std::cin >> x >> y; // Boundary check to ensure the point fits on the sheet if (std::abs(x) > maxLimit || std::abs(y) > maxLimit) { std::cout << "Error: Coordinates are out of bounds for this graph sheet layout.\n"; } else { drawGraphSheet(x, y); } return 0; } Q2.Explain the 2D Viewing Pipeline ans: The 2D Viewing Pipeline describes the step-by-step sequence of transformations required to output a 2D scene description from its original mathematical coordinates onto a physical display device screen. The pipeline ensures that objects created in independent coordinate scales are correctly aligned, clipped, and rendered into a target window layout. Sequence of the 2D Viewing Pipeline: [Object Coordinates] │ (Modeling Transformation) ▼ [World Coordinates] │ (Viewing Transformation / Window-to-Viewport Mapping) ▼ [Normalized Device Coordinates] │ (Device Transformation) ▼ [Device / Screen Coordinates] Detailed Breakdown of Each Step: 1. Object / Model Coordinates (MC): • What it is: The local coordinate system used when defining an individual object or shape on its own (e.g., creating a unit circle centered at its own local origin \(0,0\)). 2. World Coordinates (WC): • What it is: The master global coordinate system where all independent objects are assembled together into a collective scene. • Transformation: Modeling Transformations (translation, scaling, rotation) position, size, and orient each object within the larger world context. 3. Viewing Coordinates (VC): • What it is: This defines the specific spatial region of the world coordinate scene that needs to be displayed. This window is selected using a rectangular bounding box called the Window (or World Window). Any portions of objects falling outside this box are discarded during the clipping phase. 4. Normalized Device Coordinates (NDC): • What it is: A device-independent coordinate system that normalizes all visible coordinates into a uniform range (typically scaling everything between \(0\) to \(1\) or \(-1\) to \(1\)). • Transformation: The world window area is mapped to a target display sub-region called the Viewport via a mathematical Window-to-Viewport transformation matrix. 5. Device Coordinates (DC): • What it is: The actual hardware pixels of your physical hardware screen or window application interface (e.g., a \(1920 \times 1080\) display monitor). • Transformation: The final scan-conversion maps the normalized device coordinates onto the physical hardware grid resolution. ----------------END------------------