Skip to content
Snippets Groups Projects
Commit 08280e97 authored by Eugen Ciur's avatar Eugen Ciur
Browse files

refactor rectangle class to use point class

parent c867f689
No related branches found
No related tags found
No related merge requests found
import Point from 'papermerge/utils/point';
export default class Rectangle { export default class Rectangle {
...@@ -7,6 +8,11 @@ export default class Rectangle { ...@@ -7,6 +8,11 @@ export default class Rectangle {
this.y = y; this.y = y;
this.width = parseInt(width); this.width = parseInt(width);
this.height = parseInt(height); this.height = parseInt(height);
this.p1 = new Point(this.x, this.y);
this.p2 = new Point(this.x + this.width, this.y);
this.p3 = new Point(this.x + this.width, this.y + this.height);
this.p4 = new Point(this.x, this.y + this.height);
} }
intersect(rect) { intersect(rect) {
...@@ -14,37 +20,37 @@ export default class Rectangle { ...@@ -14,37 +20,37 @@ export default class Rectangle {
Returns true if this rectangle intersects with rect. Returns true if this rectangle intersects with rect.
Two rectangle intersect if one of them has a point inside other. Two rectangle intersect if one of them has a point inside other.
*/ */
if (this.contains_point(rect.x, rect.y)) { if (this.contains_point(rect.p1)) {
return true; return true;
} }
if (this.contains_point(rect.x + rect.width, rect.y)) { if (this.contains_point(rect.p2)) {
return true; return true;
} }
if (this.contains_point(rect.x + rect.width, rect.y + rect.height)) { if (this.contains_point(rect.p3)) {
return true; return true;
} }
if (this.contains_point(rect.x, rect.y + rect.height)) { if (this.contains_point(rect.p4)) {
return true; return true;
} }
return false; return false;
} }
contains_point(x, y) { contains_point(point) {
/* /*
Is point (x, y) inside this rectangle ? Is point (x, y) inside this rectangle ?
*/ */
let x_is_within = false, y_is_within = false; let x_is_within = false, y_is_within = false;
if (this.x <= x && x <= this.x + this.width) { if (this.x <= point.x && point.x <= this.x + this.width) {
x_is_within = true; x_is_within = true;
} }
if (this.y <= y && y <= this.y + this.height) { if (this.y <= point.y && point.y <= this.y + this.height) {
y_is_within = true; y_is_within = true;
} }
......
import { module, test } from 'qunit'; import { module, test } from 'qunit';
import Rectangle from 'papermerge/utils/rectangle'; import Rectangle from 'papermerge/utils/rectangle';
import Point from 'papermerge/utils/point';
module('Unit | Rectangle', function () { module('Unit | Rectangle', function () {
test('basic', function (assert) { test('basic rectangle creation', function (assert) {
let rect = new Rectangle(1, 1, 200, 200); let rect = new Rectangle(1, 1, 200, 200);
assert.ok(rect); assert.ok(rect);
}); });
test('contains_point positive', function(assert) {
let rect, point;
rect = new Rectangle(100, 100, 10, 10);
point = new Point(105, 105);
assert.true(
rect.contains_point(point)
)
});
test('contains_point negative', function(assert) {
let rect, point;
rect = new Rectangle(100, 100, 10, 10);
point = new Point(50, 50);
assert.false(
rect.contains_point(point)
)
});
test('p1, p2, p3, p4', function(assert) {
let rect;
rect = new Rectangle(100, 200, 10, 5);
assert.true(
rect.p1.isEqual(new Point(100, 200))
);
assert.true(
rect.p2.isEqual(new Point(110, 200))
);
assert.true(
rect.p3.isEqual(new Point(110, 205))
);
assert.true(
rect.p4.isEqual(new Point(100, 205))
);
});
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment