It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to solve a question, but it's a little hard and I need some help. The question is:
we have 2 triangles, and we have the coordinates of the vertices like (x1, y1), (x2, y2), (x3, y3), (a1, b1), (a2, b2), (a3, b3).
We want to measure the area that two triangles are on each other. It may be 0 or more.
For example, if we have for first triangle (0,0) (3,0) (0,3) and the second (0,0) (3,3) (3,0), the common area will be 2.25.
How should i write a program to solve this?
The problem of intersecting triangles (and convex polygon in general) is way tougher than it seems, especially if you wanna solve it in linear time with respect to the number of edges involved.
You can consider this page to have an idea of a working algorithm for general convex polygons (the algorithm is based on rotating calipers. Indeed, there's some abstract geometry behind, in particular, the geometric Hanh-Banach theorem).
Consider that once you have the intersection polygon, which is convex, evaluating the area is trivial.
Thus, you have two options:
You keep the problem abstract (somehow you consider triangles as convex polygons, and that's it) and a fast solution in C/C++ can be achieved through the GPC library (which is written in C) or, alternatively, for instance, through boost::geometry.
You specialize just for triangles: in this case, my advice is to consider this wonderful paper which details topologically the possible ways of intersecting involved, and gives an efficient implementation of the solution.
I have one more thing to say: when you consider your problem with toy triangles (i.e. low skewness, sizes way larger than machine precision) you can still think to implement your own algorithm and play with it. But, when you have to intersect millions of possibly ill-conditioned triangles per second, you'd better rely on a good and fast library.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am looking for the best (efficient algorithm for calculating the area of 2D polygon (especially for triangle given three points). I search on the web and I found the following link, but still I'm not sure that they are efficient in terms of memory cost or not (since my mesh is huge).
I am wondering if there is any tricks in c++ (I'm newbie in c++) which could be applied on them:
here are the links:
(stackoverflow)
How to determine if a point is in a 2D triangle?
http://www.softwareandfinance.com/Visual_CPP/Triangle_Area_Perimeter.html
It's worth to mention that the final target is to find out if a point is inside (NOT on the border) the polygon.
thanks for any help.
Joachim Pileborg suggested in comments that the area isn't needed, but that misses the point: there's an efficient algorithm which does require an intermediate value, that just so happens to be 2*Area.
However, in this case the problem is actually the input domain: a mesh of triangles. That means almost every vertex borders on two triangles. It's not like "point P lies on the left of edge E, so it's not in triangle T". There are a large set of triangles Ti, some of which lie on the left, some of which lie on the right, and one directly on either side of a given edge.
Given that complexity, you should pre-process the mesh. Partition it in some manageable chunks, e.g. 16x16, and note for each triangle in which chunks it lies. Any point P lies in exactly one chunk, so you need to test perhaps 1% of triangles (a single triangle may lie in multiple chunks, but the average is low).
(You rarely if ever need to do just a single point-to-mesh match, so preprocessing is justified. And pre-calculate the area while you're at it.)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm doing an 2D bin packing algorithm. The goal is to put a set of rectangle into a bin one by one.
At each rectangle insertion, i want to update the boundary between occupied area and free area. Thus, i'm looking for an algorithm or the way to do it. Algorithm must be able to:
1) Find the boundary after rectangle insertion (or find all points of the boundary).
2) Travel clockwise all the points of the boundary (imagine that now I have all points coordinates of the boundary).
3) At each corner (point) of the boundary, the algorithm can determinate if this point is on the top left, top right, bottom left, bottom right
Any help would be greatly appreciated. If you need more information, just ask and I'll provide all I can.
Thank you
See these links:
http://en.wikipedia.org/wiki/Bin_packing_problem
How is 2D bin packing achieved programmatically?
https://math.stackexchange.com/questions/352575/2d-bin-packing-problem-with-opportunity-to-optimize-the-size-of-the-bin
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have a system of mono-disperse spheres inside a cubic box. I am studying the volume distribution inside the sample, after tessellating it with either Voronoi and Delaunay tessellations. I am interested on some properties which should not depend on the tessellation.
Currently, I am comparing with the values obtained from Voronoi and Delaunay. I would like to know if you are familiar with another space partition approach (It is important that the final sum of the individual cells add up to the total volume, and the cells should be disjoint). Furthermore, in case you know another kind of tessellation, do you also know a library which already implements it, preferable in C/C++ or python?
Some variations, like Laguerre partitions, coincide with my current Voronoi approach since the spheres are mono-disperse. Another candidate will be the Centroidal Voronoi tessellation, although I have not found yet a library to do that (although it could lead to evenly spaced cells which does not reflect the disorder inside the system, which is not desirable).
Thanks in advance for your kind help.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How can i calculate the area of a polygon in c++ only by knowing the x and y coordonates of the points which make the polygon?
A simple google search shows the answer provided that you are dealing with non-self-intersecting polygons. The sign of the area is positive if the points on the polygon are arranged in counterclockwise order. This formula does not assume that the polygon is convex.
http://mathworld.wolfram.com/PolygonArea.html
Here, the area is found by summing the determinent of neighboring points. Each determinent computes the area of the parallelogram formed by the vector e.g. (x1,y1) and (x2,y2) (where both vectors stem from the origin (0,0)). The division by 2 gives the area of a triangle. When traveling around the polygon, the triangles will have a positive area if your polygon is convex. Otherwise, negative areas of these triangles will cancel with their positive counterparts for the case of a concave polygon giving you the correct result.
Simple wikipedia search shows the answer:
http://en.wikipedia.org/wiki/Polygon#Area_and_centroid
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Consider N chords in a circle, each determined by its endpoints. Describe an O(nlogn) solution for determining the number of pairs of chords that intersect inside the circle.
ASSUMPTION: No two chords share an endpoint.
There exists a general line-segment intersection algorithm which does the job in O(nlogn).
This can be used in your case as two chords can't intersect in the exterior of a circle.
The following link contains the algorithm:
http://www.cs.princeton.edu/~chazelle/pubs/IntersectLineSegments.pdf
P.S.
It requires knowledge of basic computational geometry (line sweeps, range trees).
Hope this helps.
Off the top of my head, sort the chord endpoints by polar angle (this is the O(n log n) part). Then read through the sorted list (which is O(n)) - if two adjacent endpoints belong to the same chord, it has no intersections. Where two adjacent entries in the list belong to different chords, there may be an intersection depending on where the other endpoints for those two chords lie - e.g. if a chord A has endpoints A1 and A2 in their sorted order, and similarly chord B has B1 and B2, finding B2-A1 in the list is not an intersection, because B1 is earlier and A2 is later. However, B1-A2 would be an intersection.
See also biziclop's comment for another, somewhat more carefully constructed, solution.