diff --git a/lib/src/models/ellipse.ts b/lib/src/models/ellipse.ts new file mode 100644 index 0000000..5324ec3 --- /dev/null +++ b/lib/src/models/ellipse.ts @@ -0,0 +1,22 @@ +import { IPoint } from "../interfaces"; +import { Point } from "./point"; + +export class Ellipse { + center = new Point(); + radiusX = 0; + radiusY = 0; + + get width() { + return this.radiusX * 2; + } + + get height() { + return this.radiusY * 2; + } + + constructor(center: IPoint, radiusX: number, radiusY: number) { + this.center = new Point(center.x, center.y); + this.radiusX = radiusX; + this.radiusY = radiusY; + } +} diff --git a/lib/src/models/index.ts b/lib/src/models/index.ts index 6e5171c..a9c6a7c 100644 --- a/lib/src/models/index.ts +++ b/lib/src/models/index.ts @@ -1,3 +1,6 @@ export * from "./margin"; export * from "./point"; export * from "./size"; +export * from "./ellipse"; +export * from "./rectangle"; +export * from "./transform"; diff --git a/lib/src/models/rectangle.ts b/lib/src/models/rectangle.ts new file mode 100644 index 0000000..8151356 --- /dev/null +++ b/lib/src/models/rectangle.ts @@ -0,0 +1,84 @@ +import { Point } from "./point"; +import { Size } from "./size"; + +export class Rectangle { + public x = 0; + public y = 0; + public width = 0; + public height = 0; + + get left() { + return this.x; + } + + get right() { + return this.x + this.width; + } + + get top() { + return this.y; + } + + get bottom() { + return this.y + this.height; + } + + get center() { + return new Point(this.left + this.width / 2, this.top + this.height / 2); + } + + get position() { + return new Point(this.x, this.y); + } + + get size() { + return new Size(this.width, this.height); + } + + constructor(x = 0, y = 0, width = 0, height = 0) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + isEmpty() { + return this.width <= 0 || this.height <= 0; + } + + containsPoint(point: Point) { + return point.x >= this.left && point.x <= this.right && point.y >= this.top && point.y <= this.bottom; + } + + containsRectangle() { + throw new Error("Not implemented!"); + } + + copy() { + throw new Error("Not implemented!"); + } + + static fromPoints(left: number, top: number, right: number, bottom: number) { + return new Rectangle(left, top, right - left, bottom - top); + } + + static union(rectangles: Rectangle[]) { + if (rectangles.length === 0) { + return new Rectangle(0, 0, 0, 0); + } + if (rectangles.length === 1) { + return rectangles[0]; + } + + return rectangles.reduce( + (prev, current) => { + const left = prev.x < current.x ? prev.x : current.x; + const top = prev.y < current.y ? prev.y : current.y; + const right = prev.right > current.right ? prev.right : current.right; + const bottom = prev.bottom > current.bottom ? prev.bottom : current.bottom; + return Rectangle.fromPoints(left, top, right, bottom); + }, + new Rectangle(0, 0, 0, 0), + ); + } +} diff --git a/lib/src/models/transform.ts b/lib/src/models/transform.ts new file mode 100644 index 0000000..38a909e --- /dev/null +++ b/lib/src/models/transform.ts @@ -0,0 +1,12 @@ +export class Transform { + scaleX = 1; + scaleY = 1; + + translateY = 0; + translateX = 0; + + skewY = 0; + skewX = 0; + + rotate = 0; +} diff --git a/lib/src/package.json b/lib/src/package.json index 50fd85b..d8960e4 100644 --- a/lib/src/package.json +++ b/lib/src/package.json @@ -1,6 +1,6 @@ { "name": "@gxc-solutions/math", - "version": "0.0.1", + "version": "0.0.2", "main": "index.js", "author": "GXC Solutions", "publishConfig": {