You are on page 1of 30

Practical no.

1
1a. Python program to plot a point.

from graphics import *


def main():
win = GraphWin('My Graphics', 250,
250) # specifies graphics window size
250x250
g = Point(125, 125) # creates a Point
object at x=125 y=125
g.draw(win) # draws to the graphics
window
win.getMouse() # keep window up
win.close()
main()

1b. Python program to draw a circle.


from graphics import *
def main():
win = GraphWin('My Graphics', 250,
250) # specifies graphics window size
250x250
g = Circle(Point(125, 125), 50) # creates
a Point object at x=125 y=125
g.draw(win) # draws to the graphics
window
win.getMouse() # keep window up
win.close()
main()

1c. Python program to plot a graph

import numpy as np
import matplotlib.pyplot as plt
# creating the dataset
data = {'C':20, 'C++':15, 'Java':30,
'Python':35}
courses = list(data.keys())
values = list(data.values())
fig = plt.figure(figsize = (10, 5))
# creating the bar plot
plt.bar(courses, values, color ='maroon',
width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different
courses")
plt.show()

Practical 2
Aim: using python design and color simple
hut/house
import turtle
t = turtle.Turtle()
# for background
screen = turtle.Screen()
screen.bgcolor("yellow")
#color and speed of turtle creating the
house
t.color("black")
t.shape("turtle")
t.speed(1)
# for creating base of the house
t.fillcolor('cyan')
t.begin_fill()
t.right(90)
t.forward(250)
t.left(90)
t.forward(400)
t.left(90)
t.forward(250)
t.left(90)
t.forward(400)
t.right(90)
t.end_fill()
# for top of the house
t.fillcolor('brown')
t.begin_fill()
t.right(45)
t.forward(200)
t.right(90)
t.forward(200)
t.left(180)
t.forward(200)
t.right(135)
t.forward(259)
t.right(90)
t.forward(142)
t.end_fill()
# for door and windows
t.right(90)
t.forward(400)
t.left(90)
t.forward(50)
t.left(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(180)
t.forward(200)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
t.forward(150)
t.right(90)
t.forward(100)
t.right(90)
t.forward(75)
t.right(90)
t.forward(200)
t.right(180)
t.forward(200)
t.right(90)
t.forward(75)
t.left(90)
t.forward(15)
t.left(90)
t.forward(200)
t.right(90)
t.forward(15)
Practical No. 3a
Python program to implement DDA
algorithm for line generation
from matplotlib import pyplot as plt
# DDA Function for line generation
def DDA(x0, y0, x1, y1):
# find absolute differences
dx = abs(x0 - x1)
dy = abs(y0 - y1)
# find maximum difference
steps = max(dx, dy)
# calculate the increment in x and y
xinc = dx/steps
yinc = dy/steps
# start with 1st point
x = float(x0)
y = float(y0)
# make a list for coordinates
x_coorinates = []
y_coorinates = []
for i in range(steps):
# append the x,y coordinates
in respective list
x_coorinates.append(x)
y_coorinates.append(y)
# increment the values
x = x + xinc
y = y + yinc
# plot the line with coordinates list
plt.plot(x_coorinates, y_coorinates,
marker="o",markersize=1,
markerfacecolor="green")
plt.show()
if __name__ == "__main__":
# coordinates of 1st point
x0, y0 = 20, 20
# coordinates of 2nd point
x1, y1 = 60, 50
# Function call
DDA(x0, y0, x1, y1)

Practical No. 3b
Python program to implement Bresenhams
algorithm for line generation

import matplotlib.pyplot as plt


plt.title("Bresenham Algorithm")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")

def bres(x1,y1,x2,y2):
x,y = x1,y1
dx = abs(x2 - x1)
dy = abs(y2 -y1)
gradient = dy/float(dx)

if gradient > 1:
dx, dy = dy, dx
x, y = y, x
x1, y1 = y1, x1
x2, y2 = y2, x2

p = 2*dy - dx
print(f"x = {x}, y = {y}")
# Initialize the plotting points
xcoordinates = [x]
ycoordinates = [y]

for k in range(2, dx + 2):


if p > 0:
y = y + 1 if y < y2 else y - 1
p = p + 2 * (dy - dx)
else:
p = p + 2 * dy

x = x + 1 if x < x2 else x - 1

print(f"x = {x}, y = {y}")


xcoordinates.append(x)
ycoordinates.append(y)

plt.plot(xcoordinates, ycoordinates)
plt.show()

def main():
x1 = int(input("Enter the Starting point of x:
"))
y1 = int(input("Enter the Starting point of y:
"))
x2 = int(input("Enter the end point of x: "))
y2 = int(input("Enter the end point of y: "))

bres(x1, y1, x2, y2)

if __name__ == "__main__":
main()

# Practical 5a
# Implementation of 2d scaling
from graphics import *
win = GraphWin("Scale Square", 600, 600)
print("Corner 1")
x1=int(input("Enter x"))
y1=int(input("Enter y"))
c1=Point(x1, y1)

print("Corner 2")
x2=int(input("Enter x"))
y2=int(input("Enter y"))
c2 = Point(x2, y2)

s = Rectangle(c1, c2)
s.draw(win)

sx=float(input("Scaling Factor sx"))


sy=float(input("Scaling Factor sy"))

x1*=sx
x2*=sx
y1*=sy
y2*=sy
c1=Point(x1, y1)
c2 = Point(x2, y2)

ss=Rectangle(c1, c2)
ss.draw(win)

win.getMouse()
win.close()

#Practical 5b
#Implementation of 2D translation
from graphics import *
win = GraphWin("Translate Rectangle",
600, 600)

print("Corner 1")
x1=int(input("Enter x"))
y1=int(input("Enter y"))
c1=Point(x1, y1)

print("Corner 2")
x2=int(input("Enter x"))
y2=int(input("Enter y"))
c2 = Point(x2, y2)

r = Rectangle(c1, c2)
r.draw(win)

dx=int(input("Translation tx"))
dy=int(input("Translation ty"))

x1+=dx
x2+=dx
y1+=dy
y2+=dy

c1 = Point(x1,y1)
c2 = Point(x2, y2)
rt=Rectangle(c1, c2)
rt.draw(win)

win.getMouse()
win.close()

#Practical no 6
#Implementation of 2d Rotation
from graphics import *
from random import randint
from math import sin, cos, radians
import time

def rotateTriangle(triangle, degrees):


angle = radians(degrees)
cosang, sinang = cos(angle), sin(angle)

points = triangle.getPoints()
n = len(points)
cx = sum(p.getX() for p in points) / n
cy = sum(p.getY() for p in points) / n

new_points = []
for p in points:
x, y = p.getX(), p.getY()
tx, ty = x-cx, y-cy
new_x = ( tx*cosang + ty*sinang) + cx
new_y = (-tx*sinang + ty*cosang) + cy
new_points.append(Point(new_x,
new_y))

rotated_ploygon = triangle.clone()
rotated_ploygon.points = new_points
return rotated_ploygon

win = GraphWin('Rotate Triangle', 600, 600)

print("Point 1")
x1=int(input("Enter x:"))
y1=int(input("Enter y:"))

print("Point 2")
x2=int(input("Enter x:"))
y2=int(input("Enter y:"))
print("Point 3")
x3=int(input("Enter x:"))
y3=int(input("Enter y:"))
p1 = Point(x1,y1)
p2 = Point(x2,y2)
p3 = Point(x3, y3)
triangle = Polygon(p1,p2,p3)
triangle.setFill('cyan')
triangle.draw(win)
angle=int(input("Enter the angle of
rotation"))
triangle = rotateTriangle(triangle,angle)
triangle.setFill('blue')
triangle.draw(win)

Prac6
#Python program to draw car in turtle
programming

# Import required library


import turtle
car = turtle.Turtle()
# Below code for drawing rectangular upper
body
car.color('#008000')
car.fillcolor('#008000')
car.penup()
car.goto(0,0)
car.pendown()
car.begin_fill()
car.forward(370)
car.left(90)
car.forward(50)
car.left(90)
car.forward(370)
car.left(90)
car.forward(50)
car.end_fill()

# Below code for drawing window and roof


car.penup()
car.goto(100, 50)
car.pendown()
car.setheading(45)
car.forward(70)
car.setheading(0)
car.forward(100)
car.setheading(-45)
car.forward(70)
car.setheading(90)
car.penup()
car.goto(200, 50)
car.pendown()
car.forward(49.50)

# Below code for drawing two tyres


car.penup()
car.goto(100, -10)
car.pendown()
car.color('#000000')
car.fillcolor('#000000')
car.begin_fill()
car.circle(20)
car.end_fill()
car.penup()
car.goto(300, -10)
car.pendown()
car.color('#000000')
car.fillcolor('#000000')
car.begin_fill()
car.circle(20)
car.end_fill()
car.hideturtle()

Prac 7
# Python program to draw smile
# face emoji using turtle
import turtle

# turtle object
pen = turtle.Turtle()

# function for creation of eye


def eye(col, rad):
pen.down()
pen.fillcolor(col)
pen.begin_fill()
pen.circle(rad)
pen.end_fill()
pen.up()

# draw face
pen.fillcolor('yellow')
pen.begin_fill()
pen.circle(100)
pen.end_fill()
pen.up()

# draw eyes
pen.goto(-40, 120)
eye('white', 15)
pen.goto(-37, 125)
eye('black', 5)
pen.goto(40, 120)
eye('white', 15)
pen.goto(40, 125)
eye('black', 5)

# draw nose
pen.goto(0, 75)
eye('black', 8)

# draw mouth
pen.goto(-40, 85)
pen.down()
pen.right(90)
pen.circle(40, 180)
pen.up()
# draw tongue
pen.goto(-10, 45)
pen.down()
pen.right(180)
pen.fillcolor('red')
pen.begin_fill()
pen.circle(10, 180)
pen.end_fill()
pen.hideturtle()

Prac 8
Python program to implement
Cohen Sutherland algorithm
# for line clipping.

# Defining region codes


INSIDE = 0 # 0000
LEFT = 1 # 0001
RIGHT = 2 # 0010
BOTTOM = 4 # 0100
TOP = 8 # 1000
# Defining x_max, y_max and
x_min, y_min for rectangle
# Since diagonal points are
enough to define a rectangle
x_max = 10.0
y_max = 8.0
x_min = 4.0
y_min = 4.0

# Function to compute region


code for a point(x, y)
def computeCode(x, y):
code = INSIDE
if x < x_min: # to the left
of rectangle
code |= LEFT
elif x > x_max: # to the
right of rectangle
code |= RIGHT
if y < y_min: # below the
rectangle
code |= BOTTOM
elif y > y_max: # above the
rectangle
code |= TOP
return code

# Implementing Cohen-Sutherland
algorithm
# Clipping a line from P1 = (x1,
y1) to P2 = (x2, y2)
# Implementing Cohen-Sutherland
algorithm
# Clipping a line from P1 = (x1,
y1) to P2 = (x2, y2)
def cohenSutherlandClip(x1, y1,
x2, y2):

# Compute region codes for


P1, P2
code1 = computeCode(x1, y1)
code2 = computeCode(x2, y2)
accept = False

while True:

# If both endpoints lie


within rectangle
if code1 == 0 and code2
== 0:
accept = True
break

# If both endpoints are


outside rectangle
elif (code1 & code2) !=
0:
break

# Some segment lies


within the rectangle
else:

# Line needs
clipping
# At least one of
the points is outside,
# select it
x = 1.0
y = 1.0
if code1 != 0:
code_out = code1
else:
code_out = code2

# Find intersection
point
# using formulas y =
y1 + slope * (x - x1),
# x = x1 + (1 /
slope) * (y - y1)
if code_out & TOP:
# Point is above
the clip rectangle
x = x1 + (x2 -
x1) * (y_max - y1) / (y2 - y1)
y = y_max
elif code_out &
BOTTOM:
# Point is below
the clip rectangle
x = x1 + (x2 -
x1) * (y_min - y1) / (y2 - y1)
y = y_min
elif code_out &
RIGHT:
# Point is to
the right of the clip rectangle
y = y1 + (y2 -
y1) * (x_max - x1) / (x2 - x1)
x = x_max
elif code_out &
LEFT:
# Point is to
the left of the clip rectangle
y = y1 + (y2 -
y1) * (x_min - x1) / (x2 - x1)
x = x_min

# Now intersection
point (x, y) is found
# We replace point
outside clipping rectangle
# by intersection
point
if code_out ==
code1:
x1 = x
y1 = y
code1 =
computeCode(x1, y1)
else:
x2 = x
y2 = y
code2 =
computeCode(x2, y2)

if accept:
print("Line accepted
from %.2f, %.2f to %.2f, %.2f" %
(x1, y1, x2, y2))

# Here the user can add


code to display the rectangle
# along with the
accepted (portion of) lines

else:
print("Line rejected")

# Driver script

# First line segment


# P11 = (5, 5), P12 = (7, 7)
cohenSutherlandClip(5, 5, 7, 7)

# Second line segment


# P21 = (7, 9), P22 = (11, 4)
cohenSutherlandClip(7, 9, 11, 4)

# Third line segment


# P31 = (1, 5), P32 = (4, 1)
cohenSutherlandClip(1, 5, 4, 1)
Output:
Line accepted from 5.00, 5.00 to
7.00, 7.00
Line accepted from 7.80, 8.00 to
10.00, 5.25
Line rejected

You might also like