A lightweight, interactive desktop application that lets users draw two triangles on a graphical canvas, automatically computes their areas using Heron's formula, and visually highlights the larger triangle. Built with Python's graphics.py library, it demonstrates core computational geometry concepts through an intuitive point-and-click interface.
- Interactive Drawing — Click to place vertices directly on a coordinate-based canvas; triangles render in real time.
- Automated Area Calculation — Computes side lengths via the Euclidean distance formula and derives area using Heron's formula.
- Visual Comparison — Automatically identifies the larger triangle and recolors it red for instant visual feedback.
- Dynamic Labels — Displays the computed area (to two decimal places) beneath each triangle, positioned relative to the shape's bounding box.
- Self-Contained GUI — Runs as a standalone
.pywwindow application with no external dependencies beyond thegraphicsmodule.
| Layer | Technology |
|---|---|
| Language | Python 3 |
| GUI Library | Zelle Graphics (graphics.py) |
| Math | math standard library (square root for distance & Heron's formula) |
User Click Events
│
▼
┌──────────────┐ ┌───────────────────┐ ┌──────────────────┐
│ GraphWin │────▶│ distance(p1, p2) │────▶│ area(s1, s2, s3)│
│ (Canvas) │ │ Euclidean dist. │ │ Heron's formula │
└──────────────┘ └───────────────────┘ └──────────────────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────────┐
│ Polygon │◀─────────────────────────────│ Compare areas & │
│ Rendering │ visual update │ highlight larger│
└──────────────┘ └──────────────────┘
- The user clicks three points on the canvas to define each triangle.
distance()calculates Euclidean distances between each pair of vertices.area()applies Heron's formula to derive the triangle's area from its three side lengths.- The application compares both areas, fills the larger triangle red, and renders area labels below each shape.
- Python 3.6 or later
# Clone the repository
git clone https://github.com/samie-mirghani/python-graphics.git
cd python-graphics
# Run the application
python "Project 4.pyw"Note: The project uses John Zelle's
graphics.pymodule, which wraps Python's built-intkinter. Ensuretkinteris installed with your Python distribution (it is included by default on most systems).
- Launch the program — a 600 × 600 pink canvas opens.
- Click three points to draw the first triangle (filled cornflower blue).
- Click three more points to draw the second triangle.
- Click anywhere to highlight the larger triangle in red and display both areas.
- Click once more to exit.
python · graphics · computational-geometry · herons-formula · tkinter · interactive · educational
This project is provided for educational purposes.
Built by Shamsadean Mirghani