Compute union of two arbitrary shapes
-
22-09-2019 - |
문제
I'm working on an application, I need to be able to combine two overlapping arbitrary shapes as drawn by the user. This would be a Union operation on the two shapes. The resultant shape would be the silhouette of the two overlapping shapes.
The shapes are stored as a sequence of points in a clockwise manner.
Ideally I'd like an algorithm which will take two arrays of Points (x,y) and return a single array of the resultant shape.
I've been reading Wikipedia on Boolean operations on polygons which mentions the Sweep line algorithm but I can't make the link between this and my goal, alas I'm not a Mathematician.
I'm developing the application in ActionScript 3 but I'm familiar with C#, Java and I can pick my way through C and C++.
해결책
Implementing boolean operations correctly is not trivial; fortunately, there are libraries that already implement this functionality.
What language are you using? If it's C++, take a look at CGAL, the Computational Geometry Algorithms Library.
다른 팁
Given two lists of points (A and B)
- [ 1 ] for each line in A does it intersect a line in B
-.- [2] if no (more) lines intersect, there is no overlap
-.- [3] if a line in (A) intersects a line in B then
-.-.- [4] add the point of intersection into output
-.-.- [5] does the next line from A intersect B
-.-.-.- [6] if not, add this to output (it's inside B) goto 5
-.-.-.- [7] if so, add the intersect to output and switch lists A & B goto 2
Also see Intersection Point Of Two Lines. I'm not gonna write the code sorry :)
See also GPC.
Would this algorithm work for you?
문제가 기능의 여러 인스턴스가되는 것처럼 보입니다. 페이지의 각 새로 고침 / 업데이트에서 증가하는 것처럼 보입니다. 기능에 대한 Sanity-Check를 추가하는 것이 좋습니다.
var test = 0;
var running = running || false;
function go() {
if (running) {
// if an instance of go() is already running, the function quits
return false;
}
else {
running = true; // as the test variable survives I assume this will, too
test = test + 1;
setTimeout(go, 1000);
}
}
go();
.
페이지가 업데이트 될 때마다 test
가 덮어 쓰지 않을 가능성이 큽니다. 할당이 기존 수를 덮어 쓰지 않도록 할당을 보장 할 것을 제안합니다.
var test = test || 0;
var running = running || false;
function go() {
if (running) {
// if an instance of go() is already running, the function quits
return false;
}
else {
var running = true; // as the test variable survives I assume this will, too
test = test + 1;
setTimeout(go, 1000);
}
}
go();
.
은 단순히 test
변수의 존재를 사용하여 함수가 현재 실행 중인지 여부를 결정할 수 있지만, 내가 선택한 다른 목적으로 사용될 것인지를 알지 못하기 때문에해당 목적으로 다른 변수를 만듭니다 (true
또는 false
부울 값을 보관해야합니다).
There seems to be also a javascript api:
https://github.com/bjornharrtell/jsts/
which seems to implement the jts standard and has several differnt implementations:
http://tsusiatsoftware.net/jts/jts-links.html#ports
all of them should be able to perform union etc:
http://tsusiatsoftware.net/jts/JTSUser/contents.html
But csg.js is the most impressive project IMO