Midpoint

JavaScript, Math · Oct 21, 2020

Calculates the midpoint between two pairs of (x,y) points.

  • Destructure the array to get x1, y1, x2 and y2.
  • Calculate the midpoint for each dimension by dividing the sum of the two endpoints by 2.
const midpoint = ([x1, y1], [x2, y2]) => [(x1 + x2) / 2, (y1 + y2) / 2];

midpoint([2, 2], [4, 4]); // [3, 3]
midpoint([4, 4], [6, 6]); // [5, 5]
midpoint([1, 3], [2, 4]); // [1.5, 3.5]

More like this

  • JavaScript Geometric Operations

    Learn how to perform some basic geometric operations in JavaScript with this collection of snippets.

    Collection · 4 snippets

  • Euclidean distance

    Calculates the distance between two points in any number of dimensions.

    JavaScript, Math · Dec 28, 2020

  • Distance between two points

    Calculates the distance between two points.

    JavaScript, Math · Dec 28, 2020

  • Greatest common divisor

    Calculates the greatest common divisor between two or more numbers/arrays.

    JavaScript, Math · Dec 29, 2020