0% found this document useful (0 votes)
43 views150 pages

CG LAB ASSIGNMENTmajor

Uploaded by

Shiny Chakma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views150 pages

CG LAB ASSIGNMENTmajor

Uploaded by

Shiny Chakma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 150

COMPUTER

GRAPHICS LAB
ASSIGNMENT

NAME- SHINY CHAKMA


ROLL NO.- 2022IMT-102

Lab Assignment 1

Question 1
Code:

#include <GL/glut.h>

// Initialize the OpenGL Graphics


void init() {
// Set the background color
glClearColor(0.1, 0.7, 0.5, 0.0);
// Set the shading model to flat
glShadeModel(GL_FLAT);
}

// Display callback function


void display() {
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);

// Set the color to white


//this line can be written inside glbegin
glColor3f(1.0, 1.0, 1.0);

// Draw a rectangle
glBegin(GL_POLYGON);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();

// Flush the drawing to the window


glFlush();
}
// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);

// Set display mode


glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set the window size


glutInitWindowSize(500, 500);

// Set the window position


glutInitWindowPosition(100, 100);

// Create the window with a title


glutCreateWindow("Simple GLUT Display");

// Initialize the OpenGL graphics


init();

// Set the display callback function


glutDisplayFunc(display);

// Enter the GLUT event loop


glutMainLoop();

return 0;
}
OUTPUT-

Lab Assignment 2

Question 2.1

Code:

#include <GLUT/glut.h>
#include <cstdio> // Include for sprintf

// Function to display an empty screen


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the window
glFlush(); // Finish rendering
}

// Function to initialize OpenGL with a specific background color


void init(float red, float green, float blue) {
glClearColor(red, green, blue, 1.0f); // Set the background color
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);

// List of resolutions and corresponding colors


int resolutions[][2] = {
{128, 128}, // Yellow
{256, 256}, // Blue
{512, 512}, // Default black
{256, 360} // Green
};

float colors[][3] = {
{1.0f, 1.0f, 0.0f}, // Yellow
{0.0f, 0.0f, 1.0f}, // Blue
{0.0f, 0.0f, 0.0f}, // Black (default)
{0.0f, 1.0f, 0.0f} // Green
};

// Create a window for each resolution


for (int i = 0; i < 4; i++) {
char title[50];
sprintf(title, "Window %dx%d", resolutions[i][0], resolutions[i][1]); // Format
window title

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(resolutions[i][0], resolutions[i][1]);
glutCreateWindow(title);
// Initialize the background color for this window
init(colors[i][0], colors[i][1], colors[i][2]);

glutDisplayFunc(display);
}

// Start the GLUT main loop


glutMainLoop();

return 0;
}

Output:
Question 2.2

Code:

#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the color for the point (Baby Blue)


glColor3f(0.68f, 0.85f, 0.9f);
glPointSize(10.0f);

glBegin(GL_POINTS);
glVertex2f(0.0f, 0.0f); // Draw the point at the center of the window
glEnd();
glFlush();
}

void createWindow(int width, int height, const char* title,


float bgRed, float bgGreen, float bgBlue,
float pointRed, float pointGreen, float pointBlue,
float pointSize) {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(width, height);
glutCreateWindow(title);

glClearColor(bgRed, bgGreen, bgBlue, 1.0f);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

glColor3f(pointRed, pointGreen, pointBlue);


glPointSize(pointSize);

glutDisplayFunc(display);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
createWindow(800, 600, "OpenGL Window with Point",
1.0f, 0.75f, 0.79f, // Baby Pink background
0.68f, 0.85f, 0.9f, // Baby Blue point
10.0f);
glutMainLoop();
return 0;
}

Output:

Question 2.3

Code:
#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the x-axis and y-axis


// glColor3f(0.0f, 0.0f, 0.0f); // Black for axes
// glLineWidth(2.0f);
// glBegin(GL_LINES);
// glVertex2f(-1.0f, 0.0f); // X-axis from left to right
// glVertex2f(15.0f, 0.0f);
// glVertex2f(0.0f, -1.0f); // Y-axis from bottom to top
// glVertex2f(0.0f, 10.0f);
// glEnd();

// Set point color to dark lavender


glColor3f(0.59f, 0.47f, 0.71f);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2f(0.0f, 0.0f); // Point at (0, 0)
glVertex2f(10.0f, 0.0f); // Point at (10, 0)
glVertex2f(5.0f, 8.0f); // Point at (5, 8)
glEnd();

glFlush();
}

void init() {
glClearColor(0.9f, 0.9f, 0.98f, 1.0f); // Lavender background

// Set up orthographic projection to match point coordinates


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 15.0, -1.0, 10.0, -1.0, 1.0); // Left, Right, Bottom, Top, Near, Far
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Three Points with Axes");
init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output:

Question 2.4
Code:
#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the point size and color for the "C" and "G"
glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points

// Draw the points for "C"


glBegin(GL_POINTS);
glVertex2f(3.0f, 6.0f);
glVertex2f(2.0f, 7.0f);
glVertex2f(1.0f, 6.0f);
glVertex2f(1.0f, 4.0f);
glVertex2f(1.0f, 2.0f);
glVertex2f(2.0f, 1.0f);
glVertex2f(3.0f, 2.0f);
glEnd();

// Draw the points for "G"


glBegin(GL_POINTS);
glVertex2f(7.0f, 6.0f);
glVertex2f(6.0f, 7.0f);
glVertex2f(5.0f, 6.0f);
glVertex2f(5.0f, 4.0f);
glVertex2f(5.0f, 2.0f);
glVertex2f(6.0f, 1.0f);
glVertex2f(7.0f, 2.0f);
glVertex2f(7.0f, 3.0f);
glVertex2f(6.0f, 3.0f);
glEnd();
glFlush();
}

void init() {
glClearColor(0.9f, 0.9f, 0.98f, 1.0f); // Lavender background

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 8.0, 0.0, 10.0, -1.0, 1.0); // Adjust to fit both letters
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("CG Letters");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output:
Question 2.5

Code:

#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the point size and color for the points


glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points

// Draw the points


glBegin(GL_POINTS);
glVertex2f(1.0f, 1.0f); // First point at (1, 1)
glVertex2f(7.0f, 8.0f); // Second point at (7, 8)
glEnd();
// Set the color for the line (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line between the two points


glBegin(GL_LINES);
glVertex2f(1.0f, 1.0f); // First point
glVertex2f(7.0f, 8.0f); // Second point
glEnd();

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 8.0, 0.0, 10.0, -1.0, 1.0); // Adjust to fit both points
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Line Between Two Points");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output:

Question 2.6

Code:
#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the point size and color for the points


glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points

// Draw the points


glBegin(GL_POINTS);
glVertex2f(2.0f, 1.0f); // Bottom-left point
glVertex2f(4.0f, 1.0f); // Bottom-right point
glVertex2f(3.0f, 5.0f); // Top point (A apex)
glVertex2f(2.5f, 3.0f); // Left middle point (A inner left)
glVertex2f(3.5f, 3.0f); // Right middle point (A inner right)
glEnd();

glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line segments to make the letter A


glBegin(GL_LINES);

glVertex2f(2.0f, 1.0f); // Bottom-left


glVertex2f(3.0f, 5.0f); // Top point (A apex)
glVertex2f(4.0f, 1.0f); // Bottom-right
glVertex2f(3.0f, 5.0f); // Top point (A apex)

glVertex2f(2.5f, 3.0f); // Left middle


glVertex2f(3.5f, 3.0f); // Right middle
glEnd();

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 6.0, 0.0, 6.0, -1.0, 1.0); // Adjust to fit all points of A
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Letter A with Points");
init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output-

Question 2.7

Code:
#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the point size and color for the points


glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points

// Draw the points (coordinates chosen to form a star)


glBegin(GL_POINTS);
glVertex2f(3.0f, 6.0f); // Top point
glVertex2f(5.0f, 4.5f); // Right top point
glVertex2f(5.0f, 2.5f); // Bottom-right point
glVertex2f(1.0f, 2.5f); // Bottom-left point
glVertex2f(1.0f, 4.5f); // Left top point
glVertex2f(3.0f, 1.0f); // Center point
glEnd();

// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line segments to make the star


glBegin(GL_LINES);
// Connect the points to form a star shape
glVertex2f(3.0f, 6.0f); // Top
glVertex2f(5.0f, 2.5f); // Bottom-right

glVertex2f(1.0f, 4.5f); // Left top


glVertex2f(5.0f, 4.5f); // Right top
glVertex2f(1.0f, 2.5f); // Bottom-left
glVertex2f(3.0f, 6.0f); // Top

glVertex2f(5.0f, 2.5f); // Bottom-right point


glVertex2f(1.0f, 2.5f); // Bottom-left point

// Connecting center of the star to all points

glVertex2f(3.0f, 1.0f); // Center


glVertex2f(5.0f, 4.5f); // Right top

glVertex2f(3.0f, 1.0f); // Center


glVertex2f(1.0f, 4.5f); // Left top
glEnd();

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 7.0, 0.0, 7.0, -1.0, 1.0); // Adjust to fit all points of the star
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Star Shape");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output:

Question 2.8
Code:

#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the point size and color for the points


glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points

// Draw the points (coordinates chosen to form a rectangle)


glBegin(GL_POINTS);
glVertex2f(1.0f, 1.0f); // Bottom-left point
glVertex2f(5.0f, 1.0f); // Bottom-right point
glVertex2f(5.0f, 4.0f); // Top-right point
glVertex2f(1.0f, 4.0f); // Top-left point
glEnd();

// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line segments to form a rectangle


glBegin(GL_LINES);
glVertex2f(1.0f, 1.0f); // Bottom-left
glVertex2f(5.0f, 1.0f); // Bottom-right

glVertex2f(5.0f, 1.0f); // Bottom-right


glVertex2f(5.0f, 4.0f); // Top-right
glVertex2f(5.0f, 4.0f); // Top-right
glVertex2f(1.0f, 4.0f); // Top-left

glVertex2f(1.0f, 4.0f); // Top-left


glVertex2f(1.0f, 1.0f); // Bottom-left
glEnd();
glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 6.0, 0.0, 6.0, -1.0, 1.0); // Adjust to fit all points of the rectangle
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Rectangle Shape");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output-

Question 2.9

Code:
#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);
// Set the point size and color for the points
glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points
// Draw the points (coordinates chosen to form a pentagon)
glBegin(GL_POINTS);
glVertex2f(3.0f, 5.0f); // Top point
glVertex2f(1.5f, 3.0f); // Top-left point
glVertex2f(2.0f, 0.5f); // Bottom-right point
glVertex2f(4.0f, 0.5f); // Bottom-right point
glVertex2f(4.5f, 3.0f); // Top-right point
glEnd();

// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line segments to form a pentagon


glBegin(GL_LINES);
glVertex2f(3.0f, 5.0f); // Top point
glVertex2f(1.5f, 3.0f); // Top-left point

glVertex2f(1.5f, 3.0f); // Top-left point


glVertex2f(2.0f, 0.5f); // Bottom-right point

glVertex2f(2.0f, 0.5f); // Bottom-right point


glVertex2f(4.0f, 0.5f); // Bottom-right point

glVertex2f(4.0f, 0.5f); // Bottom-right point


