Professional Documents
Culture Documents
1
1a. Python program to plot a point.
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
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]
x = x + 1 if x < x2 else x - 1
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: "))
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)
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
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
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
Prac 7
# Python program to draw smile
# face emoji using turtle
import turtle
# turtle object
pen = turtle.Turtle()
# 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.
# 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):
while True:
# 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))
else:
print("Line rejected")
# Driver script