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

fix 'intersect' method + add couple of tests

parent 10e90065
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,8 @@ export default class Rectangle { ...@@ -17,7 +17,8 @@ export default class Rectangle {
intersect(rect) { intersect(rect) {
/* /*
Returns true if this rectangle intersects with rect. Returns true if this rectangle intersects with rect
(or other way around).
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.p1)) { if (this.contains_point(rect.p1)) {
...@@ -36,6 +37,23 @@ export default class Rectangle { ...@@ -36,6 +37,23 @@ export default class Rectangle {
return true; return true;
} }
// or other way around
if (rect.contains_point(this.p1)) {
return true;
}
if (rect.contains_point(this.p2)) {
return true;
}
if (rect.contains_point(this.p3)) {
return true;
}
if (rect.contains_point(this.p4)) {
return true;
}
return false; return false;
} }
......
...@@ -53,5 +53,35 @@ module('Unit | Rectangle', function () { ...@@ -53,5 +53,35 @@ module('Unit | Rectangle', function () {
rect.p4.isEqual(new Point(100, 205)) rect.p4.isEqual(new Point(100, 205))
); );
}); });
test('instersect positive', function(assert) {
let rect1, rect2;
rect1 = new Rectangle(100, 100, 20, 20);
rect2 = new Rectangle(110, 90, 5, 25);
assert.true(
rect1.intersect(rect2)
);
assert.true(
rect2.intersect(rect1)
);
});
test('instersect negative', function(assert) {
let rect1, rect2;
rect1 = new Rectangle(100, 100, 20, 20);
rect2 = new Rectangle(10, 10, 15, 15);
assert.false(
rect1.intersect(rect2)
);
assert.false(
rect2.intersect(rect1)
);
});
}); });
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