glVertex2f(4.5f, 3.0f); // Top-right point

glVertex2f(4.5f, 3.0f); // Top-right point


glVertex2f(3.0f, 5.0f); // Top point
glEnd();
glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 6.0, 0.0, 6.0, -1.0, 1.0); // Adjust to fit all points of the pentagon
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Pentagon Shape");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}
Output:

Question 2.10

Code:

#include <GLUT/glut.h>

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Set the point size and color for the points


glPointSize(10.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black for the points
// Draw the points (coordinates chosen to form a hexagon)
glBegin(GL_POINTS);
glVertex2f(3.0f, 5.0f); // Top point
glVertex2f(1.0f, 3.5f); // Bottom-left point
glVertex2f(1.0f, 1.5f); // Bottom-right point
glVertex2f(3.0f, 0.0f); // Bottom-right point
glVertex2f(5.0f, 1.5f); // Bottom-left point
glVertex2f(5.0f, 3.5f); // Top point
glEnd();

// Set the color for the lines (can be same as points or different)
glColor3f(0.0f, 0.0f, 1.0f); // Line color (blue)

// Draw the line segments to form a hexagon


glBegin(GL_LINES);
glVertex2f(3.0f, 5.0f); // Top point
glVertex2f(1.0f, 3.5f); // Bottom-left point

glVertex2f(1.0f, 3.5f); // Bottom-left point


glVertex2f(1.0f, 1.5f); // Bottom-right point

glVertex2f(1.0f, 1.5f); // Bottom-right point


glVertex2f(3.0f, 0.0f); // Bottom-right point

glVertex2f(3.0f, 0.0f); // Bottom-right point


glVertex2f(5.0f, 1.5f); // Bottom-left point

glVertex2f(5.0f, 1.5f); // Bottom-left point


glVertex2f(5.0f, 3.5f); // Top point

glVertex2f(5.0f, 3.5f); // Top point


glVertex2f(3.0f, 5.0f); // Top point
glEnd();

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

// Set up orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 7.0, -1.0, 6.0, -1.0, 1.0); // Adjust to fit all points of the hexagon
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Hexagon Shape");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output:

Lab Assignment 3

Question 3.1

Code:
#include <GLUT/glut.h>
#include <cmath>
#include <iostream>

void drawStandardLine(int x1, int y1, int x2, int y2) {


glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
}

void drawDDA(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
int dy = y2 - y1;

int steps = std::max(abs(dx), abs(dy));

float xIncrement = dx / float(steps);


float yIncrement = dy / float(steps);

float x = x1;
float y = y1;

glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xIncrement;
y += yIncrement;
}
glEnd();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

// Set line color to blue for the standard line


glColor3f(0.0f, 0.0f, 1.0f);
drawStandardLine(50, 150, 400, 400); // Standard Line (Using Line Equation)

// Set line color to red for the DDA line


glColor3f(1.0f, 0.0f, 0.0f);
drawDDA(50, 50, 400, 300); // DDA Line

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Orthogonal projection
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512); // Set window size to 512x512
glutCreateWindow("Line Drawing Algorithms");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output-

Question 3.2
Code-
#include <GLUT/glut.h>
#include <cmath>
#include <iostream>
#include <chrono> // Include chrono library for time measurement
using namespace std;
using namespace std::chrono;

void drawStandardLine(int x1, int y1, int x2, int y2) {


glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
}

void drawDDA(int x1, int y1, int x2, int y2) {


int dx = x2 - x1;
int dy = y2 - y1;

int steps = std::max(abs(dx), abs(dy));

float xIncrement = dx / float(steps);


float yIncrement = dy / float(steps);

float x = x1;
float y = y1;

glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2i(round(x), round(y));
x += xIncrement;
y += yIncrement;
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

// Measure time for Standard Line


auto startStandardLine = high_resolution_clock::now(); // Start timer
glColor3f(0.0f, 0.0f, 1.0f);
drawStandardLine(50, 150, 400, 400); // Standard Line (Using Line Equation)
auto stopStandardLine = high_resolution_clock::now(); // Stop timer
auto durationStandardLine = duration_cast<microseconds>(stopStandardLine -
startStandardLine); // Calculate duration
cout << "Time taken by Standard Line Algorithm: " << durationStandardLine.count()
<< " microseconds" << endl;

// Measure time for DDA Line


auto startDDA = high_resolution_clock::now(); // Start timer
glColor3f(1.0f, 0.0f, 0.0f);
drawDDA(50, 50, 400, 300); // DDA Line
auto stopDDA = high_resolution_clock::now(); // Stop timer
auto durationDDA = duration_cast<microseconds>(stopDDA - startDDA); //
Calculate duration
cout << "Time taken by DDA Algorithm: " << durationDDA.count() << "
microseconds" << endl;

glFlush();
}

void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Orthogonal projection
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512); // Set window size to 512x512
glutCreateWindow("Line Drawing Algorithms");

init();
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

Output-

Question 3.3
Code-
#include <GL/glut.h>
#include <iostream>
#include <cmath>

// Function to draw a line using the DDA algorithm


void drawLineDDA(float x1, float y1, float x2, float y2) {
float dx = x2 - x1;
float dy = y2 - y1;
float steps = std::max(abs(dx), abs(dy));

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;

glBegin(GL_POINTS);
for (int i = 0; i <= steps; i++) {
glVertex2f(x, y); // Direct coordinates, no need to normalize
x += xIncrement;
y += yIncrement;
}
glEnd();
}

// Function to draw a square


void drawSquare() {
glLineWidth(10.0f); // Set line width for square
glColor3f(1.0f, 0.0f, 0.0f); // Red color

drawLineDDA(50, 50, 50, 150); // Left vertical line


drawLineDDA(50, 150, 150, 150); // Bottom horizontal line
drawLineDDA(150, 150, 150, 50); // Right vertical line
drawLineDDA(150, 50, 50, 50); // Top horizontal line
}

// Function to draw a star


void drawStar() {
glLineWidth(3.0f); // Set line width for star
glColor3f(0.0f, 1.0f, 0.0f); // Green color

// Adjusted star points


float p1[] = {250, 400};
float p2[] = {220, 330};
float p3[] = {180, 330};
float p4[] = {210, 290};
float p5[] = {170, 230};
float p6[] = {250, 270};
float p7[] = {330, 230};
float p8[] = {290, 290};
float p9[] = {320, 330};
float p10[] = {280, 330};

drawLineDDA(p1[0], p1[1], p2[0], p2[1]); // Diagonal line


drawLineDDA(p2[0], p2[1], p3[0], p3[1]); // Diagonal line
drawLineDDA(p3[0], p3[1], p4[0], p4[1]); // Diagonal line
drawLineDDA(p4[0], p4[1], p5[0], p5[1]); // Diagonal line
drawLineDDA(p5[0], p5[1], p6[0], p6[1]); // Diagonal line
drawLineDDA(p6[0], p6[1], p7[0], p7[1]); // Diagonal line
drawLineDDA(p7[0], p7[1], p8[0], p8[1]); // Diagonal line
drawLineDDA(p8[0], p8[1], p9[0], p9[1]); // Diagonal line
drawLineDDA(p9[0], p9[1], p10[0], p10[1]); // Diagonal line
drawLineDDA(p10[0], p10[1], p1[0], p1[1]); // Close star
}

// Function to draw the letter "A"


void drawLetterA() {
glLineWidth(3.0f); // Set line width for letter A
glColor3f(0.0f, 0.0f, 1.0f); // Blue color

// Letter "A" points


float p1[] = {400, 100};
float p2[] = {450, 200};
float p3[] = {500, 100};
float p4[] = {425, 150};
float p5[] = {475, 150};

drawLineDDA(p1[0], p1[1], p2[0], p2[1]); // Left diagonal


drawLineDDA(p2[0], p2[1], p3[0], p3[1]); // Right diagonal
drawLineDDA(p4[0], p4[1], p5[0], p5[1]); // Middle horizontal
}

// Function to draw all shapes


void drawShapes() {
drawSquare();
drawStar();
drawLetterA();
}

// Initialization function
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, 512, 512); // Set the viewport size
glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0); // Set the orthographic projection
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the shapes


drawShapes();

glFlush();
}

// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("DDA Line Drawing for Various Shapes"); // Create the
window

// Initialize OpenGL
init();

// Set the display function


glutDisplayFunc(display);

// Start the main loop


glutMainLoop();
return 0;
}

Output:

Lab Assignment 4

Question 4.1

Code:

#include <GL/glut.h>
#include <cmath>

// Function to initialize the OpenGL environment


void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the matrix
gluOrtho2D(-2.0, 2.0, -2.0, 2.0); // Set orthographic projection
}

// Function to display the circle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

glBegin(GL_POINTS); // Start drawing points


float radius = 1.0; // Circle radius
float centerX = 0.0, centerY = 0.0; // Circle center at (0, 0)
int numSegments = 1000; // Number of points to approximate the circle
for (int i = 0; i < numSegments; i++) {
float angle = 2.0f * M_PI * i / numSegments; // Angle for this segment
float x = centerX + radius * cos(angle); // Calculate x coordinate
float y = centerY + radius * sin(angle); // Calculate y coordinate
glVertex2f(x, y); // Draw the point
}
glEnd();

glFlush(); // Render the contents


}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
color mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 1"); // Create window with title
init(); // Initialize OpenGL
glutDisplayFunc(display); // Set display callback function
glutMainLoop(); // Enter the main loop

return 0;
}

Output:

Question 4.2

Code :

#include <GL/glut.h>
#include <cmath>

// Function to initialize the OpenGL environment


void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the matrix
gluOrtho2D(-256.0, 256.0, -256.0, 256.0); // Set orthographic projection
}

// Function to display the circle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

glBegin(GL_POINTS); // Start drawing points


float radius = 50.0; // Circle radius
float centerX = 20.0, centerY = 20.0; // Circle center at (20, 20)
int numSegments = 1000; // Number of points to approximate the circle
for (int i = 0; i < numSegments; i++) {
float angle = 2.0f * M_PI * i / numSegments; // Angle for this segment
float x = centerX + radius * cos(angle); // Calculate x coordinate
float y = centerY + radius * sin(angle); // Calculate y coordinate
glVertex2f(x, y); // Draw the point
}
glEnd();

glFlush(); // Render the contents


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
color mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 50"); // Create window with title

init(); // Initialize OpenGL


glutDisplayFunc(display); // Set display callback function
glutMainLoop(); // Enter the main loop

return 0;
}

Output:

Question 4.2.a

Code:

#include <GL/glut.h>
#include <cmath>

// Function to initialize OpenGL


void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the matrix
gluOrtho2D(-2.0, 2.0, -2.0, 2.0); // Set orthographic projection
}

// Function to display the circle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

float radius = 1.0; // Circle radius


float centerX = 0.0, centerY = 0.0; // Circle center at (0, 0)
int numSegments = 100; // Number of segments for the circle

glBegin(GL_LINE_LOOP); // Start drawing using line loop


for (int i = 0; i < numSegments; i++) {
float angle = 2.0f * M_PI * i / numSegments; // Calculate angle
float x = centerX + radius * cos(angle); // Calculate x coordinate
float y = centerY + radius * sin(angle); // Calculate y coordinate
glVertex2f(x, y); // Define the vertex
}
glEnd();

