UNPKG

cornerstone-math

Version:

Math and computation geometry functionality for cornerstone

143 lines (105 loc) 3.15 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>point.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Line3.html">Line3</a></li><li><a href="Plane.html">Plane</a></li></ul><h3>Global</h3><ul><li><a href="global.html#approximatelyEquals">approximatelyEquals</a></li><li><a href="global.html#findClosestPoint">findClosestPoint</a></li></ul> </nav> <div id="main"> <h1 class="page-title">point.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>function pageToPoint (e) { return { x: e.pageX, y: e.pageY }; } function subtract (lhs, rhs) { return { x: lhs.x - rhs.x, y: lhs.y - rhs.y }; } function copy (point) { return { x: point.x, y: point.y }; } function distance (from, to) { return Math.sqrt(distanceSquared(from, to)); } function distanceSquared (from, to) { const delta = subtract(from, to); return delta.x * delta.x + delta.y * delta.y; } function insideRect (point, rect) { if(point.x &lt; rect.left || point.x > rect.left + rect.width || point.y &lt; rect.top || point.y > rect.top + rect.height) { return false; } return true; } /** * Returns the closest source point to a target point * given an array of source points. * * @param sources An Array of source Points * @param target The target Point * @returns Point The closest point from the points array */ function findClosestPoint (sources, target) { const distances = []; let minDistance; sources.forEach(function (source, index) { const d = distance(source, target); distances.push(d); if (index === 0) { minDistance = d; } else { minDistance = Math.min(d, minDistance); } }); const index = distances.indexOf(minDistance); return sources[index]; } const point = { subtract, copy, pageToPoint, distance, distanceSquared, insideRect, findClosestPoint }; export default point; </code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.6</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/linenumber.js"></script> </body> </html>