如何用Python Matplotlib将圆弧转为多边形(已知起止点、圆心、半径)
Convert Circular Arc to Polygonal Point List (MS Paint-style Jagged Arc)
Got it, let's fix this up! You're currently using a Bézier curve, but you want to replace it with a polygonal approximation of a circular arc—where you get a list of connected points that look like the jagged arc you'd draw with MS Paint. Here's a complete solution that uses your known start_point, end_point, center, and radius to generate those points.
Step-by-Step Breakdown
First, we need to:
- Calculate the start and end angles of the arc relative to the center point
- Generate a sequence of evenly spaced angles between those two values (fewer angles = more jagged; more angles = smoother)
- Convert each angle back to a coordinate point using basic trigonometry
- Use those points to draw the polygonal arc and output the point list
Complete Working Code
import matplotlib.pyplot as plt import math # Your known arc parameters start_point = (25, 50) end_point = (50, 25) center = (25, 25) radius = 25 # Fixed the syntax error from your original code def arc_to_polygon_points(center, start_point, end_point, radius, num_segments=20): """ Convert a circular arc into a list of polygon vertices. Args: center: (x, y) tuple of the arc's center start_point: (x, y) tuple of the arc's starting point end_point: (x, y) tuple of the arc's ending point radius: Radius of the circular arc num_segments: Number of line segments to split the arc into (more = smoother) Returns: List of (x, y) points forming the polygonal arc """ # Unpack coordinates for easier calculations cx, cy = center sx, sy = start_point ex, ey = end_point # Calculate angles for start/end points relative to the center start_angle = math.atan2(sy - cy, sx - cx) end_angle = math.atan2(ey - cy, ex - cx) # Handle arcs that wrap around the 0/360-degree mark angle_diff = end_angle - start_angle if angle_diff < 0: angle_diff += 2 * math.pi # Generate evenly spaced angles between start and end angles = [start_angle + i * angle_diff / num_segments for i in range(num_segments + 1)] # Convert each angle back to a coordinate point polygon_points = [] for angle in angles: x = cx + radius * math.cos(angle) y = cy + radius * math.sin(angle) polygon_points.append((round(x, 2), round(y, 2))) return polygon_points # Generate the polygonal arc point list polygon_points = arc_to_polygon_points(center, start_point, end_point, radius, num_segments=10) # Plot the result (with original Bézier curve for comparison) plt.figure(figsize=(6,6)) # Draw the polygonal arc plt.plot([p[0] for p in polygon_points], [p[1] for p in polygon_points], lw=0.75, label='Polygonal Arc') # Draw vertex markers for clarity plt.scatter([p[0] for p in polygon_points], [p[1] for p in polygon_points], s=10, color='red') # Optional: Plot original Bézier curve for reference from matplotlib.path import Path import matplotlib.patches as patches mid_point = (45, 45) verts = [start_point, mid_point, end_point] codes = [Path.MOVETO, Path.CURVE3, Path.CURVE3] path = Path(verts, codes) shape = patches.PathPatch(path, facecolor='none', lw=0.75, linestyle='--', label='Original Bézier') plt.gca().add_patch(shape) plt.axis('scaled') plt.legend() plt.show() # Print the final list of polygon points print("Polygonal Arc Point List:") for idx, point in enumerate(polygon_points): print(f"Point {idx+1}: {point}")
Key Details to Adjust
num_segments: Tweak this value to control the jaggedness. Use 5 for a very blocky arc, or 50 for something almost indistinguishable from a smooth circle.- Angle Handling: The code automatically fixes cases where the arc wraps around the 0/360-degree boundary, so you don't have to worry about the order of your start/end points.
- Point Precision: The
round()function in the point conversion keeps coordinates clean, but you can remove it if you need full floating-point precision.
内容的提问来源于stack exchange,提问作者Kartheek Palepu
相关产品推荐
相关产品推荐

