A free learning activity with a PDF worksheet that guides learners to discover and formulate Viviani's theorem, and to explain why (prove) that it is true, together with the further explorations below, is given in my book Rethinking Proof with Sketchpad (which is now completely free to download at the preceding link).
"The process of generalization, instead of leading from elements to classes, leads from classes to classes ... we shall regard abstraction as class formation, and generalization as class extension ...." - Zoltan Dienes (1961, pp. 282; 296). On Abstraction and Generalization. Harvard Educational Review, 31(3), pp. 281-301.
2D Generalizations of Viviani's Theorem
Investigate
1) Drag point P or any of the red vertices to explore the results.
2) Use the LINK buttons in the sketch above to move to a) a hexagon with opposite sides parallel and b) an equi-angular pentagon.
3) Click on the Show Hint button in the equi-angular pentagon page for a hint to constructing a logical
explanation (proof) for the above result, or if you're really stuck, go to my 2005 paper in Mathematics in School at: Crocodiles
and Polygons.
4) Note that the results hold even when P is outside the polygon, or outside a pair of parallel lines, provided we regard distances respectively falling completely outside the polygon or outside the parallel lines as negative; in other words using directed distances (or equivalently, vectors). However, Sketchpad does not measure 'negative' distances, so dragging P outside will appear to no longer give a constant sum.
Further Generalizations with Equi-inclined lines
Viviani's theorem can be even further generalized by constructing lines to the sides of the above sets of polygons so that they form equal angles with the sides as shown with a dynamic sketch at Further generalizations of Viviani's theorem.
The theorem also generalizes to 3D as shown at 3D Generalizations of Viviani's Theorem.
A Variation on Viviani: Clough's Theorem
An interesting variation of Viviani's theorem was experimentally discovered by a Bishops Diocesan College schoolboy, Clough, in 2003 and is available at: Clough's Theorem and some generalizations.
Directed Distances
As mentioned in 4) above, Viviani's theorem and its various generalizations provide a good context for introducing talented students to directed distances when considering the case when the point P moves outside the polygon. For example, read my paper The Value of using Signed Quantities in Geometry in Learning & Teaching Mathematics, Dec 2020.