apopt.comCBE 493R Data-Driven Athletics

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.

  1. 1Gather
  2. 2Statistics (used)
  3. 3Visualize (used)
  4. 4Cleanse
  5. 5Features (used)
  6. 6Balance
  7. 7Scale
  8. 8Split
  9. 9Deploy
  1. Ask
  2. Collect
  3. Check (this week)
  4. Interpret (this week)
  5. Coach
  6. Reassess

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

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).

PythonThe angle function, checked on known cases

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.

PythonKnee angles for every frame

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

PythonKnee flexion through the clip

Show the expected result without running
left knee: 16 swing peaks, median 100 deg; median of the 20% straightest frames 17 deg
Knee flexion in degrees against frame number over 362 frames. The left knee (solid line with dots) rises and falls 16 times, from near 10 degrees to peaks of about 80 to 110 degrees; the right knee (dashed gray line) follows a similar rhythm half a stride later but is more jagged.

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

  1. Hip (0, 0), knee (0, 1), ankle (1, 1) in image units. What is the included knee angle, in degrees?

    degrees

  2. Why does the function clip the cosine to the range −1 to 1?

  3. The knee flexion is 60° in one frame but the knee keypoint had confidence 0.2. What should you report?

0 of 3 answered · 0 right the first time

References

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.