- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
import { Rectangle } from '@pixi/math';
import '@pixi/math-extras';
describe('Rectangle', () =>
{
describe('containsRect', () =>
{
it('should return true if all four corners are inside or on the edge of the rectangle', () =>
{
/*
+-----------------+-----------------+
| A | B |
| +------+ | |
| | C | | |
| | | | 🄳 |
| | | | |
| +------+ | |
| | |
+-----------------+-----------------+
*/
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(100, 0, 100, 100);
const c = new Rectangle(25, 25, 50, 50);
const d = new Rectangle(150, 50, 0, 0);
// a contains c
expect(a.containsRect(c)).toEqual(true);
// but c doesn't contain a
expect(c.containsRect(a)).toEqual(false);
// b doesn't contain c
expect(a.containsRect(b)).toEqual(false);
// B contains D. An empty rectangle **CAN** be contained
expect(b.containsRect(d)).toEqual(true);
// Any rectangle with area contains itself
expect(a.containsRect(a.clone())).toEqual(true);
expect(b.containsRect(b.clone())).toEqual(true);
expect(c.containsRect(c.clone())).toEqual(true);
// An empty rectangle can't contain anything, thus it can't contain itself
expect(d.containsRect(d.clone())).toEqual(false);
});
});
describe('equals', () =>
{
it('should return true x, y, width and height match', () =>
{
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(0, 0, 100, 100);
const c = new Rectangle(50, 50, 100, 100);
// a is equal to b
expect(a.equals(b)).toEqual(true);
// thus b is equal to a
expect(b.equals(a)).toEqual(true);
// c is not equal to a
expect(a.equals(c)).toEqual(false);
// any point is equal to themselves
expect(a.equals(a.clone())).toEqual(true);
expect(b.equals(b.clone())).toEqual(true);
expect(c.equals(c.clone())).toEqual(true);
});
});
describe('intersection', () =>
{
it('should return a rectangle with the intersection if the area is > 0, otherwise an empty rectangle',
() =>
{
/*
+--------+
| A |
| +---+---+
| | 🄴 | B |
+---|---+---|-----+
| | | |
+---+---+ C |
| |
+---------+
*/
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(50, 50, 100, 100);
const c = new Rectangle(100, 100, 100, 100);
const empty = new Rectangle(75, 75, 0, 0);
const d = a.intersection(b);
const e = a.intersection(c);
const emptyIntersects = empty.intersection(b);
const intersectingEmpty = b.intersection(empty);
const emptyIntersectsItself = empty.intersection(empty);
expect(d.x).toEqual(50);
expect(d.y).toEqual(50);
expect(d.width).toEqual(50);
expect(d.height).toEqual(50);
expect(e.x).toEqual(0);
expect(e.y).toEqual(0);
expect(e.width).toEqual(0);
expect(e.height).toEqual(0);
// empty rectangles can't intersect or be intersected
expect(emptyIntersects.x).toEqual(0);
expect(emptyIntersects.y).toEqual(0);
expect(emptyIntersects.width).toEqual(0);
expect(emptyIntersects.height).toEqual(0);
expect(intersectingEmpty.x).toEqual(0);
expect(intersectingEmpty.y).toEqual(0);
expect(intersectingEmpty.width).toEqual(0);
expect(intersectingEmpty.height).toEqual(0);
expect(emptyIntersectsItself.x).toEqual(0);
expect(emptyIntersectsItself.y).toEqual(0);
expect(emptyIntersectsItself.width).toEqual(0);
expect(emptyIntersectsItself.height).toEqual(0);
});
it('should return the same reference given', () =>
{
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(50, 50, 100, 100);
const c = a.intersection(b, a);
expect(c).toEqual(a);
});
});
describe('union', () =>
{
it('should return a rectangle that includes both rectangles (similar to enlarge)',
() =>
{
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(50, 50, 100, 100);
const c = a.union(b);
expect(c.x).toEqual(0);
expect(c.y).toEqual(0);
expect(c.width).toEqual(150);
expect(c.height).toEqual(150);
});
it('should return the same reference given', () =>
{
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(50, 50, 100, 100);
const c = a.union(b, a);
expect(c).toEqual(a);
});
it('should return the same values as enalrge()',
() =>
{
const enlarged = new Rectangle(0, 0, 100, 100);
const a = new Rectangle(0, 0, 100, 100);
const b = new Rectangle(50, 50, 100, 100);
const c = a.union(b);
enlarged.enlarge(b);
expect(c.x).toEqual(enlarged.x);
expect(c.y).toEqual(enlarged.y);
expect(c.width).toEqual(enlarged.width);
expect(c.height).toEqual(enlarged.height);
});
});
});