0% found this document useful (0 votes)
16 views15 pages

CG Lab2

Uploaded by

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

CG Lab2

Uploaded by

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

NAME: Aditya Baware

ROLL NO: 2022-IMT-007


COMPUTER GRAPHICS LAB ASSIGNMENT - 2

Q1.(A)

CODE:

#include <GL/glut.h>
#include <cmath> // for sin and cos

// Function prototypes for window display


void displayWindow();
void drawCircle(float radius, int numPoints);
// Main function
int main(int argc, char** argv) {
// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // Use double buffering and
RGB color mode

// Create the window with 512x512 resolution


glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100); // Position the window at 100,100 on the screen
glutCreateWindow("Circle Display - 512x512");
glutDisplayFunc(displayWindow); // Set the display callback for this window

// Enter the GLUT main loop


glutMainLoop();

return 0;
}

// Function to draw a circle using points


void drawCircle(float centerX, float centerY, float radius, int numPoints) {
glColor3f(0.0f, 0.0f, 0.0f); // Set the point color to black
glPointSize(2.0f); // Set the size of the points

glBegin(GL_POINTS); // Begin drawing points

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


float theta = 2.0f * 3.1415926f * float(i) / float(numPoints); // Calculate angle
theta
float x = centerX + radius * cos(theta); // Calculate x coordinate
float y = centerY + radius * sin(theta); // Calculate y coordinate
glVertex2f(x / 256.0f, y / 256.0f); // Normalize the coordinates for display in the
window
}

glEnd(); // End drawing points


}

// Display callback function for the window


void displayWindow() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set the background color to white
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer

// Draw a circle with radius 1 and center at (0, 0)


drawCircle(0.0f, 0.0f,50.0f, 100); // 100 points used to approximate the circle

glutSwapBuffers(); // Swap buffers for double buffering


}

Q1(B)

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

// Function to draw a circle given the radius and center coordinates


void drawCircle(float radius, float centerX, float centerY) {
int numSegments = 100; // Number of segments used to draw the circle

glBegin(GL_LINE_LOOP);
for (int i = 0; i < numSegments; ++i) {
float angle = 2.0f * 3.1415926f * float(i) / float(numSegments); // Compute angle
float x = radius * cosf(angle) + centerX; // x coordinate
float y = radius * sinf(angle) + centerY; // y coordinate
glVertex2f(x, y);
}
glEnd();
}

void display() {
// Clear the window with the current clear color
glClear(GL_COLOR_BUFFER_BIT);

// Set the color of the circles (RGB)


glColor3f(1.0, 1.0, 1.0); // White

// Draw the first circle with radius 1 and center at (0, 0)


drawCircle(1.0f, 0.0f, 0.0f);

// Draw the second circle with radius 50 and center at (20, 20)
drawCircle(50.0f, 20.0f, 20.0f);

// Flush the drawing to the window


glFlush();
}

void init() {
// Set the clear color to black
glClearColor(0.0, 0.0, 0.0, 0.0);

// Set up the orthographic projection


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(-60, 80, -60, 80); // Adjust to fit both circles in the window
}
int main(int argc, char** argv) {
// Initialize the GLUT library
glutInit(&argc, argv);

// Set the display mode (Single buffer and RGB)


glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

// Set the window size


glutInitWindowSize(512, 512);

// Set the window position on the screen


glutInitWindowPosition(100, 100);

// Create the window with a title


glutCreateWindow("Two Circles: r = 1 (center = (0,0)) & r = 50 (center = (20,20))");

// Call the initialization function


init();

// Register the display callback function


glutDisplayFunc(display);

// Start the main loop


glutMainLoop();

return 0;
}
Q2. DDA A

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

void displayWindow();
void ddaLine(float x1, float y1, float x2, float y2);

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Algorithm - 512x512");
glutDisplayFunc(displayWindow);

glutMainLoop();

return 0;
}

