Graphics assignment
Graphics assignment
Graphics assignment
),
Vidisha (M. P.), India
Practical- 1
Name : Samyak Jain Enrolment No : 0108CS221113
Subject/ Code: Practical Lab-1 (CS-506)
Branch: 5th Sem. BTech (CSE-B) Session: 2024-2025 Date of Execution: 24/09/24
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float x = x1;
float y = y1;
1
}
glFlush(); // Render the drawing on the window
}
// Draw a black line using DDA algorithm from (50, 100) to (400, 300)
glColor3f(0.0, 0.0, 0.0); // Set the line color to black
drawLine(50, 100, 400, 300);
}
2
// Call initialization routines
init();
return 0;
}
#include <GL/glut.h>
#include <stdio.h>
3
// Function to plot all the symmetrical points in a circle
void plotCirclePoints(int xc, int yc, int x, int y) {
setPixel(xc + x, yc + y);
setPixel(xc - x, yc + y);
setPixel(xc + x, yc - y);
setPixel(xc - x, yc - y);
setPixel(xc + y, yc + x);
setPixel(xc - y, yc + x);
setPixel(xc + y, yc - x);
setPixel(xc - y, yc - x);
}
while (y >= x) {
x++;
// Draw a circle using Bresenham's algorithm with center (250, 250) and radius
100
4
glColor3f(0.0, 0.0, 0.0); // Set the color to black
drawCircle(250, 250, 100);
}
return 0;
5
}
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
6
// Calculate the increment for each step
float xIncrement = dx / steps;
float yIncrement = dy / steps;
float x = x1;
float y = y1;
7
rotate(45); // Rotate the line by 45 degrees
scale(1.5, 1.5); // Scale the line by 1.5 in both x and y directions
drawLine(line_x1, line_y1, line_x2, line_y2); // Draw the transformed line
glPopMatrix(); // Restore the original matrix
8
// Enter the GLUT event processing loop
glutMainLoop();
return 0;
}
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
9
glVertex2f(x, y + 40); // Top left
glEnd();
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i < 360; i++) {
float angle = i * 3.14159 / 180;
float wheelX = x + 70 + 20 * cos(angle);
float wheelY = y - 10 + 20 * sin(angle);
glVertex2f(wheelX, wheelY);
}
glEnd();
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load identity matrix
glFlush(); // Render
}
10
// Update function for animation
void update(int value) {
// Update the position of the car
carX += carSpeed;
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width
return 0;
}
11
11. Write a program to draw balloons using in build graphics
function and translate it from bottom
left corner to right top corner of screen.
#include <GL/glut.h>
#include <math.h>
12
}
glEnd();
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load identity matrix
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
13
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width
return 0;
}
14
12. Write a program to draw a cube using in build library function
and perform 3D transformations
i) Translations in x, y, z directions
ii) Rotation by angle 450 about z axis, rotation by 600 about y-axis
in succession.
iii) Scaling in x-direction by a factor of 2, scaling in y- direction by a
factor of 3.
#include <GL/glut.h>
// Rotation angles
float rotAngleZ = 0.0f;
float rotAngleY = 0.0f;
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen
and depth buffer
glLoadIdentity(); // Reset the matrix
15
glutSwapBuffers(); // Swap buffers for double buffering
}
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background
glEnable(GL_DEPTH_TEST); // Enable depth testing
// Set up perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0); // Perspective with a viewing frustum
glMatrixMode(GL_MODELVIEW); // Back to model view matrix
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600); // Set the window size
glutCreateWindow("Samyak Jain"); // Create the window
init(); // Initialize
glutDisplayFunc(display); // Display callback
glutIdleFunc(idle); // Idle callback to animate
glutMainLoop(); // Enter the main loop
return 0;
}
16
13. Write a
program for making Bezier curve.
#include <GL/glut.h>
#include <math.h>
17
for (float t = 0; t <= 1.0; t += 0.01) {
float x, y;
bezierCurve(t, &x, &y);
glVertex2f(x, y); // Plot the calculated point on the curve
}
glEnd();
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
18
glutInitWindowSize(500, 500); // Set the window size
glutCreateWindow("Samyak Jain"); // Create window with title
return 0;
}
def draw_shapes():
# Clear existing shapes
cmds.delete(cmds.ls('myShapes*'))
# Draw a circle
circle = cmds.circle(name='myCircle', radius=1, ch=False)
cmds.setAttr(circle[0] + ".overrideEnabled", 1)
cmds.setAttr(circle[0] + ".overrideColor", 17) # Blue color
19
square = cmds.polyCube(name='mySquare', width=2, height=0.1, depth=2,
ch=False)
cmds.move(3, 0, 0) # Move square to the right
cmds.setAttr(square[0] + ".overrideEnabled", 1)
cmds.setAttr(square[0] + ".overrideColor", 6) # Yellow color
20
15. Write a program to show animation of a ball moving in a helical
path.
#include <GL/glut.h>
#include <cmath>
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the
screen and depth buffer
glLoadIdentity(); // Load the identity matrix
21
if (angle > 2 * M_PI) {
angle -= 2 * M_PI; // Reset angle after a full circle
}
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white with full opacity
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Enable
double buffering and depth buffer
glutInitWindowSize(800, 600); // Set the window size
glutCreateWindow("Samyak Jain"); // Create the window
return 0;
}
22
16. Write a program to show animation of solar system.
#include <GL/glut.h>
#include <math.h>
23
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load the identity matrix
glFlush(); // Render
}
24
glutPostRedisplay(); // Request a new display
glutTimerFunc(16, update, 0); // Call update function every 16 ms (approximately
60 FPS)
}
// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5, 1.5, -1.5, 1.5, -1.0, 1.0); // Define the orthographic projection
manually
glMatrixMode(GL_MODELVIEW);
}
return 0;
}
25
26