Skip to content

Instantly share code, notes, and snippets.

@alexsanqp
Last active April 24, 2019 17:28
Show Gist options
  • Save alexsanqp/0dc95a4c1df6e64bc775aadf476bcb76 to your computer and use it in GitHub Desktop.
Save alexsanqp/0dc95a4c1df6e64bc775aadf476bcb76 to your computer and use it in GitHub Desktop.
A point representing a location in (x,y) coordinate space, specified in integer precision.
/**
* A point representing a location in (x,y) coordinate space, specified in integer precision.
*/
export class Point {
/**
* The X coordinate of this Point.
* @default 0
*/
protected x: number;
/**
* The Y coordinate of this Point.
* @default 0
*/
protected y: number;
public constructor();
public constructor(point: Point);
public constructor(position: [number, number]);
public constructor(x: number, y: number);
public constructor(paramX?: any, paramY?: any) {
this.setLocation(paramX, paramY);
}
/**
* Changes the point to have the specified location.
*/
public setLocation(): void;
public setLocation(point: Point): void;
public setLocation(position: [number, number]): void;
public setLocation(x: number, y: number): void;
public setLocation(param?: any, param1?: any): void {
if (typeof param === 'undefined') {
this.x = 0;
this.y = 0;
} else if (param && param instanceof Point) {
this.x = param.getX();
this.y = param.getY();
} else if (Array.isArray(param)) {
this.x = param[0];
this.y = param[1];
} else {
this.x = param;
this.y = param1;
}
}
/**
* Moves this point to the specified location in the (x,y) coordinate plane.
* This method is identical with setLocation(int, int)
*
* @link setLocation
*
* @param {number} x
* @param {number} y
*/
public move(x: number, y: number): void {
this.setLocation(x, y);
}
/**
* Translates this point, at location (x,y),
* by dx along the x axis and dy along the y axis so that it now represents the point (x+dx,y+dy).
*
* @param {number} dx - the distance to move this point along the X axis
* @param {number} dy - the distance to move this point along the Y axis
*/
public translate(dx: number, dy: number): void {
const x: number = this.x + dx;
const y: number = this.y + dy;
this.setLocation(x, y);
}
/**
* Returns the X coordinate of this Point
*
* @return {number}
*/
public getX(): number {
return this.x;
}
/**
* Set the X coordinate of this Point
*
* @param {number} x
*/
public setX(x: number): void {
this.x = x;
}
/**
* Returns the Y coordinate of this Point
*
* @return {number}
*/
public getY(): number {
return this.y;
}
/**
* Set the Y coordinate of this Point
*
* @param {number} y
*/
public setY(y: number): void {
this.y = y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment