From 2ebe8f2c4abc55d71f064632e58be880416df995 Mon Sep 17 00:00:00 2001 From: alexanderbol Date: Sat, 30 Mar 2024 15:05:17 +0300 Subject: [PATCH] polygon.intersect() function not working in new version #166 @metehan-coone --- dist/main.cjs | 17 ++---------- dist/main.mjs | 17 ++---------- dist/main.umd.js | 17 ++---------- package.json | 2 +- src/algorithms/intersection.js | 21 ++------------ test/classes/polygon.js | 50 +++++++++++++++++++++++++--------- 6 files changed, 47 insertions(+), 77 deletions(-) diff --git a/dist/main.cjs b/dist/main.cjs index ca2970b..07ebb49 100644 --- a/dist/main.cjs +++ b/dist/main.cjs @@ -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; @@ -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; diff --git a/dist/main.mjs b/dist/main.mjs index 4c121d8..80c1784 100644 --- a/dist/main.mjs +++ b/dist/main.mjs @@ -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; @@ -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; diff --git a/dist/main.umd.js b/dist/main.umd.js index cec3f16..bfe0d6f 100644 --- a/dist/main.umd.js +++ b/dist/main.umd.js @@ -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; @@ -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; diff --git a/package.json b/package.json index 47067db..080eead 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/algorithms/intersection.js b/src/algorithms/intersection.js index 43590d8..c8fbecb 100644 --- a/src/algorithms/intersection.js +++ b/src/algorithms/intersection.js @@ -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; @@ -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; @@ -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; diff --git a/test/classes/polygon.js b/test/classes/polygon.js index e8850df..bdf63dc 100644 --- a/test/classes/polygon.js +++ b/test/classes/polygon.js @@ -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; @@ -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); @@ -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); + }); + }); });