generated from gxc-solutions/gxc-template-repo
Added rectangle/ellipse/transform classes
This commit is contained in:
parent
6ce321c051
commit
e5f5f3a08e
5 changed files with 122 additions and 1 deletions
22
lib/src/models/ellipse.ts
Normal file
22
lib/src/models/ellipse.ts
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
export * from "./margin";
|
export * from "./margin";
|
||||||
export * from "./point";
|
export * from "./point";
|
||||||
export * from "./size";
|
export * from "./size";
|
||||||
|
export * from "./ellipse";
|
||||||
|
export * from "./rectangle";
|
||||||
|
export * from "./transform";
|
||||||
|
|
|
||||||
84
lib/src/models/rectangle.ts
Normal file
84
lib/src/models/rectangle.ts
Normal file
|
|
@ -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),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
lib/src/models/transform.ts
Normal file
12
lib/src/models/transform.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
export class Transform {
|
||||||
|
scaleX = 1;
|
||||||
|
scaleY = 1;
|
||||||
|
|
||||||
|
translateY = 0;
|
||||||
|
translateX = 0;
|
||||||
|
|
||||||
|
skewY = 0;
|
||||||
|
skewX = 0;
|
||||||
|
|
||||||
|
rotate = 0;
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@gxc-solutions/math",
|
"name": "@gxc-solutions/math",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "GXC Solutions",
|
"author": "GXC Solutions",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue