• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Math Related / Calculate the midpoint between two pairs of (x,y) points

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

Calculate 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];

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

Source

https://www.30secondsofcode.org/js/s/midpoint

Math Related

Related Snippets:

  • Metric BMI Calculator
  • Convert a string into an integer (a whole number)
  • Return the largest integer less than or equal to a number
  • Get the largest number from a set of numbers

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us