Files
esengine/packages/editor-app/src/domain/value-objects/Position.ts

73 lines
1.5 KiB
TypeScript
Raw Normal View History

/**
*
*
*/
export class Position {
private readonly _x: number;
private readonly _y: number;
constructor(x: number, y: number) {
this._x = x;
this._y = y;
}
get x(): number {
return this._x;
}
get y(): number {
return this._y;
}
/**
*
*/
add(offset: Position): Position {
return new Position(this._x + offset._x, this._y + offset._y);
}
/**
*
*/
subtract(other: Position): Position {
return new Position(this._x - other._x, this._y - other._y);
}
/**
*
*/
distanceTo(other: Position): number {
const dx = this._x - other._x;
const dy = this._y - other._y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
*
*/
equals(other: Position): boolean {
return this._x === other._x && this._y === other._y;
}
/**
*
*/
toObject(): { x: number; y: number } {
return { x: this._x, y: this._y };
}
/**
*
*/
static fromObject(obj: { x: number; y: number }): Position {
return new Position(obj.x, obj.y);
}
/**
*
*/
static zero(): Position {
return new Position(0, 0);
}
}