Skip to content

Commit

Permalink
- Fix polygon.contain method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbol99 committed May 8, 2020
1 parent 373cf56 commit 80cfb6b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 78 deletions.
101 changes: 52 additions & 49 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions dist/main.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7077,14 +7077,9 @@ class Polygon {
let rel = ray_shoot(this, shape);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
}

if (shape instanceof Flatten.Segment || shape instanceof Flatten.Arc) {
let edge = new Flatten.Edge(shape);
let rel = edge.setInclusion(this);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
else {
return cover(this, shape);
}

// TODO: support Box and Circle
}

/**
Expand Down
9 changes: 2 additions & 7 deletions dist/main.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -7073,14 +7073,9 @@ class Polygon {
let rel = ray_shoot(this, shape);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
}

if (shape instanceof Flatten.Segment || shape instanceof Flatten.Arc) {
let edge = new Flatten.Edge(shape);
let rel = edge.setInclusion(this);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
else {
return cover(this, shape);
}

// TODO: support Box and Circle
}

/**
Expand Down
9 changes: 2 additions & 7 deletions dist/main.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7079,14 +7079,9 @@
let rel = ray_shoot(this, shape);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
}

if (shape instanceof Flatten.Segment || shape instanceof Flatten.Arc) {
let edge = new Flatten.Edge(shape);
let rel = edge.setInclusion(this);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
else {
return cover(this, shape);
}

// TODO: support Box and Circle
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flatten-js/core",
"version": "1.2.5",
"version": "1.2.6",
"description": "Javascript library for 2d geometry",
"main": "dist/main.cjs.js",
"umd:main": "dist/main.umd.js",
Expand Down
10 changes: 3 additions & 7 deletions src/classes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Flatten from '../flatten';
import {ray_shoot} from "../algorithms/ray_shooting";
import * as Intersection from "../algorithms/intersection";
import * as Relations from "../algorithms/relation";

/**
* Class representing a polygon.<br/>
Expand Down Expand Up @@ -367,14 +368,9 @@ export class Polygon {
let rel = ray_shoot(this, shape);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
}

if (shape instanceof Flatten.Segment || shape instanceof Flatten.Arc) {
let edge = new Flatten.Edge(shape);
let rel = edge.setInclusion(this);
return rel === Flatten.INSIDE || rel === Flatten.BOUNDARY;
else {
return Relations.cover(this, shape);
}

// TODO: support Box and Circle
}

/**
Expand Down
32 changes: 32 additions & 0 deletions test/classes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,38 @@ describe('#Flatten.Polygon', function() {
let pt = point(125, 200);
expect(polygon.contains(pt)).to.be.true;
});
it('Can check if contour contains segment (issue #31)', function() {
const points = [
point(306, 306),
point(647, 211),
point(647, 109),
point(147, 109),
point(147, 491),
point(600, 491),
point(600, 401),
point(379, 401),
];
const poly = new Polygon(points);
const lineA = line(point(550, 491), point(500, 401));
const lineB = line(point(520, 491), point(500, 401));
const intersectPointsA = poly.intersect(lineA);
const intersectPointsB = poly.intersect(lineB);
// const constSegmentOutA = new Segment(intersectPointsA[1], intersectPointsA[3]);
expect(poly.contains(segment(intersectPointsA[0], intersectPointsA[1]))).to.be.true;
expect(poly.contains(segment(intersectPointsA[1], intersectPointsA[2]))).to.be.false;
expect(poly.contains(segment(intersectPointsA[2], intersectPointsA[3]))).to.be.true;
expect(poly.contains(segment(intersectPointsA[0], intersectPointsA[2]))).to.be.false;
expect(poly.contains(segment(intersectPointsA[1], intersectPointsA[3]))).to.be.false;

expect(poly.contains(segment(intersectPointsB[0], intersectPointsB[1]))).to.be.true;
expect(poly.contains(segment(intersectPointsB[1], intersectPointsB[2]))).to.be.false;
expect(poly.contains(segment(intersectPointsB[2], intersectPointsB[3]))).to.be.true;
expect(poly.contains(segment(intersectPointsB[0], intersectPointsB[2]))).to.be.false;
expect(poly.contains(segment(intersectPointsB[1], intersectPointsB[3]))).to.be.false;

expect(poly.contains(segment(intersectPointsA[2], intersectPointsB[3]))).to.be.true;
expect(poly.contains(segment(intersectPointsA[2], intersectPointsB[2]))).to.be.true;
});
it('Can measure distance between circle and polygon', function () {
let points = [
point(100, 20),
Expand Down

0 comments on commit 80cfb6b

Please sign in to comment.