Skip to content
Exit presentation

Programming + AI Capstone

The distance between two

Every later job uses one number. How far apart are two kites? Each kite has two measurements. Span is how wide the kite is. Tail is how long its tail is.

Two points. Kite A3 sits at 9 span, 8 tail. Example R7 sits at 6 span, 12 tail.
Step 1 of 5
  1. Two points. Kite A3 sits at 9 span, 8 tail. Example R7 sits at 6 span, 12 tail.
  2. The gap across is 3.
  3. The gap up is 4.
  4. 3 and 4 make a right triangle. The third side is 5.
  5. Square each gap and add: 9 + 16 = 25. That squared total is enough to compare with.

The rule

A right triangle has two short sides and one long side. Square the two short sides and add them. The total equals the long side squared. This is the Pythagoras theorem. Here the short sides are 3 and 4. So 3 × 3 + 4 × 4 = 9 + 16 = 25. Also 5 × 5 = 25. So the long side, the real distance, is 5.

The answer we use is the total, 25, not 5. A bigger total means farther apart. To square a number is to multiply it by itself. The gap across is the difference in span. The gap up is the difference in tail.

Square the gap across, square the gap up, add them. The sum orders kites exactly as a real distance would. Two kites at the same spot measure zero.

In the lines below, a and b are two kite rows. Position 1 holds the span. Position 2 holds the tail.

  1. let across = a[1] - b[1];
  2. let up = a[2] - b[2];
  3. return across * across + up * up;

A square root undoes a square. The square root of 25 is 5. The program takes no square root. The squared total already puts every pair in order. It also keeps every number whole.

Watch out!

Added without squaring, the gaps can cancel out. −3 + 4 = 1, but that is not a distance. Squaring makes every gap zero or more first.

Move kite A on the board below. Watch the two gaps, their squares and the total.

Loading lab…

Put A on R7. Both gaps are 0, so the total is 0.

Jump to lesson

This track

  1. 0 Start here
  2. 1 The plan, in plain words
  3. 2 Collect the day
  4. 3 The distance between two
  5. 4 See the board
  6. 5 Write the classifier
  7. 6 When the nearest is wrong
  8. 7 Assemble and report
  9. 8 Who it gets wrong
  10. 9 The big picture
  11. 10 Track quiz

Other tracks