Professional Documents
Culture Documents
Priyal Keharia - CG Exp 4
Priyal Keharia - CG Exp 4
Experiment No. 04
AIM:
a) Cohen-Sutherland Algorithm
Implementation details:
// TBRL
int left = 1, right = 2, top = 8, bottom = 4;
int xmin = 100, xmax = 400, ymin = 100, ymax = 400;
int line_co_ordinate[4], i;
float x_start = 200, y_start = 250, x_end = 450, y_end = 450;
int c1, c2;
int flag = 1, clip_flag = 0;
void myInit() {
glClearColor(1.0, 1.0, 1.0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
gluOrtho2D(0, 500, 0, 500);
glFlush();
}
void clip() {
int c;
float x, y;
float m = (y_end - y_start) / (x_end - x_start);
if (c1)
c = c1;
else
c = c2;
if (c & left) {
x = xmin;
y = y_start + m * (xmin - x_start);
}
if (c & right) {
x = xmax;
y = m * (xmax - x_start) + y_start;
}
if (c == c1) {
x_start = x;
y_start = y;
} else {
x_end = x;
y_end = y;
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(4);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
if (flag == 1) {
glBegin(GL_LINES);
glVertex2f(x_start, y_start);
glVertex2f(x_end, y_end);
glEnd();
}
if ((c1 | c2) == 0)
break;
else if ((c1 & c2) != 0) {
flag = 0;
break;
} else {
clip();
}
}
glFlush();
#include <GL/glut.h>
#include <stdio.h>
float xmin = 50, xmax = 300, ymin = 50, ymax = 300;
float x_coord1, y_coord1, x_coord2, y_coord2;
void drawRectangle() {
glColor3f(1.0, 0.0, 0.0); // Red color
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
}
void drawLine(float x1, float y1, float x2, float y2, float r, float g, float b) {
glColor3f(r, g, b);
glBegin(GL_LINES);
glVertex2f(x1, y1);
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
A) Cohen-Sutherland-algorithm
Before Clipping
Date:
Signature of faculty in-charge
Post Lab
What is Turtle in CG, Demonstrate use of Turtle by implementing it?
Ans:
In computer graphics (CG), "Turtle Graphics" is a popular method of drawing pictures
using a virtual "turtle" that moves around the screen. The turtle can be instructed to
move forward, turn left or right, and draw lines as it moves.
It's a great way to introduce programming and geometry concepts to beginners.
Python provides a built-in module called turtle that allows you to create drawings using
Turtle Graphics.
The turtle graphics system provides a way to create drawings using a series of
commands. Common turtle commands include:
a) forward(distance): Move the turtle forward by a specified distance, leaving a
line behind.
b) backward(distance): Move the turtle backward by a specified distance, leaving
a line behind.
c) left(angle): Turn the turtle left by a specified angle.
d) right(angle): Turn the turtle right by a specified angle.
e) penup(): Lift the pen (stop drawing).
f) pendown(): Lower the pen (start drawing).
g) pencolor(color): Set the pen color to a specified color.
h) pensize(size): Set the pen size to a specified size.
Here's a simple example to demonstrate the use of the Turtle module by implementing
a basic drawing:
CODE:
import turtle
# Create a turtle object
t = turtle.Turtle()
turtle.bgcolor("lightblue") # Set the background color
# Set the turtle's properties
t.penup()
t.goto(0, -100) # Move to the center of the screen
t.pendown()
t.pensize(2)
t.speed(1)
# Draw the head (a yellow circle)
t.color("yellow")
t.begin_fill()
t.circle(100)
t.end_fill()
# Hide the turtle
t.hideturtle()
# Close the turtle graphics window on click
turtle.exitonclick()
OUTPUT