Advertisement
Guest User

Untitled

a guest
Mar 14th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. def shrink_points(points, center, factor):
  2. """
  3. Shrinks a set of 2D points around a given center.
  4.  
  5. Args:
  6. points (list): List of 2D points represented as tuples (x, y).
  7. center (tuple): The center point (x, y) around which the points should be shrunk.
  8. factor (float): Shrink factor. A factor of 0.5 will shrink the points by half, 2 will double the distance, etc.
  9.  
  10. Returns:
  11. list: A list of the shrunk 2D points as tuples (x, y).
  12. """
  13. if factor <= 0:
  14. raise ValueError("Shrink factor must be greater than 0")
  15.  
  16. shrunk_points = []
  17. for point in points:
  18. x = center[0] + (point[0] - center[0]) * factor
  19. y = center[1] + (point[1] - center[1]) * factor
  20. shrunk_points.append((x, y))
  21.  
  22. return shrunk_points
  23.  
  24. # Example usage:
  25. points = [(2, 2), (4, 4), (6, 2), (4, 0)]
  26. center = (4, 2)
  27. factor = 0.5
  28.  
  29. shrunk_points = shrink_points(points, center, factor)
  30. print(shrunk_points)
  31.  
  32. import matplotlib.pyplot as plt
  33.  
  34. def plot_points(original_points, shrunk_points, center):
  35. """
  36. Plots original and shrunk points using matplotlib.
  37.  
  38. Args:
  39. original_points (list): List of original 2D points represented as tuples (x, y).
  40. shrunk_points (list): List of shrunk 2D points represented as tuples (x, y).
  41. center (tuple): The center point (x, y) around which the points were shrunk.
  42. """
  43. original_x, original_y = zip(*original_points)
  44. shrunk_x, shrunk_y = zip(*shrunk_points)
  45.  
  46. plt.scatter(original_x, original_y, c='red', label='Original Points')
  47. plt.scatter(shrunk_x, shrunk_y, c='blue', label='Shrunk Points')
  48. plt.scatter(center[0], center[1], c='green', marker='x', label='Center')
  49.  
  50. plt.legend()
  51. plt.xlabel('x')
  52. plt.ylabel('y')
  53. plt.title('Shrinking Points Around a Center')
  54.  
  55. plt.show()
  56.  
  57. # Example usage:
  58. points = [(2, 2), (4, 4), (6, 2), (4, 0)]
  59. center = (4, 2)
  60. factor = 0.5
  61.  
  62. shrunk_points = shrink_points(points, center, factor)
  63. plot_points(points, shrunk_points, center)
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement