Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Practical 01
- Aim: Study and enlist the basic functions used for graphics in Python language. Give an example for each of them.
- Library: matplotlib, turtle, opencv, pygame
- Basic Functions:
- Function
- Library
- Description
- plot()
- matplotlib
- Draws a line or points.
- Circle(), Rectangle()
- matplotlib.patches
- Draws basic shapes.
- forward(), right()
- turtle
- Turtle graphics for drawing.
- line(), circle()
- opencv
- Draws shapes on an image.
- draw.line()
- pygame
- Draws a line in a game window.
- Code:
- import matplotlib.pyplot as plt
- from matplotlib.patches import Circle, Rectangle
- fig, ax = plt.subplots()
- circle = Circle((0.5, 0.5), 0.3, color='blue', alpha=0.5)
- ax.add_patch(circle)
- rect = Rectangle((0.2, 0.2), 0.4, 0.2, color='green', alpha=0.5)
- ax.add_patch(rect)
- plt.xlim(0, 1)
- plt.ylim(0, 1)
- plt.show()
- Practical 2
- Aim: Draw a co-ordinate axis at the center of the screen.
- Code:
- import matplotlib.pyplot as plt
- plt.xlim(-10, 10)
- plt.ylim(-10, 10)
- plt.axhline(y=0, color='black', linewidth=0.5)
- plt.axvline(x=0, color='black', linewidth=0.5)
- plt.xlabel("X-axis")
- plt.ylabel("Y-axis")
- plt.title("Coordinate Axis at Center")
- plt.show()
- Practical 3
- Aim : Divide your screen into four region, draw circle, rectangle, ellipse and half ellipse in each region with appropriate message.
- Code:
- import matplotlib.pyplot as plt
- from matplotlib.patches import Circle, Rectangle, Ellipse, Arc
- fig, axes = plt.subplots(2, 2, figsize=(10, 8))
- shapes_and_messages = [
- {'shape': 'circle', 'message': 'Circle'},
- {'shape': 'rectangle', 'message': 'Rectangle'},
- {'shape': 'ellipse', 'message': 'Ellipse'},
- {'shape': 'half_ellipse', 'message': 'Half Ellipse'}
- ]
- for i in range(2):
- for j in range(2):
- ax = axes[i, j]
- index = i * 2 + j
- shape_data = shapes_and_messages[index]
- if shape_data['shape'] == 'circle':
- circle = Circle((0.5, 0.5), 0.4, color='skyblue')
- ax.add_patch(circle)
- elif shape_data['shape'] == 'rectangle':
- rect = Rectangle((0.2, 0.2), 0.6, 0.6, color='lightgreen')
- ax.add_patch(rect)
- elif shape_data['shape'] == 'ellipse':
- ellipse = Ellipse((0.5, 0.5), 0.8, 0.4, color='orange')
- ax.add_patch(ellipse)
- elif shape_data['shape'] == 'half_ellipse':
- half_ellipse = Arc((0.5, 0.5), 0.8, 0.6, angle=0, theta1=0, theta2=180, color='lightcoral')
- ax.add_patch(half_ellipse)
- ax.set_aspect('equal')# Ensure shapes appear as intended
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.text(0.5, 0.5, shape_data['message'], ha='center', va='center')
- ax.set_xticks([])
- ax.set_yticks([])
- plt.tight_layout()
- # Display the plot
- plt.show()
- Practical 4
- Aim: Draw a simple hut on the screen.
- Code:
- import matplotlib.pyplot as plt
- from matplotlib.patches import Rectangle, Polygon
- fig, ax = plt.subplots(figsize=(8, 6))
- hut_base = Rectangle((0.3, 0.2), 0.4, 0.3, color='saddlebrown')
- ax.add_patch(hut_base)
- roof = Polygon([[0.3, 0.5], [0.5, 0.7], [0.7, 0.5]], color='peru')
- ax.add_patch(roof)
- door = Rectangle((0.45, 0.2), 0.1, 0.15, color='darkred')
- ax.add_patch(door)
- window = Rectangle((0.35, 0.35), 0.1, 0.1, color='lightblue', edgecolor='black')
- ax.add_patch(window)
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_aspect('equal')
- ax.axis('off')
- plt.show()
- Practical 5
- Aim: i. Circle ii. Rectangle iii. Square iv. Concentric Circles v. Ellipse vi. Line code:
- import matplotlib.pyplot as plt
- from matplotlib.patches import Circle, Rectangle, Ellipse
- fig, ax = plt.subplots()
- circle = Circle((0.5, 0.5), 0.4, color='skyblue')
- ax.add_patch(circle)
- ax.set_aspect('equal')
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Circle")
- plt.show()
- fig, ax = plt.subplots()
- rect = Rectangle((0.2, 0.2), 0.6, 0.6, color='lightgreen')
- ax.add_patch(rect)
- ax.set_aspect('equal')
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Rectangle")
- plt.show()
- fig, ax = plt.subplots()
- square = Rectangle((0.25, 0.25), 0.5, 0.5, color='yellow') # width =
- ax.add_patch(square)
- ax.set_aspect('equal')
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Square")
- plt.show()
- fig, ax = plt.subplots()
- circle1 = Circle((0.5, 0.5), 0.3, color='lightcoral')
- circle2 = Circle((0.5, 0.5), 0.2, color='skyblue')
- ax.add_patch(circle1)
- ax.add_patch(circle2)
- ax.set_aspect('equal')
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Concentric Circles")
- plt.show()
- fig, ax = plt.subplots()
- ellipse = Ellipse((0.5, 0.5), 0.8, 0.4, color='orange')
- ax.add_patch(ellipse)
- ax.set_aspect('equal')
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Ellipse")
- plt.show()
- fig, ax = plt.subplots()
- ax.plot([0.1, 0.9], [0.2, 0.8], color='purple', linewidth=3) # Example
- ax.set_xlim(0, 1)
- ax.set_ylim(0, 1)
- ax.set_title("Line")
- plt.show()
- Practical 6
- Aim : Write a program to implement 2D scaling and Rotation.
- Code:
- import matplotlib.pyplot as plt
- import numpy as np
- def scale_and_rotate(x, y, sx, sy, angle_degrees):
- angle_radians = np.radians(angle_degrees)
- scaling_matrix = np.array([[sx, 0], [0, sy]])
- rotation_matrix = np.array([[np.cos(angle_radians), -np.sin(angle_radians)],
- [np.sin(angle_radians), np.cos(angle_radians)]])
- transformation_matrix = rotation_matrix @ scaling_matrix
- points = np.array([x, y])
- scaled_rotated_points = transformation_matrix @ points
- return scaled_rotated_points[0], scaled_rotated_points[1]
- x = np.array([0, 1, 1, 0, 0])
- y = np.array([0, 0, 1, 1, 0])
- sx = 2
- sy = 0.5
- angle = 45
- x_scaled_rotated, y_scaled_rotated = scale_and_rotate(x, y, sx, sy, angle)
- plt.figure(figsize=(10, 5))
- plt.subplot(1, 2, 1)
- plt.plot(x, y, 'b-', label='Original')
- plt.title('Original Shape')
- plt.subplot(1, 2, 2)
- plt.plot(x_scaled_rotated, y_scaled_rotated, 'r-', label='Transformed')
- plt.title('Scaled and Rotated Shape')
- plt.show()
- Practical 7
- Aim: Develop the program for DDA Line drawing algorithm.
- Code:
- import matplotlib.pyplot as plt
- def dda_line(x1, y1, x2, y2):
- dx = x2 - x1
- dy = y2 - y1
- steps = abs(dx) if abs(dx) > abs(dy) else abs(dy)
- x_increment = dx / steps
- y_increment = dy / steps
- x = x1
- y = y1
- x_coordinates = []
- y_coordinates = []
- for _ in range(int(steps) + 1):
- x_coordinates.append(round(x))
- y_coordinates.append(round(y))
- x += x_increment
- y += y_increment
- return x_coordinates, y_coordinates
- x1, y1 = 1, 1
- x2, y2 = 8, 5
- x_coords, y_coords = dda_line(x1, y1, x2, y2)
- plt.plot(x_coords, y_coords, marker='o', linestyle='-')
- plt.title('DDA Line Drawing Algorithm')
- plt.xlabel('X-axis')
- plt.ylabel('Y-axis')
- plt.grid(True)
- plt.show()
- Practical 8
- Aim: Develop the program for Bresenham's Line drawing algorithm.
- Code: import matplotlib.pyplot as plt
- def bresenham_line(x1, y1, x2, y2):
- points = []
- dx = abs(x2 - x1)
- dy = abs(y2 - y1)
- sx = 1 if x2 > x1 else -1
- sy = 1 if y2 > y1 else -1
- err = dx - dy
- while True:
- points.append((x1, y1))
- if x1 == x2 and y1 == y2:
- break
- e2 = 2 * err
- if e2 > -dy:
- err -= dy
- x1 += sx
- if e2 < dx:
- err += dx
- y1 += sy
- return points
- x_start, y_start = 2, 3
- x_end, y_end = 10, 8
- line_points = bresenham_line(x_start, y_start, x_end, y_end)
- print("Points on the line:")
- for point in line_points:
- print(point)
- x_coords, y_coords = zip(*line_points)
- plt.figure(figsize=(8, 6))
- plt.plot(x_coords, y_coords, 'bo-', label="Bresenham's Line")
- plt.title("Bresenham's Line Drawing Algorithm")
- plt.xlabel("X")
- plt.ylabel("Y")
- plt.grid()
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement