Advertisement
User_codes

Untitled

Mar 10th, 2025
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 KB | None | 0 0
  1. Practical 01
  2.  
  3. Aim: Study and enlist the basic functions used for graphics in Python language. Give an example for each of them.
  4. Library: matplotlib, turtle, opencv, pygame
  5. Basic Functions:
  6. Function
  7. Library
  8. Description
  9. plot()
  10. matplotlib
  11. Draws a line or points.
  12. Circle(), Rectangle()
  13. matplotlib.patches
  14. Draws basic shapes.
  15. forward(), right()
  16. turtle
  17. Turtle graphics for drawing.
  18. line(), circle()
  19. opencv
  20. Draws shapes on an image.
  21. draw.line()
  22. pygame
  23. Draws a line in a game window.
  24.  
  25.  
  26. Code:
  27. import matplotlib.pyplot as plt
  28. from matplotlib.patches import Circle, Rectangle
  29.  
  30.  
  31. fig, ax = plt.subplots()
  32.  
  33.  
  34. circle = Circle((0.5, 0.5), 0.3, color='blue', alpha=0.5)
  35. ax.add_patch(circle)
  36.  
  37.  
  38. rect = Rectangle((0.2, 0.2), 0.4, 0.2, color='green', alpha=0.5)
  39. ax.add_patch(rect)
  40.  
  41.  
  42. plt.xlim(0, 1)
  43. plt.ylim(0, 1)
  44. plt.show()
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. Practical 2
  56.  
  57. Aim: Draw a co-ordinate axis at the center of the screen.
  58. Code:
  59. import matplotlib.pyplot as plt
  60.  
  61.  
  62. plt.xlim(-10, 10)
  63. plt.ylim(-10, 10)
  64.  
  65.  
  66. plt.axhline(y=0, color='black', linewidth=0.5)
  67. plt.axvline(x=0, color='black', linewidth=0.5)
  68.  
  69.  
  70. plt.xlabel("X-axis")
  71. plt.ylabel("Y-axis")
  72. plt.title("Coordinate Axis at Center")
  73.  
  74.  
  75. plt.show()
  76.  
  77.  
  78. Practical 3
  79.  
  80. Aim : Divide your screen into four region, draw circle, rectangle, ellipse and half ellipse in each region with appropriate message.
  81. Code:
  82. import matplotlib.pyplot as plt
  83. from matplotlib.patches import Circle, Rectangle, Ellipse, Arc
  84.  
  85.  
  86. fig, axes = plt.subplots(2, 2, figsize=(10, 8))
  87.  
  88.  
  89. shapes_and_messages = [
  90. {'shape': 'circle', 'message': 'Circle'},
  91. {'shape': 'rectangle', 'message': 'Rectangle'},
  92. {'shape': 'ellipse', 'message': 'Ellipse'},
  93. {'shape': 'half_ellipse', 'message': 'Half Ellipse'}
  94. ]
  95.  
  96.  
  97. for i in range(2):
  98. for j in range(2):
  99. ax = axes[i, j]
  100. index = i * 2 + j
  101. shape_data = shapes_and_messages[index]
  102.  
  103.  
  104. if shape_data['shape'] == 'circle':
  105. circle = Circle((0.5, 0.5), 0.4, color='skyblue')
  106. ax.add_patch(circle)
  107.  
  108.  
  109. elif shape_data['shape'] == 'rectangle':
  110. rect = Rectangle((0.2, 0.2), 0.6, 0.6, color='lightgreen')
  111. ax.add_patch(rect)
  112.  
  113. elif shape_data['shape'] == 'ellipse':
  114. ellipse = Ellipse((0.5, 0.5), 0.8, 0.4, color='orange')
  115. ax.add_patch(ellipse)
  116.  
  117.  
  118. elif shape_data['shape'] == 'half_ellipse':
  119. half_ellipse = Arc((0.5, 0.5), 0.8, 0.6, angle=0, theta1=0, theta2=180, color='lightcoral')
  120. ax.add_patch(half_ellipse)
  121.  
  122. ax.set_aspect('equal')# Ensure shapes appear as intended
  123. ax.set_xlim(0, 1)
  124. ax.set_ylim(0, 1)
  125. ax.text(0.5, 0.5, shape_data['message'], ha='center', va='center')
  126. ax.set_xticks([])
  127. ax.set_yticks([])
  128.  
  129.  
  130. plt.tight_layout()
  131.  
  132.  
  133. # Display the plot
  134. plt.show()
  135.  
  136.  
  137.  
  138.  
  139.  
  140. Practical 4
  141.  
  142. Aim: Draw a simple hut on the screen.
  143. Code:
  144. import matplotlib.pyplot as plt
  145. from matplotlib.patches import Rectangle, Polygon
  146.  
  147.  
  148.  
  149.  
  150. fig, ax = plt.subplots(figsize=(8, 6))
  151.  
  152.  
  153. hut_base = Rectangle((0.3, 0.2), 0.4, 0.3, color='saddlebrown')
  154. ax.add_patch(hut_base)
  155.  
  156.  
  157. roof = Polygon([[0.3, 0.5], [0.5, 0.7], [0.7, 0.5]], color='peru')
  158. ax.add_patch(roof)
  159.  
  160.  
  161. door = Rectangle((0.45, 0.2), 0.1, 0.15, color='darkred')
  162. ax.add_patch(door)
  163.  
  164.  
  165. window = Rectangle((0.35, 0.35), 0.1, 0.1, color='lightblue', edgecolor='black')
  166. ax.add_patch(window)
  167.  
  168.  
  169. ax.set_xlim(0, 1)
  170. ax.set_ylim(0, 1)
  171. ax.set_aspect('equal')
  172. ax.axis('off')
  173.  
  174.  
  175. plt.show()
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. Practical 5
  186.  
  187. Aim: i. Circle ii. Rectangle iii. Square iv. Concentric Circles v. Ellipse vi. Line code:
  188. import matplotlib.pyplot as plt
  189. from matplotlib.patches import Circle, Rectangle, Ellipse
  190.  
  191.  
  192. fig, ax = plt.subplots()
  193. circle = Circle((0.5, 0.5), 0.4, color='skyblue')
  194. ax.add_patch(circle)
  195. ax.set_aspect('equal')
  196. ax.set_xlim(0, 1)
  197. ax.set_ylim(0, 1)
  198. ax.set_title("Circle")
  199. plt.show()
  200.  
  201.  
  202. fig, ax = plt.subplots()
  203. rect = Rectangle((0.2, 0.2), 0.6, 0.6, color='lightgreen')
  204. ax.add_patch(rect)
  205. ax.set_aspect('equal')
  206. ax.set_xlim(0, 1)
  207. ax.set_ylim(0, 1)
  208. ax.set_title("Rectangle")
  209. plt.show()
  210.  
  211.  
  212. fig, ax = plt.subplots()
  213. square = Rectangle((0.25, 0.25), 0.5, 0.5, color='yellow') # width =
  214.  
  215.  
  216. ax.add_patch(square)
  217. ax.set_aspect('equal')
  218. ax.set_xlim(0, 1)
  219. ax.set_ylim(0, 1)
  220. ax.set_title("Square")
  221. plt.show()
  222.  
  223.  
  224. fig, ax = plt.subplots()
  225. circle1 = Circle((0.5, 0.5), 0.3, color='lightcoral')
  226. circle2 = Circle((0.5, 0.5), 0.2, color='skyblue')
  227. ax.add_patch(circle1)
  228. ax.add_patch(circle2)
  229. ax.set_aspect('equal')
  230. ax.set_xlim(0, 1)
  231. ax.set_ylim(0, 1)
  232. ax.set_title("Concentric Circles")
  233. plt.show()
  234.  
  235.  
  236. fig, ax = plt.subplots()
  237. ellipse = Ellipse((0.5, 0.5), 0.8, 0.4, color='orange')
  238. ax.add_patch(ellipse)
  239. ax.set_aspect('equal')
  240. ax.set_xlim(0, 1)
  241. ax.set_ylim(0, 1)
  242. ax.set_title("Ellipse")
  243. plt.show()
  244.  
  245.  
  246. fig, ax = plt.subplots()
  247. ax.plot([0.1, 0.9], [0.2, 0.8], color='purple', linewidth=3) # Example
  248. ax.set_xlim(0, 1)
  249. ax.set_ylim(0, 1)
  250. ax.set_title("Line")
  251. plt.show()
  252.  
  253.  
  254.  
  255.  
  256.  
  257. Practical 6
  258.  
  259. Aim : Write a program to implement 2D scaling and Rotation.
  260. Code:
  261.  
  262.  
  263. import matplotlib.pyplot as plt
  264. import numpy as np
  265.  
  266.  
  267. def scale_and_rotate(x, y, sx, sy, angle_degrees):
  268.  
  269.  
  270.  
  271. angle_radians = np.radians(angle_degrees)
  272.  
  273.  
  274.  
  275. scaling_matrix = np.array([[sx, 0], [0, sy]])
  276. rotation_matrix = np.array([[np.cos(angle_radians), -np.sin(angle_radians)],
  277. [np.sin(angle_radians), np.cos(angle_radians)]])
  278.  
  279.  
  280.  
  281. transformation_matrix = rotation_matrix @ scaling_matrix
  282.  
  283.  
  284.  
  285. points = np.array([x, y])
  286. scaled_rotated_points = transformation_matrix @ points
  287.  
  288.  
  289. return scaled_rotated_points[0], scaled_rotated_points[1]
  290.  
  291.  
  292. x = np.array([0, 1, 1, 0, 0])
  293. y = np.array([0, 0, 1, 1, 0])
  294.  
  295.  
  296. sx = 2
  297. sy = 0.5
  298. angle = 45
  299.  
  300.  
  301. x_scaled_rotated, y_scaled_rotated = scale_and_rotate(x, y, sx, sy, angle)
  302.  
  303.  
  304. plt.figure(figsize=(10, 5))
  305. plt.subplot(1, 2, 1)
  306. plt.plot(x, y, 'b-', label='Original')
  307. plt.title('Original Shape')
  308.  
  309.  
  310. plt.subplot(1, 2, 2)
  311. plt.plot(x_scaled_rotated, y_scaled_rotated, 'r-', label='Transformed')
  312. plt.title('Scaled and Rotated Shape')
  313. plt.show()
  314.  
  315.  
  316.  
  317. Practical 7
  318.  
  319. Aim: Develop the program for DDA Line drawing algorithm.
  320. Code:
  321.  
  322.  
  323. import matplotlib.pyplot as plt
  324.  
  325.  
  326. def dda_line(x1, y1, x2, y2):
  327. dx = x2 - x1
  328. dy = y2 - y1
  329.  
  330.  
  331. steps = abs(dx) if abs(dx) > abs(dy) else abs(dy)
  332.  
  333.  
  334. x_increment = dx / steps
  335. y_increment = dy / steps
  336.  
  337.  
  338. x = x1
  339. y = y1
  340.  
  341.  
  342. x_coordinates = []
  343. y_coordinates = []
  344.  
  345.  
  346. for _ in range(int(steps) + 1):
  347. x_coordinates.append(round(x))
  348. y_coordinates.append(round(y))
  349. x += x_increment
  350. y += y_increment
  351. return x_coordinates, y_coordinates
  352.  
  353.  
  354. x1, y1 = 1, 1
  355. x2, y2 = 8, 5
  356.  
  357.  
  358. x_coords, y_coords = dda_line(x1, y1, x2, y2)
  359.  
  360.  
  361. plt.plot(x_coords, y_coords, marker='o', linestyle='-')
  362. plt.title('DDA Line Drawing Algorithm')
  363. plt.xlabel('X-axis')
  364. plt.ylabel('Y-axis')
  365. plt.grid(True)
  366. plt.show()
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373. Practical 8
  374.  
  375. Aim: Develop the program for Bresenham's Line drawing algorithm.
  376. Code: import matplotlib.pyplot as plt
  377.  
  378.  
  379. def bresenham_line(x1, y1, x2, y2):
  380. points = []
  381. dx = abs(x2 - x1)
  382. dy = abs(y2 - y1)
  383. sx = 1 if x2 > x1 else -1
  384. sy = 1 if y2 > y1 else -1
  385. err = dx - dy
  386.  
  387.  
  388. while True:
  389. points.append((x1, y1))
  390. if x1 == x2 and y1 == y2:
  391. break
  392. e2 = 2 * err
  393. if e2 > -dy:
  394. err -= dy
  395. x1 += sx
  396. if e2 < dx:
  397. err += dx
  398. y1 += sy
  399.  
  400. return points
  401.  
  402.  
  403. x_start, y_start = 2, 3
  404. x_end, y_end = 10, 8
  405.  
  406.  
  407. line_points = bresenham_line(x_start, y_start, x_end, y_end)
  408.  
  409.  
  410. print("Points on the line:")
  411. for point in line_points:
  412. print(point)
  413.  
  414.  
  415. x_coords, y_coords = zip(*line_points)
  416. plt.figure(figsize=(8, 6))
  417. plt.plot(x_coords, y_coords, 'bo-', label="Bresenham's Line")
  418. plt.title("Bresenham's Line Drawing Algorithm")
  419. plt.xlabel("X")
  420. plt.ylabel("Y")
  421. plt.grid()
  422. plt.legend()
  423. plt.show()
  424.  
  425.  
  426.  
  427.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement