Chuyển đến nội dung

Bài 36 · Projects and Practice

Project: Build a Geometry Toolkit

Build a small Python toolkit that computes lengths, areas, volumes and polygon properties, test it against known answers, and plot a polygon report.

In this project you turn the formulas from the whole track into a small, reliable toolkit. You will write functions, test them against answers you know are right, and then use the toolkit to analyze a polygon and draw it. Along the way you practice the habit mathematicians and engineers share: never trust a result you have not checked in a second way.

You need no experience beyond the geometry in this track and basic Python (functions, lists, loops). Each step is a runnable program.

Step 1: write the functions

A good toolkit has small functions that each do one thing, with clear names and units in the comments. We group them by topic: lengths, areas, solids and coordinates.

Design rules we will follow:

  • One formula, one function. No function does two jobs.
  • Validate input. A negative radius or a triangle that cannot exist should raise a clear error rather than return nonsense.
  • Keep π exact until the end by using math.pi and never typing 3.14.

Step 2: test against known answers

A unit test compares a function's output with an answer you already know. We use answers from this track: a 3-4-5 triangle, a circle of radius 5, the capsule volume and so on. We use math.isclose because decimal numbers carry tiny rounding errors.

Python The toolkit and its tests

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All lines print PASS. If you change a formula on purpose (try 4 / 3 to 3 / 4 in the sphere function), a test turns FAIL, which is exactly why we write tests.

Step 3: a polygon report

Now we use the toolkit. Given the corners of a polygon, a report lists its perimeter, area and the lengths of its sides, and draws it. The shape is an irregular pentagon from the previous practice set.

Python Polygon report with a plot

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 4: extend it

A project is finished only when you have tried to improve it. Here are extension ideas, each using something you have learned:

  1. Add a function that returns whether a point lies inside a circle (distance versus radius).
  2. Add a function for the segment area ½r²(θ - sin θ) and test it against the practice-set answer 16π - 32.
  3. Add line_circle_intersections(m, b, h, k, r) from the equations lesson and test with the tangent case y = 5.
  4. Write a test for each formula that compares it to a second method (for instance, sphere volume against the stacked-disc approximation).
  5. Add a function that rejects invalid inputs for all solids.

A real library would also handle floating-point edge cases, such as a triangle with area almost zero because three points are nearly in a line. Using math.isclose with a tolerance is the first line of defense.

Reflect

When you have finished, ask yourself:

  • Which function had the most mistakes before the tests passed?
  • Did any test fail because the test was wrong rather than the function? That happens often and is a good sign that you are checking carefully.
  • Which formulas feel obvious now that you have built them?

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why do the tests compare numbers with math.isclose instead of ==?

    Floating-point results such as 0.1 + 0.2 may differ from the exact value by a tiny amount, so we compare with a tolerance.
  2. 2. What should circle_area(-3) do?

    A negative radius is invalid input. Raising an error is better than returning a plausible-looking wrong number.
  3. 3. What is the purpose of testing a function against a known answer?

    A test with a known answer, such as a 3-4-5 triangle, tells you quickly whether the function is correct.
  4. 4. The shoelace formula needs the vertices:

    Listing the vertices in order around the boundary (clockwise or counter-clockwise) is required, otherwise the sides cross.

Summary

  • A toolkit is a set of small, named functions, one formula each, with input checks.
  • Test every function against answers you already know, and with a second method when possible.
  • Reports and plots turn numbers into something you can inspect.
  • Extending your own code is the best way to find out what you really understand.

Next: Practice Set: Solids and Coordinates

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.