glFlush(); // Render the contents


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 1"); // Create the window

init(); // Initialize OpenGL


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter the main loop

return 0;
}

Output:

Question 4.2.b

Code:

#include <GL/glut.h>
#include <cmath>

// Function to initialize OpenGL


void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Set background color to black
glMatrixMode(GL_PROJECTION); // Switch to projection matrix
glLoadIdentity(); // Reset the matrix
gluOrtho2D(-256.0, 256.0, -256.0, 256.0); // Set orthographic projection
}

// Function to display the circle


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

float radius = 50.0; // Circle radius


float centerX = 20.0, centerY = 20.0; // Circle center at (20, 20)
int numSegments = 100; // Number of segments for the circle

glBegin(GL_LINE_LOOP); // Start drawing using line loop


for (int i = 0; i < numSegments; i++) {
float angle = 2.0f * M_PI * i / numSegments; // Calculate angle
float x = centerX + radius * cos(angle); // Calculate x coordinate
float y = centerY + radius * sin(angle); // Calculate y coordinate
glVertex2f(x, y); // Define the vertex
}
glEnd();

glFlush(); // Render the contents


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Single buffer and RGB
mode
glutInitWindowSize(512, 512); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Radius 50"); // Create the window

init(); // Initialize OpenGL


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter the main loop

return 0;
}
Output:

Question 4.3.a

Code:

#include <GL/glut.h>
#include <cmath>

// Function to draw a pixel


void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

// Bresenham's line drawing algorithm


void bresenhamLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;

int stepX = (dx > 0) ? 1 : -1;


int stepY = (dy > 0) ? 1 : -1;

dx = abs(dx);
dy = abs(dy);

if (dx > dy) {


int p = 2 * dy - dx;
while (x != x2) {
drawPixel(x, y);
x += stepX;
if (p >= 0) {
y += stepY;
p -= 2 * dx;
}
p += 2 * dy;
}
} else {
int p = 2 * dx - dy;
while (y != y2) {
drawPixel(x, y);
y += stepY;
if (p >= 0) {
x += stepX;
p -= 2 * dy;
}
p += 2 * dx;
}
}
}

// Function to draw the line


void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the line shifted upwards


bresenhamLine(50, 250, 200, 350); // Line shifted upwards

glFlush();
}

// OpenGL Initialization
void initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: White
glColor3f(0.0, 0.0, 1.0); // Line color: Blue
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 400); // Coordinate system
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing");
initOpenGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Question 4.3.b

Code:

#include <GL/glut.h>
#include <cmath>
// Function to draw a pixel
void drawPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

// Bresenham's line drawing algorithm


void bresenhamLine(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
int x = x1, y = y1;

int stepX = (dx > 0) ? 1 : -1;


int stepY = (dy > 0) ? 1 : -1;

dx = abs(dx);
dy = abs(dy);

if (dx > dy) {


int p = 2 * dy - dx;
while (x != x2) {
drawPixel(x, y);
x += stepX;
if (p >= 0) {
y += stepY;
p -= 2 * dx;
}
p += 2 * dy;
}
} else {
int p = 2 * dx - dy;
while (y != y2) {
drawPixel(x, y);
y += stepY;
if (p >= 0) {
x += stepX;
p -= 2 * dy;
}
p += 2 * dx;
}
}
}

// Function to draw the letter 'A'


void drawA() {
// Left slanting line
bresenhamLine(100, 100, 150, 300); // (x1, y1) -> (x2, y2)

// Right slanting line


bresenhamLine(150, 300, 200, 100);

// Horizontal line in the middle


bresenhamLine(125, 200, 175, 200);
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the letter 'A'


drawA();

glFlush();
}

// OpenGL Initialization
void initOpenGL() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: White
glColor3f(1.0, 0.0, 0.0); // Line color: Red
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 400, 0, 400); // Coordinate system
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing - Letter A");
initOpenGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Question 4.3

Code:

#include <iostream>

#include <GL/glut.h>
#include <cmath>

// Function to implement Bresenham's circle algorithm concentric circle


void bresenhamCircle(int centerX, int centerY, int radius) {
int x = radius;
int y = 0;
int p = 3 - 2 * radius; // Initial decision parameter

// Draw the initial points


glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY - x);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX - y, centerY + x);
glEnd();

while (x > y) {
y++;
if (p > 0) {
x--;
p = p + 4 * (y - x) + 10;
} else {
p = p + 4 * y + 6;
}

glBegin(GL_POINTS);
glVertex2i(centerX + x, centerY - y);
glVertex2i(centerX - x, centerY - y);
glVertex2i(centerX + x, centerY + y);
glVertex2i(centerX - x, centerY + y);
glVertex2i(centerX + y, centerY - x);
glVertex2i(centerX - y, centerY - x);
glVertex2i(centerX + y, centerY + x);
glVertex2i(centerX - y, centerY + x);
glEnd();
}
}
// Display callback function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Set the color to red


glColor3f(1.0f, 0.0f, 0.0f);

// Draw two concentric circles with radii 50 and 100


bresenhamCircle(200, 200, 50);
bresenhamCircle(200, 200, 100);

glFlush(); // Force execution of OpenGL commands in finite time


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Bresenham's Circle Algorithm - Concentric Circles"); // Create
the window

// Set the background color to white


glClearColor(1.0, 1.0, 1.0, 1.0);

// Set the viewport to cover the entire window


glViewport(0, 0, 400, 400);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Create a simple orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutDisplayFunc(display); // Register display callback function


glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

Output:

Lab Assignment 5
Question 5.1

Code:

#include <GL/glut.h>
#include <iostream>

// Function to implement the Midpoint Line Algorithm


void midpointLine(int x0, int y0, int x1, int y1) {
int dx = x1 - x0;
int dy = y1 - y0;

int d = 2 * dy - dx; // Initial decision parameter


int dE = 2 * dy; // Increment for E (East)
int dNE = 2 * (dy - dx); // Increment for NE (Northeast)

int x = x0, y = y0;

glBegin(GL_POINTS); // Start drawing points


glVertex2i(x, y); // Plot the first point

while (x < x1) {


if (d <= 0) { // Choose E (East)
d += dE;
x++;
} else { // Choose NE (Northeast)
d += dNE;
x++;
y++;
}
glVertex2i(x, y); // Plot the current point
}
glEnd();
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Set the color to black


glColor3f(0.0f, 0.0f, 0.0f);

// Draw the line using Midpoint Line Algorithm


midpointLine(50, 50, 300, 200);

glFlush(); // Force execution of OpenGL commands in finite time


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm"); // Create the window

// Set the background color to white


glClearColor(1.0, 1.0, 1.0, 1.0);

// Set the viewport to cover the entire window


glViewport(0, 0, 400, 400);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Create a simple orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutDisplayFunc(display); // Register display callback function


glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

Output:

Question 5.2

Code:

#include <GL/glut.h>
#include <cmath>
// Function to implement the Midpoint Line Algorithm
void midpointLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;

while (true) {
glBegin(GL_POINTS); // Start drawing points
glVertex2i(x0, y0); // Plot the pixel at the current point
glEnd();

if (x0 == x1 && y0 == y1) break;


int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}

// Function to draw the letter 'A' using midpoint line algorithm


void drawLetterA() {
// Set the color for the left diagonal line (black)
glColor3f(0.0f, 0.0f, 0.0f); // Black color
midpointLine(100, 100, 150, 250); // Left diagonal line
// Set the color for the right diagonal line (green)
glColor3f(0.0f, 1.0f, 0.0f); // Green color
midpointLine(150, 250, 200, 100); // Right diagonal line

// Set the color for the horizontal line (red)


glColor3f(1.0f, 0.0f, 0.0f); // Red color
midpointLine(125, 175, 175, 175); // Horizontal line

// Set the color for the bottom blue horizontal line


glColor3f(0.0f, 0.0f, 1.0f); // Blue color
midpointLine(100, 100, 200, 100); // Blue horizontal line at the bottom
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Draw the letter 'A'


drawLetterA();

glFlush(); // Force execution of OpenGL commands in finite time


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm - Letter A"); // Create the window
// Set the background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
// Set the viewport to cover the entire window
glViewport(0, 0, 400, 400);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Create a simple orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutDisplayFunc(display); // Register display callback function
glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

Output:

Question 5.3
Code:

#include <GL/glut.h>
#include <cmath>

// Function to implement the Midpoint Line Algorithm


void midpointLine(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;

while (true) {
glBegin(GL_POINTS); // Start drawing points
glVertex2i(x0, y0); // Plot the pixel at the current point
glEnd();

if (x0 == x1 && y0 == y1) break;


int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x0 += sx;
}
if (e2 < dx) {
err += dx;
y0 += sy;
}
}
}

// Function to draw the arrow shape using midpoint line algorithm


void drawArrowShape() {
// Coordinates of the arrow points (from the image)
int x1 = 100, y1 = 300; // Start point (top-left)
int x2 = 200, y2 = 300; // Right horizontal
int x3 = 250, y3 = 250; // Down-right diagonal
int x4 = 200, y4 = 200; // Down-left diagonal
int x5 = 100, y5 = 200; // Left horizontal
int x6 = 150, y6 = 250; // Up-left diagonal

// Draw lines for the arrow shape


glColor3f(0.0f, 0.0f, 1.0f); // Set the color to blue

midpointLine(x1, y1, x2, y2); // Top horizontal line


midpointLine(x2, y2, x3, y3); // Top-right diagonal line
midpointLine(x3, y3, x4, y4); // Bottom-right diagonal line
midpointLine(x4, y4, x5, y5); // Bottom horizontal line
midpointLine(x5, y5, x6, y6); // Bottom-left diagonal line
midpointLine(x6, y6, x1, y1); // Top-left diagonal line
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Draw the arrow shape


drawArrowShape();

glFlush(); // Force execution of OpenGL commands in finite time


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(400, 400); // Set the window size to 400x400
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Line Algorithm - Arrow Shape"); // Create the
window

// Set the background color to white


glClearColor(1.0, 1.0, 1.0, 1.0);

// Set the viewport to cover the entire window


glViewport(0, 0, 400, 400);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 400.0, 0.0, 400.0, -1.0, 1.0); // Create a simple orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutDisplayFunc(display); // Register display callback function


glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

Output:
Question 5.4

Code:

#include <GL/glut.h>
#include <cmath>

// Function to draw a circle using Midpoint Circle Algorithm


void drawCircle(int xc, int yc, int r) {
int x = 0, y = r;
int d = 1 - r; // Decision parameter

// Plot the initial points in all eight octants


glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y);
glVertex2i(xc - x, yc + y);
glVertex2i(xc + x, yc - y);
glVertex2i(xc - x, yc - y);
glVertex2i(xc + y, yc + x);
glVertex2i(xc - y, yc + x);
glVertex2i(xc + y, yc - x);
glVertex2i(xc - y, yc - x);
glEnd();

while (x < y) {
x++;
if (d < 0) {
d += 2 * x + 1;
} else {
y--;
d += 2 * (x - y) + 1;
}

// Plot the points in all eight octants


glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y);
glVertex2i(xc - x, yc + y);
glVertex2i(xc + x, yc - y);
glVertex2i(xc - x, yc - y);
glVertex2i(xc + y, yc + x);
glVertex2i(xc - y, yc + x);
glVertex2i(xc + y, yc - x);
glVertex2i(xc - y, yc - x);
glEnd();
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Draw first circle (center: 0, 0), radius = 1
glColor3f(1.0f, 0.0f, 0.0f); // Set color to red
drawCircle(0, 0, 1);

// Draw second circle (center: -30, -40), radius = 50


glColor3f(0.0f, 0.0f, 1.0f); // Set color to blue
drawCircle(-30, -40, 50);

glFlush(); // Force execution of OpenGL commands


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Circle Algorithm"); // Create the window

// Set the background color to white


glClearColor(1.0, 1.0, 1.0, 1.0);

// Set the viewport to cover the entire window


glViewport(0, 0, 512, 512);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-256, 256, -256, 256, -1.0, 1.0); // Create an orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutDisplayFunc(display); // Register display callback function


glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

Output:

Question 5.5

Code:

#include <GL/glut.h>
#include <cmath>

// Function to draw an ellipse using the Midpoint Ellipse Algorithm


void drawEllipse(int xc, int yc, int a, int b) {
int x = 0, y = b;
float a2 = a * a;
float b2 = b * b;
float d1 = b2 - a2 * b + 0.25 * a2;
int dx = 2 * b2 * x;
int dy = 2 * a2 * y;

// Region 1
while (dx < dy) {
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y); // First quadrant
glVertex2i(xc - x, yc + y); // Second quadrant
glVertex2i(xc + x, yc - y); // Fourth quadrant
glVertex2i(xc - x, yc - y); // Third quadrant
glEnd();

if (d1 < 0) {
x++;
dx = 2 * b2 * x;
d1 += dx + b2;
} else {
x++;
y--;
dx = 2 * b2 * x;
dy = 2 * a2 * y;
d1 += dx - dy + b2;
}
}

// Region 2
float d2 = b2 * (x + 0.5) * (x + 0.5) + a2 * (y - 1) * (y - 1) - a2 * b2;
while (y >= 0) {
glBegin(GL_POINTS);
glVertex2i(xc + x, yc + y); // First quadrant
glVertex2i(xc - x, yc + y); // Second quadrant
glVertex2i(xc + x, yc - y); // Fourth quadrant
glVertex2i(xc - x, yc - y); // Third quadrant
glEnd();

if (d2 > 0) {
y--;
dy = 2 * a2 * y;
d2 += a2 - dy;
} else {
y--;
x++;
dx = 2 * b2 * x;
dy = 2 * a2 * y;
d2 += dx - dy + a2;
}
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Draw first ellipse (center: 0, 0), (a=20, b=25)


glColor3f(1.0f, 0.0f, 0.0f); // Set color to red
drawEllipse(0, 0, 20, 25);

// Draw second ellipse (center: -20, -40), (a=60, b=40)


glColor3f(0.0f, 0.0f, 1.0f); // Set color to blue
drawEllipse(-20, -40, 60, 40);
glFlush(); // Force execution of OpenGL commands
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set the display mode
glutInitWindowSize(512, 512); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Midpoint Ellipse Algorithm"); // Create the window

// Set the background color to white


glClearColor(1.0, 1.0, 1.0, 1.0);

// Set the viewport to cover the entire window


glViewport(0, 0, 512, 512);

// Set the projection matrix to map window coordinates directly


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-256, 256, -256, 256, -1.0, 1.0); // Create an orthographic projection

// Set the modelview matrix to identity (no transformation)


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glutDisplayFunc(display); // Register display callback function


glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}
Output:

Lab Assignment 6

Question 6.1

Code:

#include <GL/glut.h>
#include <cmath>
#include <string>

const int WIDTH = 800;


const int HEIGHT = 800;
const float PI = 3.14159265358979323846;

void renderText(float x, float y, const std::string& text) {


glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
}
}

void rotatePoint(float& x, float& y, float angle) {


float rad = angle * PI / 180.0;
float newX = x * cos(rad) - y * sin(rad);
float newY = x * sin(rad) + y * cos(rad);
x = newX;
y = newY;
}

void drawCone(float apexX, float apexY, float baseRadius, float height) {


float ellipseRatio = 0.3; // Adjust this value to change the "3D" effect

// Draw the sides of the cone


glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(apexX, apexY);
glVertex2f(apexX - baseRadius, apexY - height);
glVertex2f(apexX, apexY);
glVertex2f(apexX + baseRadius, apexY - height);
glEnd();

// Draw the base ellipse


glBegin(GL_LINE_LOOP);
for (float angle = 0; angle <= 2 * PI; angle += 0.01) {
float x = apexX + baseRadius * cos(angle);
float y = apexY - height + baseRadius * ellipseRatio * sin(angle);
glVertex2f(x, y);
}
glEnd();

// Draw the dashed part of the ellipse (back of the cone)


glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x00FF);
glBegin(GL_LINE_STRIP);
for (float angle = 0; angle <= PI; angle += 0.01) {
float x = apexX + baseRadius * cos(angle);
float y = apexY - height - baseRadius * ellipseRatio * sin(angle);
glVertex2f(x, y);
}
glEnd();
glDisable(GL_LINE_STIPPLE);
}

void drawCircleSection(float centerX, float centerY, float radiusX, float radiusY, const
std::string& label) {
glColor3f(0.996f, 0.502f, 0.506f);
glBegin(GL_POLYGON);
for (int i = 0; i <= 360; i++) {
float angle = i * PI / 180;
glVertex2f(centerX + cos(angle) * radiusX, centerY + sin(angle) * radiusY);
}
glEnd();

glColor3f(0.996f, 0.502f, 0.506f);


renderText(centerX + radiusX + 10, centerY, label);
}

void drawEllipseSection(float rx, float ry, float centerX, float centerY, float angle) {
glColor3f(0.702f, 1.0f, 0.502f);
glBegin(GL_POLYGON);
for (int i = 0; i <= 360; i++) {
float rad = i * PI / 180;
float x = cos(rad) * rx;
float y = sin(rad) * ry;
rotatePoint(x, y, angle);
glVertex2f(x + centerX, y + centerY);
}
glEnd();

glColor3f(0.702f, 1.0f, 0.502f);


renderText(centerX + 80, centerY+20, "ellipse");
}

void drawParabolaSection(float scale, float centerX, float centerY, float angle) {


glColor3f(0.6667, 0.6706, 0.9961);
glBegin(GL_POLYGON);
for (float t = -1.09; t <= 1.07; t += 0.01) {
float x = t * 100;
float y = scale * (t * t * 1.0) * 100;
rotatePoint(x, y, angle);
glVertex2f(x + centerX, y + centerY);
}
glEnd();

glColor3f(0.6667, 0.6706, 0.9961);


renderText(centerX + 40, centerY, "parabola");
}
void drawHyperbolaSection(float scale, float centerX, float centerY, float angle) {
glColor3f(1.0f, 0.6f, 0.3373f);
glBegin(GL_POLYGON);
for (float t = -0.463; t <= 0.31; t += 0.001) {
float x = t * 100;
float y = scale * (t * t * 1.0) * 100;
rotatePoint(x, y, angle);
glVertex2f(x + centerX, y + centerY);
}
glEnd();

glColor3f(1.0f, 0.6f, 0.3373f);


renderText(centerX + 40, centerY, "hyperbola");
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

drawCone(0, 300, 200, 500);


drawCircleSection(0, 200, 37.8, 22, "circle");
drawEllipseSection(75, 30, -5, 110, 12);
drawParabolaSection(2.5, 87.5, 60, 155);
drawHyperbolaSection(5.5, 151, -100, 175);

glutSwapBuffers();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300, 300, -300, 400, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void reshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)w / (float)h;
if (w <= h)
glOrtho(-300, 300, -300/aspect, 400/aspect, -1.0, 1.0);
else
glOrtho(-300*aspect, 300*aspect, -300, 400, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Modular Conic Sections");

init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}

Output:
Question 6.2

Code:

#include <GL/glut.h>
#include <cmath>

int windowWidth = 500;


int windowHeight = 500;

void drawArchInSquare() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0, 0.0, 1.0);


glBegin(GL_LINE_LOOP);
glVertex2f(-4.5, 0.5);
glVertex2f(4.5, 0.5);
glVertex2f(4.5, 5.5);
glVertex2f(-4.5, 5.5);
glEnd();

glColor3f(0.0, 0.0, 0.0);


glBegin(GL_LINE_STRIP);
float a = 0.25;
for (float x = -4.0; x <= 4.0; x += 0.01) {
float y = -a * x * x + 4.85;
glVertex2f(x, y);
}
glEnd();

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, 0.0, 6.0, -1.0, 1.0);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Parabola in Blue Square");
init();
glutDisplayFunc(drawArchInSquare);
glutMainLoop();
return 0;
}

Output:

Lab Assignment 7

Q 7.1.a
Code-

#include <GL/glut.h>

// Define the vertices of a regular tetrahedron


GLfloat vertices[4][3] = {
{ 1.0f, 1.0f, 1.0f },
{-1.0f, -1.0f, 1.0f },
{-1.0f, 1.0f, -1.0f },
{ 1.0f, -1.0f, -1.0f }
};

// Function to draw a triangle's outline


void drawTriangleOutline(int v1, int v2, int v3) {
glBegin(GL_LINE_LOOP); // Draw a loop for the edges of the triangle
glVertex3fv(vertices[v1]);
glVertex3fv(vertices[v2]);
glVertex3fv(vertices[v3]);
glEnd();
}

// Function to render the wireframe tetrahedron


void drawTetrahedron() {
glColor3f(1.0f, 0.0f, 0.0f); // Red
drawTriangleOutline(0, 1, 2);

glColor3f(0.0f, 1.0f, 0.0f); // Green


drawTriangleOutline(0, 3, 1);

glColor3f(0.0f, 0.0f, 1.0f); // Blue


drawTriangleOutline(0, 2, 3);
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
drawTriangleOutline(1, 3, 2);
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
buffers
glLoadIdentity(); // Reset transformations

// Apply transformations to position the tetrahedron


glTranslatef(0.0f, 0.0f, -5.0f); // Move back from the camera
glRotatef(45.0f, 1.0f, 1.0f, 0.0f); // Rotate for better view

drawTetrahedron(); // Draw the tetrahedron wireframe


glutSwapBuffers(); // Swap the front and back buffers
}

// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background

// Set up the projection using glFrustum()


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Tetrahedron"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output:

Q. 7.1.b

Code-
#include <GL/glut.h>

// Define the vertices of a hexahedron (cube)


GLfloat vertices[8][3] = {
{ 1.0f, 1.0f, 1.0f }, // Vertex 0
{-1.0f, 1.0f, 1.0f }, // Vertex 1
{-1.0f, -1.0f, 1.0f }, // Vertex 2
{ 1.0f, -1.0f, 1.0f }, // Vertex 3
{ 1.0f, 1.0f, -1.0f }, // Vertex 4
{-1.0f, 1.0f, -1.0f }, // Vertex 5
{-1.0f, -1.0f, -1.0f }, // Vertex 6
{ 1.0f, -1.0f, -1.0f } // Vertex 7
};

// Function to draw the outline of a quadrilateral (square face)


void drawQuadOutline(int v1, int v2, int v3, int v4) {
glBegin(GL_LINE_LOOP);
glVertex3fv(vertices[v1]);
glVertex3fv(vertices[v2]);
glVertex3fv(vertices[v3]);
glVertex3fv(vertices[v4]);
glEnd();
}

// Function to render the wireframe hexahedron


void drawHexahedron() {
glColor3f(1.0f, 0.0f, 0.0f); // Red
drawQuadOutline(0, 1, 2, 3); // Front face

glColor3f(0.0f, 1.0f, 0.0f); // Green


drawQuadOutline(4, 5, 6, 7); // Back face
glColor3f(0.0f, 0.0f, 1.0f); // Blue
drawQuadOutline(0, 1, 5, 4); // Top face

glColor3f(1.0f, 1.0f, 0.0f); // Yellow


drawQuadOutline(2, 3, 7, 6); // Bottom face

glColor3f(1.0f, 0.0f, 1.0f); // Magenta


drawQuadOutline(0, 3, 7, 4); // Right face

glColor3f(0.0f, 1.0f, 1.0f); // Cyan


drawQuadOutline(1, 2, 6, 5); // Left face
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
buffers
glLoadIdentity(); // Reset transformations

// Apply transformations to position the hexahedron


glTranslatef(0.0f, 0.0f, -5.0f); // Move it back
glRotatef(45.0f, 1.0f, 1.0f, 0.0f); // Rotate for better view

drawHexahedron(); // Draw the hexahedron wireframe


glutSwapBuffers(); // Swap the front and back buffers
}

// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background
// Set up the projection using glFrustum()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Hexahedron"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output-
Q. 7.1.c

Code-

#include <GL/glut.h>

// Define the vertices of an octahedron


GLfloat vertices[6][3] = {
{ 1.0f, 0.0f, 0.0f }, // Vertex 0 (Right)
{-1.0f, 0.0f, 0.0f }, // Vertex 1 (Left)
{ 0.0f, 1.0f, 0.0f }, // Vertex 2 (Top)
{ 0.0f, -1.0f, 0.0f }, // Vertex 3 (Bottom)
{ 0.0f, 0.0f, 1.0f }, // Vertex 4 (Front)
{ 0.0f, 0.0f, -1.0f } // Vertex 5 (Back)
};
// Function to draw the outline of a triangular face
void drawTriangleOutline(int v1, int v2, int v3) {
glBegin(GL_LINE_LOOP);
glVertex3fv(vertices[v1]);
glVertex3fv(vertices[v2]);
glVertex3fv(vertices[v3]);
glEnd();
}

// Function to render the wireframe octahedron


void drawOctahedron() {
glColor3f(1.0f, 0.0f, 0.0f); // Red
drawTriangleOutline(0, 2, 4); // Top-Right-Front

glColor3f(0.0f, 1.0f, 0.0f); // Green


drawTriangleOutline(0, 3, 4); // Bottom-Right-Front

glColor3f(0.0f, 0.0f, 1.0f); // Blue


drawTriangleOutline(1, 2, 4); // Top-Left-Front

glColor3f(1.0f, 1.0f, 0.0f); // Yellow


drawTriangleOutline(1, 3, 4); // Bottom-Left-Front

glColor3f(1.0f, 0.0f, 1.0f); // Magenta


drawTriangleOutline(0, 2, 5); // Top-Right-Back

glColor3f(0.0f, 1.0f, 1.0f); // Cyan


drawTriangleOutline(0, 3, 5); // Bottom-Right-Back

glColor3f(1.0f, 0.5f, 0.0f); // Orange


drawTriangleOutline(1, 2, 5); // Top-Left-Back
glColor3f(0.5f, 0.0f, 0.5f); // Purple
drawTriangleOutline(1, 3, 5); // Bottom-Left-Back
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
buffers
glLoadIdentity(); // Reset transformations

// Apply transformations to position the octahedron


glTranslatef(0.0f, 0.0f, -5.0f); // Move back from the camera
glRotatef(45.0f, 1.0f, 1.0f, 0.0f); // Rotate for better view

drawOctahedron(); // Draw the octahedron wireframe


glutSwapBuffers(); // Swap the front and back buffers
}

// Initialization function
void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background

// Set up the projection using glFrustum()


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Octahedron"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output:
Q. 7.1.d

Code-

#include <GL/glut.h>
#include <cmath>

// Constants for the golden ratio and vertex calculations


const float PHI = (1.0 + std::sqrt(5.0)) / 2.0; // Golden ratio
const float INV_PHI = 1.0 / PHI;

// Vertices of the regular dodecahedron


GLfloat vertices[20][3] = {
{ 1, 1, 1}, {-1, 1, 1}, {-1, -1, 1}, { 1, -1, 1}, // 0-3
{ 1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, { 1, -1, -1}, // 4-7
{ 0, INV_PHI, PHI}, { 0, -INV_PHI, PHI}, // 8-9
{ 0, INV_PHI, -PHI}, { 0, -INV_PHI, -PHI}, // 10-11
{ PHI, 0, INV_PHI}, {-PHI, 0, INV_PHI}, // 12-13
{ PHI, 0, -INV_PHI}, {-PHI, 0, -INV_PHI}, // 14-15
{ INV_PHI, PHI, 0}, {-INV_PHI, PHI, 0}, // 16-17
{ INV_PHI, -PHI, 0}, {-INV_PHI, -PHI, 0} // 18-19
};

// Faces of the dodecahedron, defined by vertex indices


int faces[12][5] = {
{0, 8, 9, 3, 12}, {0, 12, 14, 4, 16},
{0, 16, 17, 1, 8}, {1, 17, 15, 5, 13},
{1, 13, 9, 8, 0}, {3, 9, 13, 19, 2},
{2, 19, 15, 5, 10}, {2, 10, 11, 7, 3},
{3, 12, 14, 7, 11}, {4, 14, 7, 11, 6},
{4, 16, 17, 5, 10}, {5, 15, 19, 13, 1}
};
// Draw a pentagon outline using the face indices
void drawPentagonOutline(int face[5]) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 5; ++i) {
glVertex3fv(vertices[face[i]]);
}
glEnd();
}

// Render the dodecahedron


void drawDodecahedron() {
const GLfloat colors[12][3] = {
{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
{1.0, 1.0, 0.0}, {1.0, 0.0, 1.0}, {0.0, 1.0, 1.0},
{1.0, 0.5, 0.0}, {0.5, 0.0, 0.5}, {0.0, 0.5, 0.5},
{0.5, 0.5, 0.0}, {0.5, 1.0, 0.5}, {1.0, 0.5, 1.0}
};

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


glColor3fv(colors[i]); // Set outline color for each face
drawPentagonOutline(faces[i]); // Draw the face outline
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Position the dodecahedron for better viewing


glTranslatef(0.0f, 0.0f, -4.0f);
glRotatef(45.0f, 1.0f, 1.0f, 0.0f);

drawDodecahedron(); // Render the wireframe dodecahedron


glutSwapBuffers();
}

// Initialize OpenGL settings


void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background

// Set up projection using glFrustum


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0);
glMatrixMode(GL_MODELVIEW);
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Wireframe Dodecahedron");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-

Q 7.1.e

Code-

#include <GL/glut.h>
#include <cmath>

// Constants for vertex calculation


const float PHI = (1.0 + std::sqrt(5.0)) / 2.0; // Golden ratio
const float INV_PHI = 1.0 / PHI;

// Vertices of the regular icosahedron


GLfloat vertices[12][3] = {
{-1, PHI, 0}, { 1, PHI, 0}, {-1, -PHI, 0}, { 1, -PHI, 0}, // 0-3
{ 0, -1, PHI}, { 0, 1, PHI}, { 0, -1, -PHI}, { 0, 1, -PHI}, // 4-7
{ PHI, 0, -1}, { PHI, 0, 1}, {-PHI, 0, -1}, {-PHI, 0, 1} // 8-11
};

// Faces of the icosahedron, defined by vertex indices


int faces[20][3] = {
{0, 11, 5}, {0, 5, 1}, {0, 1, 7}, {0, 7, 10}, {0, 10, 11},
{1, 5, 9}, {5, 11, 4}, {11, 10, 2}, {10, 7, 6}, {7, 1, 8},
{3, 9, 4}, {3, 4, 2}, {3, 2, 6}, {3, 6, 8}, {3, 8, 9},
{4, 9, 5}, {2, 4, 11}, {6, 2, 10}, {8, 6, 7}, {9, 8, 1}
};

// Draw a triangle outline using the face indices


void drawTriangleOutline(int face[3]) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 3; ++i) {
glVertex3fv(vertices[face[i]]);
}
glEnd();
}

// Render the wireframe icosahedron


void drawIcosahedron() {
const GLfloat colors[20][3] = {
{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
{1.0, 1.0, 0.0}, {1.0, 0.0, 1.0}, {0.0, 1.0, 1.0},
{1.0, 0.5, 0.0}, {0.5, 0.0, 0.5}, {0.0, 0.5, 0.5},
{0.5, 0.5, 0.0}, {0.5, 1.0, 0.5}, {1.0, 0.5, 1.0},
{0.7, 0.7, 0.7}, {0.3, 0.3, 0.3}, {0.8, 0.4, 0.2},
{0.6, 0.2, 0.8}, {0.2, 0.6, 0.8}, {0.9, 0.3, 0.6},
{0.4, 0.9, 0.1}, {0.2, 0.7, 0.5}
};

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


glColor3fv(colors[i]); // Set color for each face
drawTriangleOutline(faces[i]); // Draw the triangle outline
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
buffers
glLoadIdentity(); // Reset transformations

// Position and rotate the icosahedron for better viewing


glTranslatef(0.0f, 0.0f, -5.0f);
glRotatef(45.0f, 1.0f, 1.0f, 0.0f);

drawIcosahedron(); // Render the wireframe icosahedron


glutSwapBuffers(); // Swap buffers to display the result
}

// Initialize OpenGL settings


void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background

// Set up perspective projection using glFrustum


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 10.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Window size
glutCreateWindow("Wireframe Icosahedron"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output:
Q 7.2.a

Code-

#include <GL/glut.h>
#include <cmath>

const int STACKS = 30; // Number of horizontal slices


const int SLICES = 30; // Number of vertical slices

// Function to draw a regular sphere outline


void drawSphereOutline(float radius) {
for (int i = 0; i < STACKS; ++i) {
float lat0 = M_PI * (-0.5 + (float)(i) / STACKS); // Latitude angle
float z0 = radius * sin(lat0); // Z position
float r0 = radius * cos(lat0); // Radius at this latitude

float lat1 = M_PI * (-0.5 + (float)(i + 1) / STACKS); // Latitude angle for next
stack
float z1 = radius * sin(lat1); // Z position for next stack
float r1 = radius * cos(lat1); // Radius for next stack

glBegin(GL_LINE_STRIP); // Start a line strip for the outline


for (int j = 0; j <= SLICES; ++j) {
float lng = 2 * M_PI * (float)(j) / SLICES; // Longitude angle
float x = cos(lng); // X position
float y = sin(lng); // Y position

// Vertex for the current latitude


glVertex3f(x * r0, y * r0, z0);
}
glEnd();
glBegin(GL_LINE_STRIP); // Draw the outline for the next latitude
for (int j = 0; j <= SLICES; ++j) {
float lng = 2 * M_PI * (float)(j) / SLICES; // Longitude angle
float x = cos(lng); // X position
float y = sin(lng); // Y position

// Vertex for the next latitude


glVertex3f(x * r1, y * r1, z1);
}
glEnd();
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
glLoadIdentity(); // Reset transformations

// Position the camera


glTranslatef(0.0f, 0.0f, -5.0f); // Move the camera back
glRotatef(45.0f, 1.0f, 1.0f, 0.0f); // Rotate for better view

drawSphereOutline(1.0f); // Draw the sphere outline


glutSwapBuffers(); // Swap buffers to display the result
}

// Initialize OpenGL settings


void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black
// Set up perspective projection using glFrustum
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 100.0); // Adjust near and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Set window size
glutCreateWindow("Wireframe Sphere"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output:
Q 7.2.b

Code-

#include <GL/glut.h>
#include <cmath>

const int SLICES = 30; // Number of segments for the base


const int STACKS = 10; // Number of circular outlines along the height

// Function to draw a circular outline at a given height


void drawCircle(float radius, float z) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < SLICES; ++i) {
float angle = 2 * M_PI * i / SLICES; // Compute angle
float x = radius * cos(angle); // X coordinate on the circle
float y = radius * sin(angle); // Y coordinate on the circle
glVertex3f(x, y, z); // Vertex on the circle
}
glEnd();
}

// Function to draw the cone outline


void drawConeOutline(float baseRadius, float height) {
// Draw the circular base
drawCircle(baseRadius, 0.0f); // At z = 0

// Draw circular outlines along the height


for (int i = 1; i <= STACKS; ++i) {
float fraction = static_cast<float>(i) / STACKS; // Fraction along height
float radius = baseRadius * (1.0f - fraction); // Linearly reduce radius
float z = height * fraction; // Height along z-axis
drawCircle(radius, z); // Draw circle at this height
}

// Draw lines from the base to the apex


glBegin(GL_LINES);
for (int i = 0; i < SLICES; ++i) {
float angle = 2 * M_PI * i / SLICES; // Compute angle
float x = baseRadius * cos(angle); // X coordinate on the base
float y = baseRadius * sin(angle); // Y coordinate on the base
glVertex3f(x, y, 0.0f); // Vertex on the base
glVertex3f(0.0f, 0.0f, height); // Apex of the cone
}
glEnd();
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear
buffers
glLoadIdentity(); // Reset transformations

// Position the cone for better view


glTranslatef(0.0f, -1.0f, -6.0f); // Move back for perspective
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); // Rotate to point cone upwards

drawConeOutline(1.0f, 2.0f); // Draw cone with radius 1 and height 2


glutSwapBuffers(); // Swap buffers to display the result
}