void ddaLine(float x1, float y1, float x2, float y2) {


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

float steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;

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


glPointSize(2.0f);
glBegin(GL_POINTS);

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


glVertex2f(x / 256.0f, y / 256.0f);
x += xIncrement;
y += yIncrement;
}

glEnd();
}
void displayWindow() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

ddaLine(-50.0f, -50.0f, 0.0f, 200.0f);


ddaLine(0.0f, 200.0f, 50.0f, -50.0f);
ddaLine(-25.0f, 75.0f, 25.0f, 75.0f);
glutSwapBuffers();
}

Q2 DDA STAR

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

void displayWindow();
void ddaLine(float x1, float y1, float x2, float y2);

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Algorithm - 512x512");
glutDisplayFunc(displayWindow);

glutMainLoop();

return 0;
}

void ddaLine(float x1, float y1, float x2, float y2) {


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

float steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;

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


glPointSize(2.0f);
glBegin(GL_POINTS);

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


glVertex2f(x / 256.0f, y / 256.0f);
x += xIncrement;
y += yIncrement;
}

glEnd();
}

void displayWindow() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

ddaLine(-100.0f, 0.0f, 100.0f, 0.0f);


ddaLine(0.0f, 173.0f, -100.0f, 0.0f);
ddaLine(0.0f, 173.0f, 100.0f, 0.0f);
ddaLine(100.0f, 100.0f, -100.0f, 100.0f);
ddaLine(0.0f, -73.0f, 100.0f, 100.0f);
ddaLine(0.0f, -73.0f, -100.0f, 100.0f);
glutSwapBuffers();
}

Q3. BRESHMAN A
CODE:
#include <GL/glut.h>
#include <cmath>

void displayWindow();
void ddaLine(float x1, float y1, float x2, float y2);

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Algorithm - 512x512");
glutDisplayFunc(displayWindow);

glutMainLoop();

return 0;
}

void ddaLine(float x1, float y1, float x2, float y2) {


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

float steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;

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


glPointSize(2.0f);
glBegin(GL_POINTS);

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


glVertex2f(x / 256.0f, y / 256.0f);
x += xIncrement;
y += yIncrement;
}

glEnd();
}

void displayWindow() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ddaLine(-100.0f, 0.0f, 100.0f, 0.0f);
ddaLine(0.0f, 173.0f, -100.0f, 0.0f);
ddaLine(0.0f, 173.0f, 100.0f, 0.0f);
ddaLine(100.0f, 100.0f, -100.0f, 100.0f);
ddaLine(0.0f, -73.0f, 100.0f, 100.0f);
ddaLine(0.0f, -73.0f, -100.0f, 100.0f);
glutSwapBuffers();
}

Q3. BRESHMAN STAR:

CODE:
#include <GL/glut.h>
#include <cmath>
void displayWindow();
void ddaLine(float x1, float y1, float x2, float y2);

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("DDA Line Algorithm - 512x512");
glutDisplayFunc(displayWindow);

glutMainLoop();

return 0;
}

void ddaLine(float x1, float y1, float x2, float y2) {


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

float steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

float xIncrement = dx / steps;


float yIncrement = dy / steps;

float x = x1;
float y = y1;

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


glPointSize(2.0f);
glBegin(GL_POINTS);

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


glVertex2f(x / 256.0f, y / 256.0f);
x += xIncrement;
y += yIncrement;
}

glEnd();
}

void displayWindow() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

ddaLine(-100.0f, 0.0f, 100.0f, 0.0f);


ddaLine(0.0f, 173.0f, -100.0f, 0.0f);
ddaLine(0.0f, 173.0f, 100.0f, 0.0f);
ddaLine(100.0f, 100.0f, -100.0f, 100.0f);
ddaLine(0.0f, -73.0f, 100.0f, 100.0f);
ddaLine(0.0f, -73.0f, -100.0f, 100.0f);
glutSwapBuffers();
}

You might also like