-
Notifications
You must be signed in to change notification settings - Fork 16
Types: Add jsdoc and fix notation format. #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nielio
wants to merge
1
commit into
tmpvar:master
Choose a base branch
from
28Apps-Software-GmbH:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,189 @@ | ||
| declare module 'vec2' { | ||
| export default class Vec2 { | ||
| public x: number | ||
| public y: number | ||
| public length: () => number | ||
| public lengthSquared: () => number | ||
| public distance: (vec: Vec2) => number | ||
| public nearest: (vecs: Vec2[]) => Vec2 | ||
| public clone: () => Vec2 | ||
| public set: ( | ||
| ((x: number, y: number) => Vec2) & | ||
| ((vec: Vec2) => Vec2) | ||
| ) | ||
| public zero: () => Vec2 | ||
| public negate: (returnNew?: boolean) => Vec2 | ||
| public normalize: (returnNew?: boolean) => Vec2 | ||
| public add: addSub | ||
| public subtract: addSub | ||
| public multiply: mulDiv | ||
| public divide: mulDiv | ||
| public rotate: (radians: number, inverse?: boolean, returnNew?: boolean) => Vec2 | ||
| public angleTo: (vec: Vec2) => number | ||
| public change: (fn: any) => void | ||
| public ignore: (fn: any) => void | ||
| public equal: ( | ||
| ((x: number, y: number) => Vec2) & | ||
| ((vec: Vec2) => Vec2) & | ||
| ((pos: [number, number]) => Vec2) | ||
| ) | ||
| public abs: (returnNew?: boolean) => Vec2 | ||
| public min: (vec: Vec2) => Vec2 | ||
| public max: (vec: Vec2) => Vec2 | ||
| public clamp: (low: Vec2, high: Vec2, returnNew?: boolean) => Vec2 | ||
| public lerp: (vec: Vec2, amount: number, returnNew?: boolean) => Vec2 | ||
| public skew: (returnNew?: boolean) => Vec2 | ||
| public dot: () => number | ||
| public perpDot: () => number | ||
| public isPointOnLine: (start: {x: number, y: number}, end: {x: number, y: number}) => boolean | ||
| public toArray: () => [number, number] | ||
| public fromArray: (arr: [number, number]) => Vec2 | ||
| public toJson: () => {x: number, y: number} | ||
| public toString: () => string | ||
| constructor(x?: number, y?: number) | ||
| } | ||
|
|
||
| type addSub = ( | ||
| ((x: number, y: number, returnNew?: boolean) => Vec2) & | ||
| ((vec: Vec2, returnNew?: boolean) => Vec2) & | ||
| ((pos: [number, number], returnNew?: boolean) => Vec2) | ||
| ) | ||
|
|
||
| type mulDiv = ( | ||
| (addSub) & | ||
| ((scalar: number, returnNew?: boolean) => Vec2) | ||
| ) | ||
| /** | ||
| * A generic library useful when you need to work with points/vectors in 2d space. | ||
| */ | ||
| export default class Vec2 { | ||
| public readonly x: number; | ||
| public readonly y: number; | ||
|
|
||
| /** | ||
| * Add an observer `fn` that will be called whenever this vector changes. Calling this method without a function causes it to notify observers. | ||
| * `fn` signature: `function(vec, prev) {}` - where `prev` is a clone of the vector before the last operation. | ||
| * this function returns the passed `fn` | ||
| */ | ||
| public change(fn?: (vec: Vec2, prev: Vec2) => any): Vec2; | ||
|
|
||
| /** | ||
| * Pass a `fn` to remove it from the observers list. Calling this function without a `fn` will remove all observers. | ||
| */ | ||
| public ignore(fn: Function): Vec2; | ||
|
|
||
| /** | ||
| * Sets the `x` and `y` coordinates of this vector. If `false` is passed for `notify`, none of the observers will be called. | ||
| */ | ||
| public set(x: number, y: number, notify?: boolean): Vec2; | ||
| public set(vec: Vec2, notify?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Sets the `x` and `y` of this vector to `0` | ||
| */ | ||
| public zero(): Vec2; | ||
|
|
||
| /** | ||
| * Returns a clone of this vector. | ||
| * _Note_: this does not clone observers | ||
| */ | ||
| public clone(): Vec2; | ||
|
|
||
| /** | ||
| * Negate the `x` and `y` coords of this vector. If `returnNew` is truthy, a new vector with the negated coordinates will be returned. | ||
| */ | ||
| public negate(returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Add the `x` and `y` to this vector's coordinates. | ||
| * If `returnNew` is truthy, return a new vector containing the resulting coordinates. Otherwise apply them to this vector and return it. | ||
| */ | ||
| public add(x: number, y: number, returnNew?: boolean): Vec2; | ||
| public add(vec: Vec2, returnNew?: boolean): Vec2; | ||
| public add(pos: [number, number], returnNew?: boolean): Vec2; | ||
|
|
||
| public subtract(x: number, y: number, returnNew?: boolean): Vec2; | ||
| public subtract(vec: Vec2, returnNew?: boolean): Vec2; | ||
| public subtract(pos: [number, number], returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Multiply this vectors components with the incoming, returning a clone if `returnNew` is truthy. | ||
| */ | ||
| public multiply(x: number, y: number, returnNew?: boolean): Vec2; | ||
| public multiply(vec: Vec2, returnNew?: boolean): Vec2; | ||
| public multiply(pos: [number, number], returnNew?: boolean): Vec2; | ||
| public multiply(scalar: number, returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Divide this vectors components by the incoming, returning a clone if `returnNew` is truthy. | ||
| * _note_: this method will throw if you attempt to divide by zero or pass values that cause NaNs | ||
| */ | ||
| public divide(x: number, y: number, returnNew?: boolean): Vec2; | ||
| public divide(vec: Vec2, returnNew?: boolean): Vec2; | ||
| public divide(pos: [number, number], returnNew?: boolean): Vec2; | ||
| public divide(scalar: number, returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Rotate this vector's cordinates around `(0,0)`. If `returnNew` is specified, a new `Vec2` will be created and populated with the result and returned. Otherwise the result is applied to this vector and `this` is returned. | ||
| * @param radians | ||
| * @param inverse inverts the direction of the rotation | ||
| * @param returnNew causes the result to be applied to a new `Vec2`, otherwise the result is applied to `this` | ||
| */ | ||
| public rotate(radians: number, inverse?: boolean, returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Returns the length of this vector from `(0,0)` | ||
| */ | ||
| public length(): number; | ||
|
|
||
| /** | ||
| * Returns the length of this vector prior to the `Math.sqrt` call. | ||
| * This is usefull when you don't need to know the actual distance, but need a normalized value to compare with another `Vec2#lengthSquared` or similar. | ||
| */ | ||
| public lengthSquared(): number; | ||
|
|
||
| /** | ||
| * Distance between this vector and the incoming | ||
| */ | ||
| public distance(vec: Vec2): number; | ||
|
|
||
| /** | ||
| * Closest vector in array to this vector. | ||
| */ | ||
| public nearest(vecs: Vec2[]): Vec2; | ||
|
|
||
| /** | ||
| * Normalizes this vector. If `returnNew` is truthy, a new vector populated with the normalized coordinates will be returned. | ||
| */ | ||
| public normalize(returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * returns true if the incoming coordinates are the same as this vector's | ||
| */ | ||
| public equal(x: number, y: number): boolean; | ||
| public equal(vec: Vec2): boolean; | ||
| public equal(pos: [number, number]): boolean; | ||
|
|
||
| /** | ||
| * Return a `Vec2` that contains the absolute value of each of this vector's parts. | ||
| * @param returnNew If `returnNew` is truthy, create a new `Vec2` and return it. Otherwise apply the absolute values to to `this`. | ||
| */ | ||
| public abs(returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Return a `Vec2` consisting of the smallest values from this vector and the incoming | ||
| * When returnNew is truthy, a new `Vec2` will be returned otherwise the minimum values in either this or `vec` will be applied to this vector. | ||
| */ | ||
| public min(vec: Vec2): Vec2; | ||
|
|
||
| /** | ||
| * Return a `Vec2` consisting of the largest values from this vector and the incoming | ||
| * When returnNew is truthy, a new `Vec2` will be returned otherwise the maximum values in either `this` or `vec` will be applied to this vector. | ||
| */ | ||
| public max(vec: Vec2): Vec2; | ||
|
|
||
| /** | ||
| * Clamp the coordinates of this vector to the high/low of the incoming vec2s. If `returnNew` apply the result to the new vector and return. Otherwise apply to this vector. | ||
| */ | ||
| public clamp(low: Vec2, high: Vec2, returnNew?: boolean): Vec2; | ||
|
|
||
|
|
||
| /** | ||
| * Perform linear interpolation between this vector and the incoming. | ||
| * @param vec | ||
| * @param amount The percentage along the path to place the vector | ||
| * @param returnNew If `truthy`, apply the result to a new vector and return it, otherwise return `this` | ||
| */ | ||
| public lerp(vec: Vec2, amount: number, returnNew?: boolean): Vec2; | ||
|
|
||
| /** | ||
| * Returns a vector set with the `(-y,x)` coordinates of this vector. | ||
| * @param returnNew A new vector is created and the operation is applied to the new vector. | ||
| */ | ||
| public skew(returnNew?: boolean): Vec2; | ||
|
|
||
| public dot(): number; | ||
|
|
||
| public perpDot(): number; | ||
|
|
||
| /** | ||
| * returns the angle from this vector to the incoming. | ||
| */ | ||
| public angleTo(vec: Vec2): number; | ||
|
|
||
| /** | ||
| * where `start` and `end` are vec2-like (e.g. `start.x` and `start.y`) | ||
| */ | ||
| public isPointOnLine(start: { x: number, y: number }, end: { x: number, y: number }): boolean; | ||
|
|
||
| /** | ||
| * @return `[x, y]` | ||
| */ | ||
| public toArray(): [number, number]; | ||
|
|
||
| /** | ||
| * Applies the `[0]` to `this.x` and `[1]` to `this.y` | ||
| */ | ||
| public fromArray(arr: [number, number]): Vec2; | ||
|
|
||
| /** | ||
| * @return `{ x: ..., y: ...}` | ||
| */ | ||
| public toJson(): { x: number, y: number }; | ||
|
|
||
| /** | ||
| * @return `'(x, y)'` | ||
| */ | ||
| public toString(): string; | ||
|
|
||
| constructor(x?: number, y?: number); | ||
| constructor(xy: number[]); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, remove the
readonlyfromxandy? It produces lint errors when I just want to change a value fast without extra function callThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should stay
readonly.There are no setters implemented. The values are just simple attributes.
If you set the values directly, it would be like calling the
set()function withnotify = false.From the docs:
Also, the internal
clean()function would not be called. It validates the values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Forgot the observers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem 😅