Course / Tutorials / Week 3 / Q-7
Quick tutorial Q-7 · Week 3 · about 15 min · Foundation
The angle from three points
Write the angle-from-three-points function, check it on the book's worked example, and apply it to every frame of a pose table to draw a runner's knee flexion stride by stride.
- 1Gather
- 2Statistics (used)
- 3Visualize (used)
- 4Cleanse
- 5Features (used)
- 6Balance
- 7Scale
- 8Split
- 9Deploy
- Ask
- Collect
- Check (this week)
- Interpret (this week)
- Coach
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 3 (sections 3.2, 3.9)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser.
The decision
A coach asks how far a runner's knee bends in the swing. The answer is an angle from three keypoints in each frame, and it is only as good as the three points and the camera's view.
The raw data
raw/form2_keypoints_yolo26s.csv: a pose model's keypoint table for a side-view running clip, one row per frame (362 rows), x and y in pixels and a confidence for 17 keypoints. Made for this tutorial with the YOLO26s pose model from the course's example clipform2_60fps.mp4(header 30 frames per second). The clip shows a person and is not published; the table shows no one.
Try it
Three points make an angle
A joint angle needs three keypoints: one on each segment and one at the joint. For the knee, take the hip A, the knee B and the ankle C. The two vectors that leave the knee, u = A − B and w = C − B, meet at the included angle θ, and the dot product gives it (Section 3.2.2):
θ = arccos( u·w / (|u| |w|) )
A straight leg is 180°. Clinicians often report flexion, 180° − θ, which is 0° for a straight leg. Check the function on the book's example first: in one frame of Jordan's jump the hip is at (0.50, 0.40), the knee at (0.55, 0.60) and the ankle at (0.48, 0.80).
Type a prediction to enable Run.
Show the expected result without running
Jordan's knee: included 146.7 deg, flexion 33.3 deg straight leg 180.0 deg; right angle 90.0 deg same points in pixels 146.7 deg
146.7°, a flexion of 33.3°, as in Example "Jordan's knee angle by hand". The units of the points do not matter, because the lengths divide out: the same frame in pixels gives the same angle.
The same function on every frame of a clip
The file is a pose model's table for a running clip from the course's example set, form2_60fps.mp4, filmed from the side: one row per frame, x and y in pixels for 17 keypoints, and a confidence for each. The video is not published; the table shows no one. The function works on whole columns at once.
Type a prediction to enable Run.
Show the expected result without running
l knee flexion: 5 to 112 deg; 0 of 362 frames with a point below 0.5 confidence r knee flexion: 3 to 100 deg; 0 of 362 frames with a point below 0.5 confidence
One chart
Show the expected result without running
left knee: 16 swing peaks, median 100 deg; median of the 20% straightest frames 17 deg

The left knee bends and straightens once a stride, 16 times in the clip: nearly straight (10 to 20°) around foot contact, folded to about 100° in the swing, when the foot comes up behind the body. The right knee follows half a stride later, but its trace is more jagged. In a side view one leg is always partly behind the other, and the model sometimes moves a point or swaps the two legs' labels for a frame. And every angle here is measured in the image plane: if the leg is not square to the camera, the true knee angle differs (Section 3.9.1).
For the athlete: "From the side, your knee folds to about 100 degrees in each swing and is nearly straight when your foot lands; to compare sessions we'll film from the same side, square to your path, every time."
With your own data
Run a pose model on a side-view clip of your own (CS-10 shows how), or use the canvas app's angle(left_hip, left_knee, left_ankle) on one frame, and check the app's number against this function on the same three points.
Check yourself
Hip (0, 0), knee (0, 1), ankle (1, 1) in image units. What is the included knee angle, in degrees?
Why does the function clip the cosine to the range −1 to 1?
The knee flexion is 60° in one frame but the knee keypoint had confidence 0.2. What should you report?
References
- Data-Driven Athletics, Chapter 3, The angle from three points (Section 3.2.2) and Image-plane angles are not joint angles (Section 3.9.1).
Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2, scipy 1.18.1. In the browser the same code runs on Pyodide, so the last digit can differ.