Buckets:
tahamajs/Sysmem2_in_AI / ComputerAssignments /CA5_geometric_reasoning /src /visualization /diagram.py
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as patches | |
| from matplotlib.patches import Circle, Polygon | |
| import numpy as np | |
| from typing import List, Dict, Tuple, Any, Optional | |
| from ..core.geometry import OlympiadProblem | |
| class DiagramVisualizer: | |
| def __init__(self, figsize=(10, 8), style="professional"): | |
| self.figsize = figsize | |
| self.style = style | |
| self._setup_style() | |
| def _setup_style(self): | |
| if self.style == "professional": | |
| plt.style.use("seaborn-v0_8") | |
| else: | |
| plt.style.use("default") | |
| def visualize_problem( | |
| self, problem: OlympiadProblem, save_path: Optional[str] = None | |
| ) -> Tuple[plt.Figure, plt.Axes]: | |
| fig, ax = plt.subplots(1, 1, figsize=self.figsize) | |
| for point in problem.points.values(): | |
| if point.x is not None and point.y is not None: | |
| ax.plot(point.x, point.y, "ko", markersize=8) | |
| ax.annotate( | |
| point.name, | |
| (point.x, point.y), | |
| xytext=(5, 5), | |
| textcoords="offset points", | |
| fontsize=12, | |
| fontweight="bold", | |
| ) | |
| for line in problem.lines.values(): | |
| if len(line.points) >= 2: | |
| x_coords = [p.x for p in line.points if p.x is not None] | |
| y_coords = [p.y for p in line.points if p.y is not None] | |
| if len(x_coords) >= 2: | |
| ax.plot(x_coords, y_coords, "b-", linewidth=2, alpha=0.7) | |
| for circle in problem.circles.values(): | |
| if ( | |
| circle.center.x is not None | |
| and circle.center.y is not None | |
| and circle.radius is not None | |
| ): | |
| circle_patch = Circle( | |
| (circle.center.x, circle.center.y), | |
| circle.radius, | |
| fill=False, | |
| color="red", | |
| linewidth=2, | |
| ) | |
| ax.add_patch(circle_patch) | |
| for triangle in problem.triangles.values(): | |
| if all(v.x is not None and v.y is not None for v in triangle.vertices): | |
| vertices = [(v.x, v.y) for v in triangle.vertices] | |
| triangle_patch = Polygon( | |
| vertices, fill=False, edgecolor="green", linewidth=2 | |
| ) | |
| ax.add_patch(triangle_patch) | |
| ax.set_aspect("equal") | |
| ax.grid(True, alpha=0.3) | |
| ax.set_title(f"Problem: {problem.name}", fontsize=14, fontweight="bold") | |
| if save_path: | |
| plt.savefig(save_path, dpi=300, bbox_inches="tight") | |
| plt.tight_layout() | |
| return fig, ax | |
| def create_geometric_diagram( | |
| self, | |
| points: Dict[str, Tuple[float, float]], | |
| lines: List[Tuple[str, str]] = None, | |
| circles: List[Dict[str, Any]] = None, | |
| triangles: List[List[str]] = None, | |
| title: str = "Geometric Diagram", | |
| ) -> Tuple[plt.Figure, plt.Axes]: | |
| fig, ax = plt.subplots(1, 1, figsize=self.figsize) | |
| for name, (x, y) in points.items(): | |
| ax.plot(x, y, "ko", markersize=8) | |
| ax.annotate( | |
| name, | |
| (x, y), | |
| xytext=(5, 5), | |
| textcoords="offset points", | |
| fontsize=12, | |
| fontweight="bold", | |
| ) | |
| if lines: | |
| for p1, p2 in lines: | |
| if p1 in points and p2 in points: | |
| x1, y1 = points[p1] | |
| x2, y2 = points[p2] | |
| ax.plot([x1, x2], [y1, y2], "b-", linewidth=2, alpha=0.7) | |
| if circles: | |
| for circle in circles: | |
| center = circle.get("center") | |
| radius = circle.get("radius") | |
| if center in points and radius: | |
| x, y = points[center] | |
| circle_patch = Circle( | |
| (x, y), radius, fill=False, color="red", linewidth=2 | |
| ) | |
| ax.add_patch(circle_patch) | |
| if triangles: | |
| for triangle_points in triangles: | |
| if all(p in points for p in triangle_points): | |
| vertices = [points[p] for p in triangle_points] | |
| triangle_patch = Polygon( | |
| vertices, fill=False, edgecolor="green", linewidth=2 | |
| ) | |
| ax.add_patch(triangle_patch) | |
| ax.set_aspect("equal") | |
| ax.grid(True, alpha=0.3) | |
| ax.set_title(title, fontsize=14, fontweight="bold") | |
| plt.tight_layout() | |
| return fig, ax | |
| def animate_proof_steps( | |
| self, | |
| problem: OlympiadProblem, | |
| proof_steps: List[str], | |
| save_path: Optional[str] = None, | |
| ) -> List[plt.Figure]: | |
| figures = [] | |
| for i, step in enumerate(proof_steps): | |
| fig, ax = self.visualize_problem(problem) | |
| ax.text( | |
| 0.02, | |
| 0.98, | |
| f"Step {i+1}: {step}", | |
| transform=ax.transAxes, | |
| fontsize=10, | |
| verticalalignment="top", | |
| bbox=dict(boxstyle="round,pad=0.3", facecolor="yellow", alpha=0.7), | |
| ) | |
| figures.append(fig) | |
| if save_path: | |
| plt.savefig(f"{save_path}_step_{i+1}.png", dpi=300, bbox_inches="tight") | |
| return figures | |
Xet Storage Details
- Size:
- 5.47 kB
- Xet hash:
- a7e3e2e90996b056b2ca60f9c1efc5e033f4fc63bfa0ee105611e9ea9f4fa459
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.