Skip to content

Commit

Permalink
Merge pull request #160 from alexbol99/111_contains_relation_between_…
Browse files Browse the repository at this point in the history
…point_and_box

Add method 'contains' to Box
  • Loading branch information
alexbol99 authored Dec 23, 2023
2 parents 4903aa3 + 649b34b commit b89e303
Show file tree
Hide file tree
Showing 60 changed files with 639 additions and 202 deletions.
69 changes: 60 additions & 9 deletions dist/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3983,28 +3983,35 @@ class PlanarSet extends Set {
* This happens with no error, it is possible to use <i>size</i> property to check if
* a shape was actually added.<br/>
* Method returns planar set object updated and may be chained
* @param {Shape} shape - shape to be added, should have valid <i>box</i> property
* @param {AnyShape | {Box, AnyShape}} entry - shape to be added, should have valid <i>box</i> property
* Another option to transfer as an object {key: Box, value: AnyShape}
* @returns {PlanarSet}
*/
add(shape) {
add(entry) {
let size = this.size;
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
super.add(shape);
// size not changed - item not added, probably trying to add same item twice
if (this.size > size) {
this.index.insert(shape.box, shape);
this.index.insert(box, shape);
}
return this; // in accordance to Set.add interface
}

/**
* Delete shape from planar set. Returns true if shape was actually deleted, false otherwise
* @param {Shape} shape - shape to be deleted
* @param {AnyShape | {Box, AnyShape}} entry - shape to be deleted
* @returns {boolean}
*/
delete(shape) {
delete(entry) {
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
let deleted = super.delete(shape);
if (deleted) {
this.index.remove(shape.box, shape);
this.index.remove(box, shape);
}
return deleted;
}
Expand All @@ -4021,7 +4028,7 @@ class PlanarSet extends Set {
* 2d range search in planar set.<br/>
* Returns array of all shapes in planar set which bounding box is intersected with query box
* @param {Box} box - query box
* @returns {Shapes[]}
* @returns {AnyShape[]}
*/
search(box) {
let resp = this.index.search(box);
Expand All @@ -4031,7 +4038,7 @@ class PlanarSet extends Set {
/**
* Point location test. Returns array of shapes which contains given point
* @param {Point} point - query point
* @returns {Array}
* @returns {AnyShape[]}
*/
hit(point) {
let box = new Flatten.Box(point.x - 1, point.y - 1, point.x + 1, point.y + 1);
Expand Down Expand Up @@ -4302,14 +4309,18 @@ let Point$1 = class Point extends Shape {

/**
* Returns true if point is on a shape, false otherwise
* @param {Shape} shape Shape of the one of supported types Point, Line, Circle, Segment, Arc, Polygon
* @param {Shape} shape
* @returns {boolean}
*/
on(shape) {
if (shape instanceof Flatten.Point) {
return this.equalTo(shape);
}

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

if (shape instanceof Flatten.Line) {
return shape.contains(this);
}
Expand Down Expand Up @@ -6330,6 +6341,46 @@ class Box extends Shape {
(new_box, pt) => new_box.merge(pt.box), new Box())
}

/**
* Return true if box contains shape: no point of shape lies outside the box
* @param {AnyShape} 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);
}

if (shape instanceof Flatten.Segment) {
return shape.vertices.every(vertex => this.contains(vertex))
}

if (shape instanceof Flatten.Box) {
return shape.toSegments().every(segment => this.contains(segment))
}

if (shape instanceof Flatten.Circle) {
return this.contains(shape.box)
}

if (shape instanceof Flatten.Arc) {
return shape.vertices.every(vertex => this.contains(vertex)) &&
shape.toSegments().every(segment => intersectSegment2Arc(segment, shape).length === 0)
}

if (shape instanceof Flatten.Line || shape instanceof Flatten.Ray) {
return false
}

if (shape instanceof Flatten.Multiline) {
return shape.toShapes().every(shape => this.contains(shape))
}

if (shape instanceof Flatten.Polygon) {
return this.contains(shape.box)
}
}

get name() {
return "box"
}
Expand Down
69 changes: 60 additions & 9 deletions dist/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3979,28 +3979,35 @@ class PlanarSet extends Set {
* This happens with no error, it is possible to use <i>size</i> property to check if
* a shape was actually added.<br/>
* Method returns planar set object updated and may be chained
* @param {Shape} shape - shape to be added, should have valid <i>box</i> property
* @param {AnyShape | {Box, AnyShape}} entry - shape to be added, should have valid <i>box</i> property
* Another option to transfer as an object {key: Box, value: AnyShape}
* @returns {PlanarSet}
*/
add(shape) {
add(entry) {
let size = this.size;
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
super.add(shape);
// size not changed - item not added, probably trying to add same item twice
if (this.size > size) {
this.index.insert(shape.box, shape);
this.index.insert(box, shape);
}
return this; // in accordance to Set.add interface
}

/**
* Delete shape from planar set. Returns true if shape was actually deleted, false otherwise
* @param {Shape} shape - shape to be deleted
* @param {AnyShape | {Box, AnyShape}} entry - shape to be deleted
* @returns {boolean}
*/
delete(shape) {
delete(entry) {
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
let deleted = super.delete(shape);
if (deleted) {
this.index.remove(shape.box, shape);
this.index.remove(box, shape);
}
return deleted;
}
Expand All @@ -4017,7 +4024,7 @@ class PlanarSet extends Set {
* 2d range search in planar set.<br/>
* Returns array of all shapes in planar set which bounding box is intersected with query box
* @param {Box} box - query box
* @returns {Shapes[]}
* @returns {AnyShape[]}
*/
search(box) {
let resp = this.index.search(box);
Expand All @@ -4027,7 +4034,7 @@ class PlanarSet extends Set {
/**
* Point location test. Returns array of shapes which contains given point
* @param {Point} point - query point
* @returns {Array}
* @returns {AnyShape[]}
*/
hit(point) {
let box = new Flatten.Box(point.x - 1, point.y - 1, point.x + 1, point.y + 1);
Expand Down Expand Up @@ -4298,14 +4305,18 @@ let Point$1 = class Point extends Shape {

/**
* Returns true if point is on a shape, false otherwise
* @param {Shape} shape Shape of the one of supported types Point, Line, Circle, Segment, Arc, Polygon
* @param {Shape} shape
* @returns {boolean}
*/
on(shape) {
if (shape instanceof Flatten.Point) {
return this.equalTo(shape);
}

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

if (shape instanceof Flatten.Line) {
return shape.contains(this);
}
Expand Down Expand Up @@ -6326,6 +6337,46 @@ class Box extends Shape {
(new_box, pt) => new_box.merge(pt.box), new Box())
}

/**
* Return true if box contains shape: no point of shape lies outside the box
* @param {AnyShape} 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);
}

if (shape instanceof Flatten.Segment) {
return shape.vertices.every(vertex => this.contains(vertex))
}

if (shape instanceof Flatten.Box) {
return shape.toSegments().every(segment => this.contains(segment))
}

if (shape instanceof Flatten.Circle) {
return this.contains(shape.box)
}

if (shape instanceof Flatten.Arc) {
return shape.vertices.every(vertex => this.contains(vertex)) &&
shape.toSegments().every(segment => intersectSegment2Arc(segment, shape).length === 0)
}

if (shape instanceof Flatten.Line || shape instanceof Flatten.Ray) {
return false
}

if (shape instanceof Flatten.Multiline) {
return shape.toShapes().every(shape => this.contains(shape))
}

if (shape instanceof Flatten.Polygon) {
return this.contains(shape.box)
}
}

get name() {
return "box"
}
Expand Down
69 changes: 60 additions & 9 deletions dist/main.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3985,28 +3985,35 @@
* This happens with no error, it is possible to use <i>size</i> property to check if
* a shape was actually added.<br/>
* Method returns planar set object updated and may be chained
* @param {Shape} shape - shape to be added, should have valid <i>box</i> property
* @param {AnyShape | {Box, AnyShape}} entry - shape to be added, should have valid <i>box</i> property
* Another option to transfer as an object {key: Box, value: AnyShape}
* @returns {PlanarSet}
*/
add(shape) {
add(entry) {
let size = this.size;
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
super.add(shape);
// size not changed - item not added, probably trying to add same item twice
if (this.size > size) {
this.index.insert(shape.box, shape);
this.index.insert(box, shape);
}
return this; // in accordance to Set.add interface
}

/**
* Delete shape from planar set. Returns true if shape was actually deleted, false otherwise
* @param {Shape} shape - shape to be deleted
* @param {AnyShape | {Box, AnyShape}} entry - shape to be deleted
* @returns {boolean}
*/
delete(shape) {
delete(entry) {
const {key, value} = entry;
const box = key || entry.box;
const shape = value || entry;
let deleted = super.delete(shape);
if (deleted) {
this.index.remove(shape.box, shape);
this.index.remove(box, shape);
}
return deleted;
}
Expand All @@ -4023,7 +4030,7 @@
* 2d range search in planar set.<br/>
* Returns array of all shapes in planar set which bounding box is intersected with query box
* @param {Box} box - query box
* @returns {Shapes[]}
* @returns {AnyShape[]}
*/
search(box) {
let resp = this.index.search(box);
Expand All @@ -4033,7 +4040,7 @@
/**
* Point location test. Returns array of shapes which contains given point
* @param {Point} point - query point
* @returns {Array}
* @returns {AnyShape[]}
*/
hit(point) {
let box = new Flatten.Box(point.x - 1, point.y - 1, point.x + 1, point.y + 1);
Expand Down Expand Up @@ -4304,14 +4311,18 @@

/**
* Returns true if point is on a shape, false otherwise
* @param {Shape} shape Shape of the one of supported types Point, Line, Circle, Segment, Arc, Polygon
* @param {Shape} shape
* @returns {boolean}
*/
on(shape) {
if (shape instanceof Flatten.Point) {
return this.equalTo(shape);
}

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

if (shape instanceof Flatten.Line) {
return shape.contains(this);
}
Expand Down Expand Up @@ -6332,6 +6343,46 @@
(new_box, pt) => new_box.merge(pt.box), new Box())
}

/**
* Return true if box contains shape: no point of shape lies outside the box
* @param {AnyShape} 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);
}

if (shape instanceof Flatten.Segment) {
return shape.vertices.every(vertex => this.contains(vertex))
}

if (shape instanceof Flatten.Box) {
return shape.toSegments().every(segment => this.contains(segment))
}

if (shape instanceof Flatten.Circle) {
return this.contains(shape.box)
}

if (shape instanceof Flatten.Arc) {
return shape.vertices.every(vertex => this.contains(vertex)) &&
shape.toSegments().every(segment => intersectSegment2Arc(segment, shape).length === 0)
}

if (shape instanceof Flatten.Line || shape instanceof Flatten.Ray) {
return false
}

if (shape instanceof Flatten.Multiline) {
return shape.toShapes().every(shape => this.contains(shape))
}

if (shape instanceof Flatten.Polygon) {
return this.contains(shape.box)
}
}

get name() {
return "box"
}
Expand Down
4 changes: 2 additions & 2 deletions docs/Arc.html

Large diffs are not rendered by default.

208 changes: 182 additions & 26 deletions docs/Box.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Circle.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/CircularLinkedList.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/DE9IM.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Edge.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Errors.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Face.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Inversion.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Line.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/LinkedList.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Matrix.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Multiline.html

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions docs/PlanarSet.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/Point.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Polygon.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Ray.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Segment.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Shape.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Vector.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_boolean_op.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_distance.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_ray_shooting.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/algorithms_relation.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_arc.js.html

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions docs/classes_box.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_circle.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_edge.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_face.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_inversion.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_line.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_matrix.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_multiline.js.html

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions docs/classes_point.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_polygon.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_ray.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_segment.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_shape.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes_vector.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_circular_linked_list.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_de9im.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/data_structures_linked_list.js.html

Large diffs are not rendered by default.

27 changes: 17 additions & 10 deletions docs/data_structures_planar_set.js.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/global.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-BooleanOperations.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-RayShoot.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/module-Relation.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_constants.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_errors.js.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/utils_utils.js.html

Large diffs are not rendered by default.

Loading

0 comments on commit b89e303

Please sign in to comment.