123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const __1 = require("..");
- describe('rectangle', () => {
- describe('#constructor', () => {
- it('should create a rectangle instance', () => {
- expect(new __1.Rectangle()).toBeInstanceOf(__1.Rectangle);
- expect(new __1.Rectangle(1)).toBeInstanceOf(__1.Rectangle);
- expect(new __1.Rectangle(1, 2)).toBeInstanceOf(__1.Rectangle);
- expect(new __1.Rectangle(1, 2).x).toEqual(1);
- expect(new __1.Rectangle(1, 2).y).toEqual(2);
- expect(new __1.Rectangle(1, 2).width).toEqual(0);
- expect(new __1.Rectangle(1, 2).height).toEqual(0);
- expect(new __1.Rectangle().equals(new __1.Rectangle(0, 0, 0, 0)));
- });
- it('should work with key points', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.origin.equals({ x: 1, y: 2 })).toBeTruthy();
- expect(rect.topLeft.equals({ x: 1, y: 2 })).toBeTruthy();
- expect(rect.topCenter.equals({ x: 2.5, y: 2 })).toBeTruthy();
- expect(rect.topRight.equals({ x: 4, y: 2 })).toBeTruthy();
- expect(rect.center.equals({ x: 2.5, y: 4 })).toBeTruthy();
- expect(rect.bottomLeft.equals({ x: 1, y: 6 })).toBeTruthy();
- expect(rect.bottomCenter.equals({ x: 2.5, y: 6 })).toBeTruthy();
- expect(rect.bottomRight.equals({ x: 4, y: 6 })).toBeTruthy();
- expect(rect.corner.equals({ x: 4, y: 6 })).toBeTruthy();
- expect(rect.leftMiddle.equals({ x: 1, y: 4 })).toBeTruthy();
- expect(rect.rightMiddle.equals({ x: 4, y: 4 })).toBeTruthy();
- });
- it('should return the key points', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.getOrigin().equals({ x: 1, y: 2 })).toBeTruthy();
- expect(rect.getTopLeft().equals({ x: 1, y: 2 })).toBeTruthy();
- expect(rect.getTopCenter().equals({ x: 2.5, y: 2 })).toBeTruthy();
- expect(rect.getTopRight().equals({ x: 4, y: 2 })).toBeTruthy();
- expect(rect.getCenter().equals({ x: 2.5, y: 4 })).toBeTruthy();
- expect(rect.getBottomLeft().equals({ x: 1, y: 6 })).toBeTruthy();
- expect(rect.getBottomCenter().equals({ x: 2.5, y: 6 })).toBeTruthy();
- expect(rect.getBottomRight().equals({ x: 4, y: 6 })).toBeTruthy();
- expect(rect.getCorner().equals({ x: 4, y: 6 })).toBeTruthy();
- expect(rect.getLeftMiddle().equals({ x: 1, y: 4 })).toBeTruthy();
- expect(rect.getRightMiddle().equals({ x: 4, y: 4 })).toBeTruthy();
- expect(rect.getCenterX()).toEqual(2.5);
- expect(rect.getCenterY()).toEqual(4);
- });
- it('should work with key lines', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.topLine.equals(new __1.Line(1, 2, 4, 2)));
- expect(rect.rightLine.equals(new __1.Line(4, 2, 4, 6)));
- expect(rect.bottomLine.equals(new __1.Line(1, 6, 4, 6)));
- expect(rect.leftLine.equals(new __1.Line(1, 2, 1, 6)));
- });
- it('should return the key lines', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.getTopLine().equals(new __1.Line(1, 2, 4, 2)));
- expect(rect.getRightLine().equals(new __1.Line(4, 2, 4, 6)));
- expect(rect.getBottomLine().equals(new __1.Line(1, 6, 4, 6)));
- expect(rect.getLeftLine().equals(new __1.Line(1, 2, 1, 6)));
- });
- });
- describe('#Rectangle.clone', () => {
- it('should clone rectangle', () => {
- const obj = { x: 1, y: 2, width: 3, height: 4 };
- expect(__1.Rectangle.clone(new __1.Rectangle(1, 2, 3, 4)).toJSON()).toEqual(obj);
- expect(__1.Rectangle.clone(obj).toJSON()).toEqual(obj);
- expect(__1.Rectangle.clone([1, 2, 3, 4]).toJSON()).toEqual(obj);
- });
- });
- describe('#Rectangle.isRectangleLike', () => {
- it('should return true if the given object is a rectangle-like object', () => {
- const obj = { x: 1, y: 2, width: 3, height: 4 };
- expect(__1.Rectangle.isRectangleLike(obj)).toBeTruthy();
- expect(__1.Rectangle.isRectangleLike(Object.assign(Object.assign({}, obj), { z: 10 }))).toBeTruthy();
- expect(__1.Rectangle.isRectangleLike(Object.assign(Object.assign({}, obj), { z: 10, s: 's' }))).toBeTruthy();
- });
- it('should return false if the given object is a rectangle-like object', () => {
- expect(__1.Rectangle.isRectangleLike({ x: 1 })).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike({ y: 2 })).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike({})).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike(null)).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike(false)).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike(1)).toBeFalsy();
- expect(__1.Rectangle.isRectangleLike('s')).toBeFalsy();
- });
- });
- describe('#Rectangle.fromSize', () => {
- it('should create a rectangle from the given size', () => {
- expect(__1.Rectangle.fromSize({ width: 10, height: 8 }).toJSON()).toEqual({
- x: 0,
- y: 0,
- width: 10,
- height: 8,
- });
- });
- });
- describe('#Rectangle.fromPositionAndSize', () => {
- it('should create a rectangle from the given position and size', () => {
- expect(__1.Rectangle.fromPositionAndSize({ x: 2, y: 5 }, { width: 10, height: 8 }).toJSON()).toEqual({
- x: 2,
- y: 5,
- width: 10,
- height: 8,
- });
- });
- });
- describe('#Rectangle.fromEllipse', () => {
- it('should create a rectangle from the given ellipse', () => {
- expect(__1.Rectangle.fromEllipse(new __1.Ellipse(1, 2, 3, 4)).toJSON()).toEqual({
- x: -2,
- y: -2,
- width: 6,
- height: 8,
- });
- });
- });
- describe('#left,top,right,bottom', () => {
- it('should return properties of rectangle', () => {
- const obj = { x: 1, y: 2, width: 3, height: 4 };
- const rect = __1.Rectangle.create(obj);
- expect(rect.left).toEqual(obj.x);
- expect(rect.top).toEqual(obj.y);
- expect(rect.right).toEqual(obj.x + obj.width);
- expect(rect.bottom).toEqual(obj.y + obj.height);
- });
- });
- describe('#valueOf', () => {
- it('should return JSON object', () => {
- const obj = { x: 1, y: 2, width: 3, height: 4 };
- const rect = __1.Rectangle.create(obj);
- expect(rect.valueOf()).toEqual(obj);
- });
- });
- describe('#toString', () => {
- it('should return JSON string', () => {
- const obj = { x: 1, y: 2, width: 3, height: 4 };
- const rect = __1.Rectangle.create(obj);
- expect(rect.toString()).toEqual(JSON.stringify(obj));
- });
- });
- describe('#bbox', () => {
- it('should return a rectangle that is the bounding box of the rectangle.', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.bbox().equals(rect)).toBeTruthy();
- });
- it('should rotate the rectangle if angle specified.', () => {
- const rect = new __1.Rectangle(0, 0, 2, 4);
- expect(rect.bbox(90).round().equals(new __1.Rectangle(-1, 1, 4, 2))).toBeTruthy();
- });
- });
- describe('#add', () => {
- it('should add the given `rect` to me', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.add(0, 0, 0, 0)).toEqual(new __1.Rectangle(0, 0, 4, 6));
- });
- });
- describe('#update', () => {
- it('should update me with the given `rect`', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.update(0, 0, 1, 1)).toEqual(new __1.Rectangle(0, 0, 1, 1));
- });
- });
- describe('#inflate', () => {
- it('should inflate me with the given `amount`', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.inflate(2)).toEqual(new __1.Rectangle(-1, 0, 7, 8));
- });
- it('should inflate me with the given `dx` and `dy`', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.inflate(2, 1)).toEqual(new __1.Rectangle(-1, 1, 7, 6));
- });
- });
- describe('#snapToGrid', () => {
- it('should snap to grid', () => {
- const rect1 = new __1.Rectangle(2, 6, 33, 44);
- const rect2 = rect1.clone().snapToGrid(10);
- const rect3 = rect1.clone().snapToGrid(3, 5);
- expect(rect2.equals({ x: 0, y: 10, width: 40, height: 40 })).toBeTruthy();
- expect(rect3.equals({ x: 3, y: 5, width: 33, height: 45 })).toBeTruthy();
- });
- });
- describe('#translate', () => {
- it('should translate x and y by adding the given `dx` and `dy` values respectively', () => {
- const rect = new __1.Rectangle(1, 2, 3, 4);
- expect(rect.clone().translate(2, 3).toJSON()).toEqual({
- x: 3,
- y: 5,
- width: 3,
- height: 4,
- });
- expect(rect.clone().translate(new __1.Point(-2, 4)).toJSON()).toEqual({
- x: -1,
- y: 6,
- width: 3,
- height: 4,
- });
- });
- });
- describe('#scale', () => {
- it('should scale point with the given amount', () => {
- expect(new __1.Rectangle(1, 2, 3, 4).scale(2, 3).toJSON()).toEqual({
- x: 2,
- y: 6,
- width: 6,
- height: 12,
- });
- });
- it('should scale point with the given amount and center ', () => {
- expect(new __1.Rectangle(20, 30, 10, 20).scale(2, 3, new __1.Point(40, 45)).toJSON()).toEqual({ x: 0, y: 0, width: 20, height: 60 });
- });
- });
- describe('#rotate', () => {
- it('should rorate the rect by the given angle', () => {
- expect(new __1.Rectangle(1, 2, 3, 4).rotate(180).round().toJSON()).toEqual({
- x: 1,
- y: 2,
- width: 3,
- height: 4,
- });
- });
- it('should keep the same when the given angle is `0`', () => {
- expect(new __1.Rectangle(1, 2, 3, 4).rotate(0).toJSON()).toEqual({
- x: 1,
- y: 2,
- width: 3,
- height: 4,
- });
- });
- });
- describe('#rotate90', () => {
- it("should rorate the rect by 90deg around it's center", () => {
- expect(new __1.Rectangle(1, 2, 3, 4).rotate90().toJSON()).toEqual({
- x: 0.5,
- y: 2.5,
- width: 4,
- height: 3,
- });
- });
- });
- describe('#getMaxScaleToFit', () => {
- it('should return the scale amount', () => {
- const scale1 = new __1.Rectangle(1, 2, 3, 4).getMaxScaleToFit([5, 6, 7, 8]);
- const scale2 = new __1.Rectangle(1, 2, 3, 4).getMaxScaleToFit([0, 0, 7, 8]);
- expect(scale1.sx.toFixed(2)).toEqual('0.16');
- expect(scale1.sy.toFixed(2)).toEqual('0.20');
- expect(scale2.sx.toFixed(2)).toEqual('0.33');
- expect(scale2.sy.toFixed(2)).toEqual('0.50');
- });
- });
- describe('#getMaxUniformScaleToFit', () => {
- it('should return the scale amount', () => {
- const s1 = new __1.Rectangle(1, 2, 3, 4).getMaxUniformScaleToFit([5, 6, 7, 8]);
- const s2 = new __1.Rectangle(1, 2, 3, 4).getMaxUniformScaleToFit([0, 0, 7, 8]);
- expect(s1.toFixed(2)).toEqual('0.16');
- expect(s2.toFixed(2)).toEqual('0.33');
- });
- });
- describe('#moveAndExpand', () => {
- it('should translate and expand me by the given `rect`', () => {
- expect(new __1.Rectangle(1, 2, 3, 4)
- .moveAndExpand(new __1.Rectangle(1, 2, 3, 4))
- .toJSON()).toEqual({ x: 2, y: 4, width: 6, height: 8 });
- expect(new __1.Rectangle(1, 2, 3, 4).moveAndExpand(new __1.Rectangle()).toJSON()).toEqual({ x: 1, y: 2, width: 3, height: 4 });
- });
- });
- describe('#containsPoint', () => {
- it('should return true when rect contains the given point', () => {
- expect(new __1.Rectangle(50, 50, 100, 100).containsPoint(60, 60)).toBeTruthy();
- });
- });
- describe('#containsRect', () => {
- it('should return true when rect is completely inside the other rect', () => {
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 80, 80)).toBeTruthy();
- });
- it('should return true when rect is equal the other rect', () => {
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect({
- x: 50,
- y: 50,
- width: 100,
- height: 100,
- })).toBeTruthy();
- });
- it('should return false when rect is not inside the other rect', () => {
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(20, 20, 200, 200)).toBeFalsy();
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(40, 40, 100, 100)).toBeFalsy();
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 100, 40)).toBeFalsy();
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 100, 100)).toBeFalsy();
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(60, 60, 40, 100)).toBeFalsy();
- });
- it('should return false when one of the dimensions is `0`', () => {
- expect(new __1.Rectangle(50, 50, 100, 100).containsRect(75, 75, 0, 0)).toBeFalsy();
- expect(new __1.Rectangle(50, 50, 0, 0).containsRect(50, 50, 0, 0)).toBeFalsy();
- });
- });
- describe('#intersectsWithRect', () => {
- it('should return the intersection', () => {
- var _a, _b, _c;
- // inside
- expect((_a = new __1.Rectangle(20, 20, 100, 100)
- .intersectsWithRect([40, 40, 20, 20])) === null || _a === void 0 ? void 0 : _a.toJSON()).toEqual({ x: 40, y: 40, width: 20, height: 20 });
- expect((_b = new __1.Rectangle(20, 20, 100, 100)
- .intersectsWithRect([0, 0, 100, 100])) === null || _b === void 0 ? void 0 : _b.toJSON()).toEqual({ x: 20, y: 20, width: 80, height: 80 });
- expect((_c = new __1.Rectangle(20, 20, 100, 100)
- .intersectsWithRect([40, 40, 100, 100])) === null || _c === void 0 ? void 0 : _c.toJSON()).toEqual({ x: 40, y: 40, width: 80, height: 80 });
- });
- it('should return null when no intersection', () => {
- expect(new __1.Rectangle(20, 20, 100, 100).intersectsWithRect([140, 140, 20, 20])).toBeNull();
- });
- });
- describe('#intersectsWithLine', () => {
- it('should return the intersection points', () => {
- const points1 = new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(2, 2, 2, 8));
- const points2 = new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(2, -2, 2, 8));
- expect(__1.Point.equalPoints(points1, [{ x: 2, y: 4 }]));
- expect(__1.Point.equalPoints(points2, [
- { x: 2, y: 0 },
- { x: 2, y: 4 },
- ]));
- });
- it('should return null when no intersection exists', () => {
- expect(new __1.Rectangle(0, 0, 4, 4).intersectsWithLine(new __1.Line(-2, -2, -2, -8))).toBeNull();
- });
- });
- describe('#intersectsWithLineFromCenterToPoint', () => {
- it('should return the intersection point', () => {
- var _a, _b;
- expect((_a = new __1.Rectangle(0, 0, 4, 4)
- .intersectsWithLineFromCenterToPoint([2, 8])) === null || _a === void 0 ? void 0 : _a.equals([2, 4])).toBeTruthy();
- expect((_b = new __1.Rectangle(0, 0, 4, 4)
- .intersectsWithLineFromCenterToPoint([2, 8], 90)) === null || _b === void 0 ? void 0 : _b.round().equals([2, 4])).toBeTruthy();
- });
- it('should return null when no intersection exists', () => {
- expect(new __1.Rectangle(0, 0, 4, 4).intersectsWithLineFromCenterToPoint([3, 3])).toBeNull();
- });
- });
- describe('#normalize', () => {
- it('should keep the same when width and height is positive', () => {
- expect(new __1.Rectangle(1, 2, 3, 4).normalize().toJSON()).toEqual({
- x: 1,
- y: 2,
- width: 3,
- height: 4,
- });
- });
- it('should make the width positive', () => {
- expect(new __1.Rectangle(1, 2, -3, 4).normalize().toJSON()).toEqual({
- x: -2,
- y: 2,
- width: 3,
- height: 4,
- });
- });
- it('should make the height positive', () => {
- expect(new __1.Rectangle(1, 2, 3, -4).normalize().toJSON()).toEqual({
- x: 1,
- y: -2,
- width: 3,
- height: 4,
- });
- });
- });
- describe('#union', () => {
- it('should return the unioned rectangle', () => {
- const rect1 = new __1.Rectangle(-1, -1, 3, 3);
- const rect2 = new __1.Rectangle(0, 0, 4, 4);
- const rect = rect1.union(rect2);
- expect(rect.toJSON()).toEqual({
- x: -1,
- y: -1,
- width: 5,
- height: 5,
- });
- });
- });
- describe('#getNearestSideToPoint', () => {
- it('should return the nearest side to point when point is on the side', () => {
- const rect = new __1.Rectangle(0, 0, 4, 4);
- expect(rect.getNearestSideToPoint({ x: 0, y: 0 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: 4, y: 4 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 0, y: 4 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: 4, y: 0 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 2, y: 0 })).toEqual('top');
- expect(rect.getNearestSideToPoint({ x: 0, y: 2 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: 4, y: 2 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 2, y: 4 })).toEqual('bottom');
- });
- it('should return the nearest side to point when point is outside', () => {
- const rect = new __1.Rectangle(0, 0, 4, 4);
- expect(rect.getNearestSideToPoint({ x: 5, y: 5 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 5, y: 4 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 5, y: 2 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 5, y: 0 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 5, y: -1 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: -1, y: 5 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: -1, y: 4 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: -1, y: 2 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: -1, y: 0 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: -1, y: -1 })).toEqual('left');
- expect(rect.getNearestSideToPoint({ x: 0, y: 5 })).toEqual('bottom');
- expect(rect.getNearestSideToPoint({ x: 2, y: 5 })).toEqual('bottom');
- expect(rect.getNearestSideToPoint({ x: 4, y: 5 })).toEqual('bottom');
- expect(rect.getNearestSideToPoint({ x: 0, y: -1 })).toEqual('top');
- expect(rect.getNearestSideToPoint({ x: 2, y: -1 })).toEqual('top');
- expect(rect.getNearestSideToPoint({ x: 4, y: -1 })).toEqual('top');
- });
- it('should return the nearest side to point when point is inside', () => {
- const rect = new __1.Rectangle(0, 0, 4, 4);
- expect(rect.getNearestSideToPoint({ x: 2, y: 1 })).toEqual('top');
- expect(rect.getNearestSideToPoint({ x: 3, y: 2 })).toEqual('right');
- expect(rect.getNearestSideToPoint({ x: 2, y: 3 })).toEqual('bottom');
- expect(rect.getNearestSideToPoint({ x: 1, y: 2 })).toEqual('left');
- });
- });
- describe('#getNearestPointToPoint', () => {
- it('should return the nearest point to point when point is inside the rect', () => {
- const rect = new __1.Rectangle(0, 0, 4, 4);
- // left
- expect(rect.getNearestPointToPoint({ x: 1, y: 2 }).toJSON()).toEqual({
- x: 0,
- y: 2,
- });
- // right
- expect(rect.getNearestPointToPoint({ x: 3, y: 2 }).toJSON()).toEqual({
- x: 4,
- y: 2,
- });
- // top
- expect(rect.getNearestPointToPoint({ x: 2, y: 1 }).toJSON()).toEqual({
- x: 2,
- y: 0,
- });
- // bottom
- expect(rect.getNearestPointToPoint({ x: 2, y: 3 }).toJSON()).toEqual({
- x: 2,
- y: 4,
- });
- });
- it('should return the nearest point to point when point is outside the rect', () => {
- const rect = new __1.Rectangle(0, 0, 4, 4);
- expect(rect.getNearestPointToPoint({ x: 5, y: 5 }).toJSON()).toEqual({
- x: 4,
- y: 4,
- });
- expect(rect.getNearestPointToPoint({ x: -1, y: -1 }).toJSON()).toEqual({
- x: 0,
- y: 0,
- });
- });
- });
- describe('#serialize', () => {
- it('should return the serialized string', () => {
- expect(new __1.Rectangle(1, 2, 3, 4).serialize()).toEqual('1 2 3 4');
- });
- });
- });
- //# sourceMappingURL=rectangle.test.js.map
|