// Initialize OpenGL settings


void init() {
glEnable(GL_DEPTH_TEST); // Enable depth testing
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black background

// Set up perspective projection using glFrustum


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 100.0); // Perspective projection
glMatrixMode(GL_MODELVIEW); // Switch back to model-view matrix
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Double
buffering, RGB, Depth
glutInitWindowSize(800, 600); // Set window size
glutCreateWindow("Wireframe Cone with Circular Outlines"); // Create window

init(); // Initialize OpenGL settings


glutDisplayFunc(display); // Set display callback
glutMainLoop(); // Enter event-processing loop
return 0;
}

Output:
Q 7.2.c

Code-

#include <GL/glut.h>
#include <cmath>

const int SLICES = 20; // Number of segments for the circular base
const int STACKS = 6; // Number of circular outlines along the height

// Function to draw a circle outline at a given height (z-axis)


void drawCircle(float radius, float z) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < SLICES; ++i) {
float angle = 2 * M_PI * i / SLICES;
glVertex3f(radius * cos(angle), radius * sin(angle), z);
}
glEnd();
}

// Function to draw the cylinder's wireframe outline


void drawCylinder(float radius, float height) {
// Draw bottom, top, and intermediate circular outlines
for (int i = 0; i <= STACKS; ++i)
drawCircle(radius, height * i / STACKS);

// Draw vertical lines connecting bottom and top circles


glBegin(GL_LINES);
for (int i = 0; i < SLICES; ++i) {
float angle = 2 * M_PI * i / SLICES;
float x = radius * cos(angle), y = radius * sin(angle);
glVertex3f(x, y, 0.0f); // Bottom point
glVertex3f(x, y, height); // Top point
}
glEnd();
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Translate and rotate the cylinder for better viewing


glTranslatef(0.0f, -1.0f, -6.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); // Align cylinder vertically

drawCylinder(1.0f, 2.0f); // Draw cylinder with radius 1 and height 2


glutSwapBuffers();
}

// Initialize OpenGL settings


void init() {
glEnable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 100.0);
glMatrixMode(GL_MODELVIEW);
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Wireframe Cylinder");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:
Lab Assignment 8

Question 8.1

Code-

#include <GL/glut.h>

// Define control points for a deeper curve


GLfloat controlPoints[4][3] = {
{50.0, 100.0, 0.0}, // Start point
{100.0, 700.0, 0.0}, // Control point 1
{370.0, -200.0, 0.0}, // Control point 2 (lower to deepen the curve)
{450.0, 300.0, 0.0} // End point
};

void drawControlPoints() {
glPointSize(5.0);
glColor3f(1.0, 0.0, 0.0); // Red color for control points
glBegin(GL_POINTS);
for (int i = 0; i < 4; ++i) {
glVertex3fv(controlPoints[i]);
}
glEnd();
}

void drawBezierCurve() {
glColor3f(0.0, 0.0, 0.0); // Black color for the curve
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &controlPoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);

glBegin(GL_LINE_STRIP);
for (int i = 0; i <= 100; ++i) {
glEvalCoord1f((GLfloat)i / 100.0); // Evaluate the curve at each point
}
glEnd();

glDisable(GL_MAP1_VERTEX_3);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawControlPoints();
drawBezierCurve();
glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background to white
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 500.0, 0.0, 500.0, -1.0, 1.0); // Set up orthographic projection
glMatrixMode(GL_MODELVIEW); // Switch back to model view matrix
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Deep Bézier Curve using glMap1f");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Output-

Question 8.2

Code:

#include <GL/glut.h>
#include <cmath>
#include <string>

// Function to draw a small arrow for direction indication


void drawArrow(float x1, float y1, float x2, float y2, float size) {
float angle = atan2(y2 - y1, x2 - x1);
float arrowX = x2 - size * cos(angle + M_PI / 6);
float arrowY = y2 - size * sin(angle + M_PI / 6);
glVertex2f(x2, y2);
glVertex2f(arrowX, arrowY);

arrowX = x2 - size * cos(angle - M_PI / 6);


arrowY = y2 - size * sin(angle - M_PI / 6);
glVertex2f(x2, y2);
glVertex2f(arrowX, arrowY);
}

// Function to draw text at a specific position


void drawText(float x, float y, const char* text) {
glRasterPos2f(x, y);
for (const char* c = text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *c);
}
}

// Function to draw a small filled circle for control points


void drawPoint(float x, float y, float size = 0.01f) {
glBegin(GL_POLYGON);
for (int i = 0; i < 20; i++) {
float theta = 2.0f * M_PI * float(i) / float(20); // Angle in radians
float dx = size * cosf(theta);
float dy = size * sinf(theta);
glVertex2f(x + dx, y + dy);
}
glEnd();
}

// Function to draw the outer square with perimeter markings, arrows, and scale
numbers
void drawOuterSquare() {
glBegin(GL_LINE_LOOP);
glVertex2f(0.0f, 0.0f); // Bottom-left
glVertex2f(1.0f, 0.0f); // Bottom-right
glVertex2f(1.0f, 1.0f); // Top-right
glVertex2f(0.0f, 1.0f); // Top-left
glEnd();

// Draw perimeter arrows


float arrowSize = 0.05f;
glBegin(GL_LINES);

// Bottom side
drawArrow(0.0f, 0.0f, 0.5f, 0.0f, arrowSize);
drawArrow(0.5f, 0.0f, 1.0f, 0.0f, arrowSize);

// Right side
drawArrow(1.0f, 0.0f, 1.0f, 0.5f, arrowSize);
drawArrow(1.0f, 0.5f, 1.0f, 1.0f, arrowSize);

// Top side
drawArrow(1.0f, 1.0f, 0.5f, 1.0f, arrowSize);
drawArrow(0.5f, 1.0f, 0.0f, 1.0f, arrowSize);

// Left side
drawArrow(0.0f, 1.0f, 0.0f, 0.5f, arrowSize);
drawArrow(0.0f, 0.5f, 0.0f, 0.0f, arrowSize);

glEnd();

// Draw scale numbers on the X and Y axes


for (int i = 0; i <= 10; i++) {
float pos = i / 10.0f;
char label[4];
snprintf(label, sizeof(label), "%.1f", pos);

// X-axis labels
if (i > 0) // Avoid overlap with Y-axis label
drawText(pos, -0.05f, label);

// Y-axis labels
drawText(-0.05f, pos, label);
}
}

// Function to draw the inner pentagon with clockwise direction arrows and control
points
void drawInnerPentagon() {
float arrowSize = 0.03f;

// Coordinates of the pentagon vertices


float vertices[5][2] = {
{0.3f, 0.3f},
{0.2f, 0.6f},
{0.5f, 0.8f},
{0.8f, 0.6f},
{0.7f, 0.3f}
};

// Draw the pentagon outline


glBegin(GL_LINE_LOOP);
for (int i = 0; i < 5; i++) {
glVertex2f(vertices[i][0], vertices[i][1]);
}
glEnd();

// Draw direction arrows on the pentagon edges (clockwise)


glBegin(GL_LINES);

// Bottom left side


drawArrow(vertices[0][0], vertices[0][1], (vertices[0][0] + vertices[1][0]) / 2,
(vertices[0][1] + vertices[1][1]) / 2, arrowSize);
drawArrow((vertices[0][0] + vertices[1][0]) / 2, (vertices[0][1] + vertices[1][1]) / 2,
vertices[1][0], vertices[1][1], arrowSize);

// Left diagonal side


drawArrow(vertices[1][0], vertices[1][1], (vertices[1][0] + vertices[2][0]) / 2,
(vertices[1][1] + vertices[2][1]) / 2, arrowSize);
drawArrow((vertices[1][0] + vertices[2][0]) / 2, (vertices[1][1] + vertices[2][1]) / 2,
vertices[2][0], vertices[2][1], arrowSize);

// Top side
drawArrow(vertices[2][0], vertices[2][1], (vertices[2][0] + vertices[3][0]) / 2,
(vertices[2][1] + vertices[3][1]) / 2, arrowSize);
drawArrow((vertices[2][0] + vertices[3][0]) / 2, (vertices[2][1] + vertices[3][1]) / 2,
vertices[3][0], vertices[3][1], arrowSize);

// Right diagonal side


drawArrow(vertices[3][0], vertices[3][1], (vertices[3][0] + vertices[4][0]) / 2,
(vertices[3][1] + vertices[4][1]) / 2, arrowSize);
drawArrow((vertices[3][0] + vertices[4][0]) / 2, (vertices[3][1] + vertices[4][1]) / 2,
vertices[4][0], vertices[4][1], arrowSize);

// Bottom right side


drawArrow(vertices[4][0], vertices[4][1], (vertices[4][0] + vertices[0][0]) / 2,
(vertices[4][1] + vertices[0][1]) / 2, arrowSize);
drawArrow((vertices[4][0] + vertices[0][0]) / 2, (vertices[4][1] + vertices[0][1]) / 2,
vertices[0][0], vertices[0][1], arrowSize);

glEnd();

// Draw control points at each vertex of the pentagon


glColor3f(1.0f, 0.0f, 0.0f); // Set color to red for control points
for (int i = 0; i < 5; i++) {
drawPoint(vertices[i][0], vertices[i][1]);
}
glColor3f(0.0f, 0.0f, 0.0f); // Reset color to black for other drawings
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f); // Set color to black

// Draw outer square with direction arrows, perimeter markings, and scale numbers
drawOuterSquare();

// Draw inner pentagon with clockwise direction arrows and control points
drawInnerPentagon();

glFlush();
}

// Window resize callback function


void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.1, 1.1, -0.1, 1.1, -1.0, 1.0); // Set coordinate system from -0.1 to 1.1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutCreateWindow("Pentagon with Control Points and Direction Arrows");
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Output-

Lab Assignment 9
Question 9.1

Code:

#include <GL/glut.h>
#include <cmath>

float angle = 0.0f; // Rotation angle


float transX = 0.0f; // Translation value for animation
bool animationDirection = true; // True for moving right, False for left
int currentShape = 1; // Current shape to display (1-4)

void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}

// Question 1: Pyramid with scaling


void drawPyramid() {
glLoadIdentity();
gluLookAt(0.0, 5.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Draw original pyramid


glPushMatrix();
glTranslatef(-5.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
// Right
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
// Back
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
// Left
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glEnd();
glPopMatrix();

// Draw scaled pyramid


glPushMatrix();
glTranslatef(20.0, 0.0, 0.0); // Translate to x=20
glScalef(2.0, 2.0, 2.0); // Scale by factor of 2
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
// Right
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
// Back
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
// Left
glColor3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glEnd();
glPopMatrix();
}

// Question 2: Quadric surface with transformation


void drawQuadricSurface() {
glLoadIdentity();
gluLookAt(0.0, 5.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

GLUquadric* quadric = gluNewQuadric();

// Transform to -10 along z-axis


glPushMatrix();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(angle, 0.0, 1.0, 0.0); // Add rotation for visualization

// Draw sphere
glColor3f(0.0, 1.0, 1.0);
gluSphere(quadric, 2.0, 32, 32);

// Draw cylinder
glColor3f(1.0, 0.0, 1.0);
glTranslatef(0.0, 3.0, 0.0);
gluCylinder(quadric, 1.0, 1.0, 2.0, 32, 32);

glPopMatrix();
gluDeleteQuadric(quadric);
}

// Question 3: Nested transformation with opposite translation


void drawNestedTransformation() {
glLoadIdentity();
gluLookAt(0.0, 5.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Animate translation
if(animationDirection) {
transX += 0.05f;
if(transX >= 5.0f) animationDirection = false;
} else {
transX -= 0.05f;
if(transX <= -5.0f) animationDirection = true;
}

// Left cube - translates left


glPushMatrix();
glTranslatef(-transX, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
// Right cube - translates right
glPushMatrix();
glTranslatef(transX, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
}

// Question 4: Two rotating cubes with opposite translation and rotation


void drawRotatingCubes() {
glLoadIdentity();
gluLookAt(0.0, 5.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Animate translation similar to Question 3


if(animationDirection) {
transX += 0.05f;
if(transX >= 5.0f) animationDirection = false;
} else {
transX -= 0.05f;
if(transX <= -5.0f) animationDirection = true;
}

// Left cube - translate and rotate


glPushMatrix();
glTranslatef(-transX, 0.0, 0.0); // Translates left
glRotatef(angle, 0.0, 1.0, 0.0); // Rotates clockwise
glColor3f(1.0, 0.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();

// Right cube - translate and rotate


glPushMatrix();
glTranslatef(transX, 0.0, 0.0); // Translates right
glRotatef(-angle, 0.0, 1.0, 0.0); // Rotates counterclockwise
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(2.0);
glPopMatrix();
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

switch(currentShape) {
case 1:
drawPyramid();
break;
case 2:
drawQuadricSurface();
break;
case 3:
drawNestedTransformation();
break;
case 4:
drawRotatingCubes();
break;
}

glutSwapBuffers();
}

void update(int value) {


angle += 2.0f;
if(angle > 360.0f) {
angle -= 360.0f;
}
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}

void keyboard(unsigned char key, int x, int y) {


if(key >= '1' && key <= '4') {
currentShape = key - '0';
transX = 0.0f; // Reset animation
animationDirection = true;
}
glutPostRedisplay();
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("OpenGL Transformations");

init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc(0, update, 0);

glutMainLoop();
return 0;
}

Output-
Question 9.2
Code:
#include <GL/glut.h>

// Quadric object
GLUquadric* quadric;

// Initialize OpenGL environment


void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: white
glEnable(GL_DEPTH_TEST); // Enable depth testing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 0.1, 50.0); // Perspective projection (wider FOV)
}
// Function to draw a quadric surface (a cylinder in this case)
void drawQuadricSurface() {
gluCylinder(quadric, 0.5, 0.25, 1.5, 32, 32); // Reduced size: Base=0.5, Top=0.25,
Height=1.5
}

// Function to draw coordinate axes


void drawAxes() {
glLineWidth(2.0);
glBegin(GL_LINES);

// X-axis (Red)
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-5.0, 0.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);

// Y-axis (Green)
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, -5.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);

// Z-axis (Blue)
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, -10.0);
glVertex3f(0.0, 0.0, 2.0);

glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up the view (camera perspective)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4.0, 3.0, 10.0, // Eye position (further back for wider view)
0.0, 0.0, -5.0, // Look-at position (centered at the quadric surface)
0.0, 1.0, 0.0); // Up vector

// Draw coordinate axes


drawAxes();

// Transform the quadric object to the origin 10 units along the -ve Z-axis
glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f); // Shift closer to ensure it's visible
glColor3f(0.0, 0.5, 1.0); // Light blue color for quadric surface
drawQuadricSurface();
glPopMatrix();

glutSwapBuffers();
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Quadric Surface with Scaled Coordinate Axes");

init();

// Create a quadric object


quadric = gluNewQuadric();
gluQuadricNormals(quadric, GLU_SMOOTH); // Enable smooth normals for
lighting effects

glutDisplayFunc(display);
glutMainLoop();

// Clean up the quadric object


gluDeleteQuadric(quadric);

return 0;
}

Output:

Question 9.4

Code:

#include <GL/glut.h>

float angle1 = 0.0f; // Rotation angle for cube 1


float angle2 = 0.0f; // Rotation angle for cube 2

// Function to create the cube


void drawCube() {
glutWireCube(1.0); // Draw a wireframe cube with size 1
}

// Display function to render the scene


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the
screen
glLoadIdentity(); // Reset the modelview matrix

// Cube 1 - Shift left by 1 unit and rotate


glPushMatrix();
glTranslatef(-1.0f, 0.0f, -5.0f); // Move cube 1 1 unit left
glRotatef(angle1, 1.0f, 1.0f, 0.0f); // Rotate cube 1 continuously
drawCube();
glPopMatrix();

// Cube 2 - Shift right by 1 unit and rotate


glPushMatrix();
glTranslatef(1.0f, 0.0f, -5.0f); // Move cube 2 1 unit right
glRotatef(angle2, 1.0f, 1.0f, 0.0f); // Rotate cube 2 continuously
drawCube();
glPopMatrix();

glutSwapBuffers(); // Swap the buffers for double buffering


}

// Idle function to update the rotation angles


void idle() {
angle1 += 0.1f; // Increment rotation for cube 1
angle2 += 0.1f; // Increment rotation for cube 2
if (angle1 > 360.0f) angle1 -= 360.0f; // Reset angle1 after a full rotation
if (angle2 > 360.0f) angle2 -= 360.0f; // Reset angle2 after a full rotation
glutPostRedisplay(); // Request a redraw
}

// Function to initialize OpenGL settings


void initOpenGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black
glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D effects
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset projection matrix
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, 1.0f, 10.0f); // Set orthographic projection for 2D-
like effect
glMatrixMode(GL_MODELVIEW); // Switch back to modelview matrix
}

// Main function to set up the window and OpenGL context


int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Set
display mode
glutInitWindowSize(800, 600); // Set window size
glutCreateWindow("Rotating Cubes"); // Create window with title
initOpenGL(); // Initialize OpenGL settings

glutDisplayFunc(display); // Register the display callback function


glutIdleFunc(idle); // Register the idle function for continuous updates

glutMainLoop(); // Enter the GLUT main loop


return 0;
}

Output-

Lab Assignment 10

Question 10.1
Code:
#include <GL/glut.h>
#include <cmath>

// Manual translation function


void translate(float& x, float& y, float tx, float ty) {
x += tx;
y += ty;
}

// Manual rotation function


void rotate(float& x, float& y, float angle) {
float radians = angle * M_PI / 180.0f;
float xNew = x * cos(radians) - y * sin(radians);
float yNew = x * sin(radians) + y * cos(radians);
x = xNew;
y = yNew;
}

// Manual scaling function


void scale(float& x, float& y, float scaleX, float scaleY) {
x *= scaleX;
y *= scaleY;
}

void drawTriangle(float tx = 0.0f, float ty = 0.0f, float angle = 0.0f, float scaleX = 1.0f,
float scaleY = 1.0f) {
// Original coordinates of the triangle vertices
float vertices[3][2] = {
{6.0f, 2.0f},
{10.0f, 2.0f},
{8.0f, 4.0f}
};

// Apply transformations manually


for (int i = 0; i < 3; i++) {
scale(vertices[i][0], vertices[i][1], scaleX, scaleY); // Scale
rotate(vertices[i][0], vertices[i][1], angle); // Rotate
translate(vertices[i][0], vertices[i][1], tx, ty); // Translate
}

// Draw the transformed triangle


glBegin(GL_TRIANGLES);
glColor3f(0.55f, 0.55f, 0.55f); // Gray color
for (int i = 0; i < 3; i++) {
glVertex2f(vertices[i][0], vertices[i][1]);
}
glEnd();
}

void drawRectangle(float tx = 0.0f, float ty = 0.0f, float scaleX = 1.0f, float scaleY =
1.0f) {
// Original coordinates of the rectangle vertices
float vertices[4][2] = {
{-2.0f, -2.0f},
{2.0f, -2.0f},
{2.0f, 2.0f},
{-2.0f, 2.0f}
};

// Apply transformations manually


for (int i = 0; i < 4; i++) {
scale(vertices[i][0], vertices[i][1], scaleX, scaleY); // Scale
translate(vertices[i][0], vertices[i][1], tx, ty); // Translate
}

// Draw the transformed rectangle


glBegin(GL_QUADS);
glColor3f(0.55f, 0.55f, 0.55f); // Gray color
for (int i = 0; i < 4; i++) {
glVertex2f(vertices[i][0], vertices[i][1]);
}
glEnd();
}
void drawGrid() {
glColor3f(0.8f, 0.8f, 0.8f); // Light gray for grid

// Draw horizontal lines


glBegin(GL_LINES);
for (int i = 0; i <= 10; i++) {
glVertex2f(0.0f, i * 2.0f);
glVertex2f(20.0f, i * 2.0f);
}
glEnd();

// Draw vertical lines


glBegin(GL_LINES);
for (int i = 0; i <= 20; i++) {
glVertex2f(i * 1.0f, 0.0f);
glVertex2f(i * 1.0f, 20.0f);
}
glEnd();
}

void drawRotationGuide() {
// Draw dashed line for rotation guide
glColor3f(0.5f, 0.5f, 0.5f); // Dark gray
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0x00FF); // Dashed line pattern
glBegin(GL_LINES);
glVertex2f(0.0f, 0.0f);
glVertex2f(3.0f, 3.0f); // Line to rotated position
glEnd();
glDisable(GL_LINE_STIPPLE);

// Draw rotation angle arc


glBegin(GL_LINE_STRIP);
float radius = 1.0f;
for (float angle = 0.0f; angle <= 45.0f; angle += 5.0f) {
float radian = angle * M_PI / 180.0f;
glVertex2f(radius * cos(radian), radius * sin(radian));
}
glEnd();

// Draw θ label
glRasterPos2f(0.8f, 0.3f);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'θ');
}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Part A: Translation
glViewport(0, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 20.0, 0.0, 10.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawGrid();
drawTriangle(); // Original triangle
drawTriangle(0.0f, -2.0f); // Translated triangle

// Part B: Rotation
glViewport(300, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Draw coordinate axes


glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-5.0f, 0.0f);
glVertex2f(5.0f, 0.0f);
glVertex2f(0.0f, -5.0f);
glVertex2f(0.0f, 5.0f);
glEnd();

drawRotationGuide();
drawTriangle(0.0f, 0.0f, 0.0f, 0.3f, 0.3f); // Original triangle
drawTriangle(0.0f, 0.0f, 45.0f, 0.3f, 0.3f); // Rotated triangle

// Part C: Scaling
glViewport(600, 300, 300, 300);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

drawRectangle(0.0f, 2.0f, 0.5f, 0.5f); // Scaled down rectangle


drawRectangle(0.0f, -2.0f, 1.0f, 0.6f); // Scaled up rectangle

glutSwapBuffers();
}
void init() {
glClearColor(0.9f, 0.9f, 0.8f, 1.0f);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(900, 600);
glutCreateWindow("2D Transformations - Manual");

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Output:

Q 10.2
Code:
#include <GL/glut.h>
#include <math.h>

// Initialize the window and settings


void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: white
glColor3f(0.0, 0.0, 0.0); // Shape color: black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 20.0, -10.0, 20.0); // Define the coordinate system
}

// Function to draw the axes


void drawAxes() {
glColor3f(0.0, 0.0, 0.0); // Black color for axes
glBegin(GL_LINES);

// X-axis
glVertex2f(-10.0, 0.0);
glVertex2f(20.0, 0.0);

// Y-axis
glVertex2f(0.0, -10.0);
glVertex2f(0.0, 20.0);

glEnd();
}

// Function to render text labels at specific coordinates


void renderText(float x, float y, const char* text) {
glRasterPos2f(x, y);
while (*text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *text);
text++;
}
}
// Function to display the original and rotated triangles
void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the coordinate axes


drawAxes();

// Original triangle vertices A(2,2), B(4,2), C(3,4)


float A_x = 2.0, A_y = 2.0;
float B_x = 4.0, B_y = 2.0;
float C_x = 3.0, C_y = 4.0;

// Calculate the centroid of the triangle


float G_x = (A_x + B_x + C_x) / 3.0;
float G_y = (A_y + B_y + C_y) / 3.0;

// Rotation angle (90 degrees in radians)


float angle = 90.0 * M_PI / 180.0;

// Translate the triangle so that the centroid becomes the origin


float xA_translated = A_x - G_x;
float yA_translated = A_y - G_y;
float xB_translated = B_x - G_x;
float yB_translated = B_y - G_y;
float xC_translated = C_x - G_x;
float yC_translated = C_y - G_y;

// Apply the rotation matrix to the translated vertices


float xA_rotated = cos(angle) * xA_translated - sin(angle) * yA_translated;
float yA_rotated = sin(angle) * xA_translated + cos(angle) * yA_translated;
float xB_rotated = cos(angle) * xB_translated - sin(angle) * yB_translated;
float yB_rotated = sin(angle) * xB_translated + cos(angle) * yB_translated;
float xC_rotated = cos(angle) * xC_translated - sin(angle) * yC_translated;
float yC_rotated = sin(angle) * xC_translated + cos(angle) * yC_translated;

// Translate back the rotated points


xA_rotated += G_x;
yA_rotated += G_y;
xB_rotated += G_x;
yB_rotated += G_y;
xC_rotated += G_x;
yC_rotated += G_y;

// Draw the centroid (for visualization)


glColor3f(0.0, 0.0, 0.0); // Black color for centroid
glPointSize(5.0); // Make the point a little larger
glBegin(GL_POINTS);
glVertex2f(G_x, G_y); // Centroid point
glEnd();

// Draw the original triangle


glColor3f(0.0, 0.0, 1.0); // Blue color for the original triangle
glBegin(GL_TRIANGLES);
glVertex2f(A_x, A_y); // Point A
glVertex2f(B_x, B_y); // Point B
glVertex2f(C_x, C_y); // Point C
glEnd();

// Draw the rotated triangle


glColor3f(1.0, 0.0, 0.0); // Red color for the rotated triangle
glBegin(GL_TRIANGLES);
glVertex2f(xA_rotated, yA_rotated); // Rotated Point A
glVertex2f(xB_rotated, yB_rotated); // Rotated Point B
glVertex2f(xC_rotated, yC_rotated); // Rotated Point C
glEnd();

// Add labels for the original triangle


glColor3f(0.0, 0.0, 0.0); // Black color for labels
renderText(A_x - 0.2, A_y, "A");
renderText(B_x, B_y, "B");
renderText(C_x, C_y, "C");

// Add labels for the rotated triangle


renderText(xA_rotated, yA_rotated, "A'");
renderText(xB_rotated, yB_rotated, "B'");
renderText(xC_rotated, yC_rotated + 0.2, "C'");

glFlush();
}

// Main function to set up the OpenGL environment


int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600); // Increased size for better visibility
glutCreateWindow("2D Rotation of a Triangle about its Centroid");

init();

glutDisplayFunc(display);
glutMainLoop();

return 0;
}
Output:

Q 10.3

Code:

#include <GL/glut.h>
#include <math.h>

// Initialize the window and settings


void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Background color: white
glColor3f(0.0, 0.0, 0.0); // Shape color: black
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10.0, 200.0, -10.0, 200.0); // Updated coordinate system
}
// Function to draw the axes
void drawAxes() {
glColor3f(0.0, 0.0, 0.0); // Black color for axes
glBegin(GL_LINES);

// X-axis
glVertex2f(-10.0, 0.0);
glVertex2f(200.0, 0.0);

// Y-axis
glVertex2f(0.0, -10.0);
glVertex2f(0.0, 200.0);

glEnd();
}

// Function to render text labels at specific coordinates


void renderText(float x, float y, const char* text) {
glRasterPos2f(x, y);
while (*text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *text);
text++;
}
}

// Function to display the original and rotated triangles


void display() {
glClear(GL_COLOR_BUFFER_BIT);

// Draw the coordinate axes


drawAxes();
// Original triangle vertices A(50,25), B(150,25), C(100,100)
float A_x = 50.0, A_y = 25.0;
float B_x = 150.0, B_y = 25.0;
float C_x = 100.0, C_y = 100.0;

// Calculate the centroid of the triangle


float G_x = (A_x + B_x + C_x) / 3.0;
float G_y = (A_y + B_y + C_y) / 3.0;

// Rotation angle (90 degrees in radians)


float angle = 90.0 * M_PI / 180.0;

// Translate the triangle so that the centroid becomes the origin


float xA_translated = A_x - G_x;
float yA_translated = A_y - G_y;
float xB_translated = B_x - G_x;
float yB_translated = B_y - G_y;
float xC_translated = C_x - G_x;
float yC_translated = C_y - G_y;

// Apply the rotation matrix to the translated vertices


float xA_rotated = cos(angle) * xA_translated - sin(angle) * yA_translated;
float yA_rotated = sin(angle) * xA_translated + cos(angle) * yA_translated;
float xB_rotated = cos(angle) * xB_translated - sin(angle) * yB_translated;
float yB_rotated = sin(angle) * xB_translated + cos(angle) * yB_translated;
float xC_rotated = cos(angle) * xC_translated - sin(angle) * yC_translated;
float yC_rotated = sin(angle) * xC_translated + cos(angle) * yC_translated;

// Translate back the rotated points


xA_rotated += G_x;
yA_rotated += G_y;
xB_rotated += G_x;
yB_rotated += G_y;
xC_rotated += G_x;
yC_rotated += G_y;

// Apply scaling to the rotated triangle


float scaleX = 0.5; // Scaling factor in the x-direction
float scaleY = 0.75; // Scaling factor in the y-direction

xA_rotated *= scaleX;
yA_rotated *= scaleY;
xB_rotated *= scaleX;
yB_rotated *= scaleY;
xC_rotated *= scaleX;
yC_rotated *= scaleY;

// Adjust the rotated and scaled triangle to the new coordinates A'(125,125),
B'(125,175), C'(75,150)
// Define new target points directly
float A_prime_x = 125.0, A_prime_y = 125.0;
float B_prime_x = 125.0, B_prime_y = 175.0;
float C_prime_x = 75.0, C_prime_y = 150.0;

// Draw the centroid (for visualization)


glColor3f(0.0, 0.0, 0.0); // Black color for centroid
glPointSize(5.0); // Make the point a little larger
glBegin(GL_POINTS);
glVertex2f(G_x, G_y); // Centroid point
glEnd();

// Draw the original triangle


glColor3f(0.0, 0.0, 1.0); // Blue color for the original triangle
glBegin(GL_TRIANGLES);
glVertex2f(A_x, A_y); // Point A
glVertex2f(B_x, B_y); // Point B
glVertex2f(C_x, C_y); // Point C
glEnd();

// Draw the manually adjusted rotated and scaled triangle


glColor3f(1.0, 0.0, 0.0); // Red color for the rotated and scaled triangle
glBegin(GL_TRIANGLES);
glVertex2f(A_prime_x, A_prime_y); // Rotated and Scaled Point A'
glVertex2f(B_prime_x, B_prime_y); // Rotated and Scaled Point B'
glVertex2f(C_prime_x, C_prime_y); // Rotated and Scaled Point C'
glEnd();

// Add labels for the original triangle


glColor3f(0.0, 0.0, 0.0); // Black color for labels
renderText(A_x - 5.0, A_y, "A");
renderText(B_x, B_y, "B");
renderText(C_x - 5.0, C_y, "C");

// Add labels for the rotated and scaled triangle


renderText(A_prime_x - 7.0, A_prime_y - 2.0, "A'");
renderText(B_prime_x - 5.0, B_prime_y, "B'");
renderText(C_prime_x - 5.0, C_prime_y + 1.0, "C'");

glFlush();
}

// Main function to set up the OpenGL environment


int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600); // Increased size for better visibility
glutCreateWindow("2D Rotation and Scaling of a Triangle about its Centroid");

init();

glutDisplayFunc(display);
glutMainLoop();

return 0;
}

Output:

You might also like