Skip to content

Commit

Permalink
Merge pull request #168 from alexbol99/166_polygon_intersect_not_working
Browse files Browse the repository at this point in the history
Fix polygon.intersect function
  • Loading branch information
alexbol99 authored Mar 30, 2024
2 parents 565ae09 + 2ebe8f2 commit d2c5652
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 77 deletions.
17 changes: 2 additions & 15 deletions dist/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,18 +2221,7 @@ function intersectEdge2Polygon(edge, polygon) {
let resp_edges = polygon.edges.search(edge.shape.box);

for (let resp_edge of resp_edges) {
if (resp_edge.isSegment) {
ip = [...ip, ...intersectSegment2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isArc) {
ip = [...ip, ...intersectArc2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isLine) {
ip = [...ip, ...intersectLine2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isRay) {
ip = [...ip, ...intersectRay2Polygon(resp_edge, polygon)];
}
ip = [...ip, ...intersectEdge2Edge(edge, resp_edge)];
}

return ip;
Expand All @@ -2250,9 +2239,7 @@ function intersectPolygon2Polygon(polygon1, polygon2) {
}

for (let edge1 of polygon1.edges) {
for (let pt of intersectEdge2Polygon(edge1, polygon2)) {
ip.push(pt);
}
ip = [...ip, ...intersectEdge2Polygon(edge1, polygon2)];
}

return ip;
Expand Down
17 changes: 2 additions & 15 deletions dist/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,18 +2217,7 @@ function intersectEdge2Polygon(edge, polygon) {
let resp_edges = polygon.edges.search(edge.shape.box);

for (let resp_edge of resp_edges) {
if (resp_edge.isSegment) {
ip = [...ip, ...intersectSegment2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isArc) {
ip = [...ip, ...intersectArc2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isLine) {
ip = [...ip, ...intersectLine2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isRay) {
ip = [...ip, ...intersectRay2Polygon(resp_edge, polygon)];
}
ip = [...ip, ...intersectEdge2Edge(edge, resp_edge)];
}

return ip;
Expand All @@ -2246,9 +2235,7 @@ function intersectPolygon2Polygon(polygon1, polygon2) {
}

for (let edge1 of polygon1.edges) {
for (let pt of intersectEdge2Polygon(edge1, polygon2)) {
ip.push(pt);
}
ip = [...ip, ...intersectEdge2Polygon(edge1, polygon2)];
}

return ip;
Expand Down
17 changes: 2 additions & 15 deletions dist/main.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2223,18 +2223,7 @@
let resp_edges = polygon.edges.search(edge.shape.box);

for (let resp_edge of resp_edges) {
if (resp_edge.isSegment) {
ip = [...ip, ...intersectSegment2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isArc) {
ip = [...ip, ...intersectArc2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isLine) {
ip = [...ip, ...intersectLine2Polygon(resp_edge, polygon)];
}
else if (resp_edge.isRay) {
ip = [...ip, ...intersectRay2Polygon(resp_edge, polygon)];
}
ip = [...ip, ...intersectEdge2Edge(edge, resp_edge)];
}

return ip;
Expand All @@ -2252,9 +2241,7 @@
}

for (let edge1 of polygon1.edges) {
for (let pt of intersectEdge2Polygon(edge1, polygon2)) {
ip.push(pt);
}
ip = [...ip, ...intersectEdge2Polygon(edge1, polygon2)];
}

return ip;
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.5.0",
"version": "1.5.1",
"description": "Javascript library for 2d geometry",
"main": "dist/main.cjs",
"umd:main": "dist/main.umd.js",
Expand Down
21 changes: 3 additions & 18 deletions src/algorithms/intersection.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,18 +527,7 @@ export function intersectEdge2Polygon(edge, polygon) {
let resp_edges = polygon.edges.search(edge.shape.box);

for (let resp_edge of resp_edges) {
if (resp_edge.isSegment) {
ip = [...ip, ...intersectSegment2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isArc) {
ip = [...ip, ...intersectArc2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isLine) {
ip = [...ip, ...intersectLine2Polygon(resp_edge, polygon)]
}
else if (resp_edge.isRay) {
ip = [...ip, ...intersectRay2Polygon(resp_edge, polygon)]
}
ip = [...ip, ...intersectEdge2Edge(edge, resp_edge)]
}

return ip;
Expand All @@ -552,9 +541,7 @@ export function intersectMultiline2Polygon(multiline, polygon) {
}

for (let edge of multiline) {
let ip_edge = intersectEdge2Polygon(edge, polygon);
let ip_sorted = edge.shape.sortPoints(ip_edge); // TODO: support arc edge
ip = [...ip, ...ip_sorted];
ip = [...ip, ...intersectEdge2Polygon(edge, polygon)];
}

return ip;
Expand All @@ -572,9 +559,7 @@ export function intersectPolygon2Polygon(polygon1, polygon2) {
}

for (let edge1 of polygon1.edges) {
for (let pt of intersectEdge2Polygon(edge1, polygon2)) {
ip.push(pt);
}
ip = [...ip, ...intersectEdge2Polygon(edge1, polygon2)]
}

return ip;
Expand Down
50 changes: 37 additions & 13 deletions test/classes/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { expect } from 'chai';
import Flatten, {matrix} from '../../index';

import {Point, Circle, Line, Segment, Arc, Box, Polygon, Edge, PlanarSet} from '../../index';
import {Point, Circle, Line, Segment, Arc, Box, Polygon, Edge, PlanarSet, Multiline} from '../../index';
import {point, vector, circle, line, segment, box, multiline} from '../../index';
import {intersectLine2Polygon} from "../../src/algorithms/intersection";
import {intersectLine2Polygon, intersectPolygon2Polygon, intersectMultiline2Polygon} from "../../src/algorithms/intersection";
import * as BooleanOperations from "../../src/algorithms/boolean_op";
let {unify} = BooleanOperations;

Expand Down Expand Up @@ -812,10 +812,7 @@ describe('#Flatten.Polygon', function() {
let l = line( point(100,400), vector(0,1) );
let points = [point(100, 20), point(250, 75), point(350, 75), point(300, 200), point(170, 200), point(120, 350), point(70, 120) ];
let p = new Polygon(points);
let ip = intersectLine2Polygon(l, p);
let mline = multiline([l]);
mline.split(ip);
let cut_polygon = p.cut(mline);
let cut_polygon = p.cutWithLine(l);

expect(cut_polygon.faces.size).to.equal(p.faces.size);
expect(cut_polygon.edges.size).to.equal(p.edges.size);
Expand Down Expand Up @@ -919,25 +916,52 @@ describe('#Flatten.Polygon', function() {
expect(res_poly.faces.size).to.equal(3);
expect(res_poly.edges.size).to.equal(14);
});
it('Cannot cut polygon with MultiLine #159 - fixed', () => {
const { point, Polygon, Multiline } = Flatten;

it('Can cut polygon with MultiLine #159', () => {
const poly = new Polygon([
point(20, 20),
point(60, 20),
point(60, 60),
point(20, 60)
]);
const segments = [
const multiLine = new Multiline([
segment(20, 20, 40, 40),
segment(40, 40, 50, 40),
segment(50, 40, 60, 60)
];

const multiLine = new Multiline(segments);
]);
const newPoly = poly.cut(multiLine);

expect(newPoly.faces.size).to.equal(2);
expect(newPoly.edges.size).to.equal(10)
})
describe('#Intersections', function () {
it('Can perform intersection between polygons', function () {
const poly1 = new Polygon(
[point(0, 0), point(150, 0), point(150, 30), point(0, 30)]
);
const poly2 = new Polygon(
[point(100, 20), point(200, 20), point(200, 40), point(100, 40)]
);
const ip = intersectPolygon2Polygon(poly1, poly2)

expect(ip.length).to.equal(2)
expect(ip[0]).to.deep.equal({x:150, y:20})
expect(ip[1]).to.deep.equal({x:100, y:30})
});
it('Can perform intersection between multiline and polygon', function() {
let poly = new Polygon([
point(100, 20),
point(250, 75),
point(350, 75),
point(300, 200),
point(170, 200),
point(120, 350),
point(70, 120)
]);
let l = line( point(100,175), vector(0,1) );
let mline = new Multiline([l]);
let ip = intersectMultiline2Polygon(mline, poly)

expect(ip.length).to.equal(2);
});
});
});

0 comments on commit d2c5652

Please sign in to comment.