Skip to content

Commit

Permalink
Merge 6b894bf into 4d007c6
Browse files Browse the repository at this point in the history
  • Loading branch information
delmohf authored Aug 22, 2023
2 parents 4d007c6 + 6b894bf commit face43e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/classes/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export class Box extends Shape {
return this.clone();
}

/**
* Return true if box contains shape: no point of shape lies outside of the box
* @param {Shape} shape - test shape
* @returns {boolean}
*/
contains(shape) {
if (shape instanceof Flatten.Point) {
return (shape.x >= this.xmin) && (shape.x <= this.xmax) && (shape.y >= this.ymin) && (shape.y <= this.ymax);
}
}

/**
* Returns true if not intersected with other box
* @param {Box} other_box - other box to test
Expand All @@ -132,17 +143,21 @@ export class Box extends Shape {
}

/**
* Returns new box merged with other box
* @param {Box} other_box - Other box to merge with
* Returns new box merged with other box or point
* @param {Box|Point} other_box_pt - Other box or point to merge with
* @returns {Box}
*/
merge(other_box) {
return new Box(
this.xmin === undefined ? other_box.xmin : Math.min(this.xmin, other_box.xmin),
this.ymin === undefined ? other_box.ymin : Math.min(this.ymin, other_box.ymin),
this.xmax === undefined ? other_box.xmax : Math.max(this.xmax, other_box.xmax),
this.ymax === undefined ? other_box.ymax : Math.max(this.ymax, other_box.ymax)
);
merge(other_box_pt) {
if (other_box_pt instanceof Flatten.Box) {
return new Box(
this.xmin === undefined ? other_box_pt.xmin : Math.min(this.xmin, other_box_pt.xmin),
this.ymin === undefined ? other_box_pt.ymin : Math.min(this.ymin, other_box_pt.ymin),
this.xmax === undefined ? other_box_pt.xmax : Math.max(this.xmax, other_box_pt.xmax),
this.ymax === undefined ? other_box_pt.ymax : Math.max(this.ymax, other_box_pt.ymax)
);
} else {
return this.merge(other_box_pt.box());
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/classes/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class Point extends Shape {
return this.equalTo(shape);
}

if (shape instanceof Flatten.Box) {
return shape.contains(this);
}

if (shape instanceof Flatten.Line) {
return shape.contains(this);
}
Expand Down

0 comments on commit face43e

Please sign in to comment.