Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def shrink_points(points, center, factor):
- """
- Shrinks a set of 2D points around a given center.
- Args:
- points (list): List of 2D points represented as tuples (x, y).
- center (tuple): The center point (x, y) around which the points should be shrunk.
- factor (float): Shrink factor. A factor of 0.5 will shrink the points by half, 2 will double the distance, etc.
- Returns:
- list: A list of the shrunk 2D points as tuples (x, y).
- """
- if factor <= 0:
- raise ValueError("Shrink factor must be greater than 0")
- shrunk_points = []
- for point in points:
- x = center[0] + (point[0] - center[0]) * factor
- y = center[1] + (point[1] - center[1]) * factor
- shrunk_points.append((x, y))
- return shrunk_points
- # Example usage:
- points = [(2, 2), (4, 4), (6, 2), (4, 0)]
- center = (4, 2)
- factor = 0.5
- shrunk_points = shrink_points(points, center, factor)
- print(shrunk_points)
- import matplotlib.pyplot as plt
- def plot_points(original_points, shrunk_points, center):
- """
- Plots original and shrunk points using matplotlib.
- Args:
- original_points (list): List of original 2D points represented as tuples (x, y).
- shrunk_points (list): List of shrunk 2D points represented as tuples (x, y).
- center (tuple): The center point (x, y) around which the points were shrunk.
- """
- original_x, original_y = zip(*original_points)
- shrunk_x, shrunk_y = zip(*shrunk_points)
- plt.scatter(original_x, original_y, c='red', label='Original Points')
- plt.scatter(shrunk_x, shrunk_y, c='blue', label='Shrunk Points')
- plt.scatter(center[0], center[1], c='green', marker='x', label='Center')
- plt.legend()
- plt.xlabel('x')
- plt.ylabel('y')
- plt.title('Shrinking Points Around a Center')
- plt.show()
- # Example usage:
- points = [(2, 2), (4, 4), (6, 2), (4, 0)]
- center = (4, 2)
- factor = 0.5
- shrunk_points = shrink_points(points, center, factor)
- plot_points(points, shrunk_points, center)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement