Skip to content

Commit

Permalink
remove instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danehansen committed Mar 16, 2021
1 parent 6f2e3d9 commit 284b3a3
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 457 deletions.
96 changes: 35 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Point ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@danehansen/point.svg) ![npm](https://img.shields.io/npm/dt/@danehansen/point.svg)
# point ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@danehansen/point.svg) ![npm](https://img.shields.io/npm/dt/@danehansen/point.svg)

**Class** : public class [Point](https://github.com/danehansen/Point)
**Inheritance** : [Point](https://github.com/danehansen/Point) > Object

The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis. Mostly based on the AS3 Point class, but with some added features/useless ones removed.
The point library contains a collection of functions for dealing with points with numerical x and y values.

## Installation

Expand All @@ -13,68 +10,45 @@ The Point object represents a location in a two-dimensional coordinate system, w

As a module:

import Point from '@danehansen/point';
import * as point from '@danehansen/point';

var p = new Point(50, 75);
p.setTo(25, 75);
var d = Point.distace(p, new Point(5, 20));
var p1 = {x: 50, y: 75};
var p2 = point.add(p1, {x: 5, y: 5});
var d = point.distance(p1, p2);

In your browser:

<script src='danehansen-Point.min.js'></script>
<script src='danehansen-point.min.js'></script>
<script>
var Point = window.danehansen.Point;
var p = new Point(3, 4);
p.add(new Point(5, 6));
var point = window.danehansen.point;
var p1 = {x: 50, y: 75};
var p2 = point.add(p1, {x: 5, y: 5});
var d = point.distance(p1, p2);
</script>

## Public Static Methods

- **distance**(pt1:Point, pt2:Point):Number
[static] Returns the distance between pt1 and pt2.
- **interpolate**(pt1:Point, pt2:Point, f:Number):Point
[static] Determines a point between two specified points.
- **intersection**(aStart:Point, aEnd:Point, bStart:Point, bEnd:Point):Point
[static] Returns a point where to traveling points intersect, or null if never.
- **polar**(len:Number, angle:Number):Point
[static] Converts a pair of polar coordinates to a Cartesian point coordinate.
- **randomPointInCircle**(center:Point, radius:Number):Point
[static] Returns a random point within a given circle.
- **round**(v:Point, increment:Number = 1):Point
[static] Returns a new point with its x and y values rounded to the nearest increment.

## Public Properties

- **length**() : Number
[read-only] Gets the length of the line segment from (0,0) to this point.
- **x** : Number
The horizontal coordinate of the point.
- **y** : Number
The vertical coordinate of the point.

## Public Methods

- **Point**(x:Number = 0, y:Number = 0)
Creates a new point.
- **add**(v:Point):Point
Adds the coordinates of another point to the coordinates of this point to create a new point.
- **angle**():Number
Returns the angle in radians of this point from (0,0).
- **clone**():Point
Creates a copy of this Point object.
- **copyFrom**(sourcePoint:Point)
Copies all of the point data from the source Point object into the calling Point object.
- **equals**(toCompare:Point):Boolean
## Methods

- **add**(pt1:Object, pt2:Object):Object
Returns a new point equal to sum of two points.
- **angle**(point:Object):Number
Returns the angle in radians of a point from (0,0).
- **distance**(pt1:Object, pt2:Object):Number
Returns the distance between pt1 and pt2.
- **interpolate**(pt1:Object, pt2:Object, f:Number):Object
Determines a point between two specified points.
- **intersection**(aStart:Object, aEnd:Object, bStart:Object, bEnd:Object):Object
Returns a point where to traveling points intersect, or null if never.
- **isEqual**(pt1:Object, pt2:Object):Boolean
Determines whether two points are equal.
- **normalize**(thicknes:Number)
Scales the line segment between (0,0) and the current point to a set length.
- **offset**(dx:Number, dy:Number)
Offsets the Point object by the specified amount.
- **rotate**(angle:Number, center:Point = new Point())
Rotates the Point object around a center point by the specified angle.
- **setTo**(xa:Number, ya:Number)
Sets the members of Point to the specified values
- **subtract**(v:Point):Boolean
Subtracts the coordinates of another point from the coordinates of this point to create a new point.
- **toString**():String
- **normalize**(point:Object, thicknes:Number)
Returns a new point scaled from the line segment between (0,0) and a point to a set length.
- **polar**(len:Number, angle:Number):Object
Converts a pair of polar coordinates to a Cartesian point coordinate.
- **randomPointInCircle**(center:Object, radius:Number):Object
Returns a random point within a given circle.
- **rotate**(point:Object, angle:Number, center:Object = {x: 0, y: 0})
Returns a new point rotated around a center point by the specified angle.
- **round**(v:Object, increment:Number = 1):Object
Returns a new point with its x and y values rounded to the nearest increment.
- **toString**(point:Object):String
Returns a string that contains the values of the x and y coordinates.
199 changes: 198 additions & 1 deletion danehansen-Point.min.js
Original file line number Diff line number Diff line change
@@ -1 +1,198 @@
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@danehansen/math")):"function"==typeof define&&define.amd?define(["@danehansen/math"],t):"object"==typeof exports?exports.Point=t(require("@danehansen/math")):(n.danehansen=n.danehansen||{},n.danehansen.Point=t(n.danehansen.math))}(this,function(n){return function(n){function t(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var e={};return t.m=n,t.c=e,t.i=function(n){return n},t.d=function(n,e,r){t.o(n,e)||Object.defineProperty(n,e,{configurable:!1,enumerable:!0,get:r})},t.n=function(n){var e=n&&n.__esModule?function(){return n.default}:function(){return n};return t.d(e,"a",e),e},t.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},t.p="",t(t.s=1)}([function(t,e){t.exports=n},function(n,t,e){"use strict";function r(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")}var o=function(){function n(n,t){for(var e=0;e<t.length;e++){var r=t[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(n,r.key,r)}}return function(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),t}}(),i=e(0),u={x:0,y:0},a=function(){function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;r(this,n),c.call(this),this.x=t,this.y=e}return o(n,null,[{key:"distance",value:function(n,t){return Math.sqrt(Math.pow(n.x-t.x,2)+Math.pow(n.y-t.y,2))}},{key:"interpolate",value:function(t,e,r){return new n(t.x+(e.x-t.x)*r,t.y+(e.y-t.y)*r)}},{key:"intersection",value:function(t,e,r,o){var i=t.x,u=t.y,a=e.x,c=e.y,s=r.x,f=r.y,h=o.x,y=o.y,l=i-a,x=f-y,d=u-c,p=s-h,v=l*x-d*p;if(0===v)return null;var m=i*c-u*a,b=s*y-f*h;return new n((m*p-l*b)/v,(m*x-d*b)/v)}},{key:"polar",value:function(t,e){return new n(Math.cos(e)*t,Math.sin(e)*t)}},{key:"randomPointInCircle",value:function(t,e){var r={};do{r.x=Math.random()*e*2+t.x-e,r.y=Math.random()*e*2+t.y-e}while(n.distance(r,t)>e);return r}},{key:"round",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1;return new n((0,i.round)(t.x,e),(0,i.round)(t.y,e))}}]),o(n,[{key:"rotate",value:function(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:u,e=Math.sin(n),r=Math.cos(n),o=this.x,i=this.y,a=t.x,c=t.y;o-=a,i-=c,this.x=o*r-i*e+a,this.y=o*e+i*r+c}},{key:"length",get:function(){return n.distance(this,u)}}]),n}(),c=function(){var n=this;this.add=function(t){n.offset(t.x,t.y)},this.angle=function(){return Math.atan2(n.y,n.x)},this.clone=function(){return new a(n.x,n.y)},this.copyFrom=function(t){n.setTo(t.x,t.y)},this.equals=function(t){return n.x===t.x&&n.y===t.y},this.normalize=function(t){var e=t/n.length;n.x*=e,n.y*=e},this.offset=function(t,e){n.x+=t,n.y+=e},this.setTo=function(t,e){n.x=t,n.y=e},this.subtract=function(t){n.x-=t.x,n.y-=t.y},this.toString=function(){return"{x: "+n.x+", y: "+n.y+"}"}};n.exports=a}])});
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("@danehansen/math"));
else if(typeof define === 'function' && define.amd)
define(["@danehansen/math"], factory);
else if(typeof exports === 'object')
exports["point"] = factory(require("@danehansen/math"));
else
root["danehansen"] = root["danehansen"] || {}, root["danehansen"]["point"] = factory(root["danehansen"]["math"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {

module.exports = __WEBPACK_EXTERNAL_MODULE_0__;

/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


Object.defineProperty(exports, "__esModule", {
value: true
});
exports.add = add;
exports.angle = angle;
exports.distance = distance;
exports.interpolate = interpolate;
exports.intersection = intersection;
exports.length = length;
exports.normalize = normalize;
exports.polar = polar;
exports.randomPointInCircle = randomPointInCircle;
exports.rotate = rotate;
exports.round = round;
exports.toString = toString;

var _math = __webpack_require__(0);

function add(a, b) {
return { x: a.x + b.x, y: a.y + b.y };
}

function angle(point) {
return Math.atan2(point.y, point.x);
}

function distance(a, b) {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}

function interpolate(start, end, amount) {
return { x: start.x + (end.x - start.x) * amount, y: start.y + (end.y - start.y) * amount };
}

function intersection(startA, endA, startB, endB) {
var x1 = startA.x;
var y1 = startA.y;
var x2 = endA.x;
var y2 = endA.y;
var x3 = startB.x;
var y3 = startB.y;
var x4 = endB.x;
var y4 = endB.y;
var a = x1 - x2;
var b = y3 - y4;
var c = y1 - y2;
var d = x3 - x4;
var e = a * b - c * d;
if (e === 0) {
return null;
}
var f = x1 * y2 - y1 * x2;
var g = x3 * y4 - y3 * x4;
return { x: (f * d - a * g) / e, y: (f * b - c * g) / e };
}

function length(point) {
return distance(point, { x: 0, y: 0 });
}

function normalize(point, thickness) {
var l = length(point);
var ratio = thickness / l;
return { x: point.x * ratio, y: point.y * ratio };
}

function polar(len, angle) {
return { x: Math.cos(angle) * len, y: Math.sin(angle) * len };
}

function randomPointInCircle(center, radius) {
var random = {};
do {
random.x = Math.random() * radius * 2 + center.x - radius;
random.y = Math.random() * radius * 2 + center.y - radius;
} while (distance(random, center) > radius);
return random;
}

function rotate(point, angle) {
var center = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { x: 0, y: 0 };

var sin = Math.sin(angle);
var cos = Math.cos(angle);
var x = point.x,
y = point.y;

var centerX = center.x;
var centerY = center.y;
x -= centerX;
y -= centerY;
return { x: x * cos - y * sin + centerX, y: x * sin + y * cos + centerY };
}

function round(point) {
var increment = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;

return { x: (0, _math.round)(point.x, increment), y: (0, _math.round)(point.y, increment) };
}

function toString(point) {
return '{x: ' + point.x + ', y: ' + point.y + '}';
}

/***/ })
/******/ ]);
});
Loading

0 comments on commit 284b3a3

Please sign in to comment.