Quadrilateral Intersection 4 points - c#

Rectangle Possibilities
I am trying to figure out if 2 rectangles that are rotated are intersecting.
The image(Rectangle Possibilities) shows examples of intersecting rectangle possibilities. The information that I know about each rectangle is the 4 vertices.

Rotate the two rectangles so that one becomes axis parallel, and check non-interference of the two axis-aligned bounding boxes. Then repeat with the second. This is necessary and sufficient to guarantee no overlap.

Powerfule method of checking whether two convex polygons do intersect is using Separating Axes Theorem. Yet more description.
It is rather simple (and fast) for rectangles.

Related

Finding the specific outline of a set of points

I have a set of "blocks" (denoted by the red and green lines) that are placed inside a "container" (denoted by the blue lines).
All of the intersection points of the blocks (green and red dots) and all relevant information of the container (angle, gradient, start,end points, etc.) are known.
I want to extract the "top-most" outline of the resultant figure after the blocks are placed (denoted by the green lines and dots).
I tried to use methods such as convex hull (shown by purple lines in the following diagram), but it does not give the exact lines.
My question is can anyone point me to a solution or some kind of algorithm that I can apply to solve these type of problems?
Fill the list (array ) with all points. (Repeat points in T-nodes like 2nd green point at your picture)
Sort this list by Y-coordinates
Scan list (starting from top points) like sweep line algorithm.
At every stage you'll get a pack of points with the same Y-coordinate (a pair or more).
Remove points covered be intervals (see below) both from left and from right.
Make intervals (by X-coordinates) from pairs of these points.
Add these intervals in interval (segment) tree.
Join neighbor intervals.
Repeat until single interval cover all the top part.
The convex hull should do the job.
You can check: Fast and improved 2D Convex Hull algorithm and its implementation in O(n log h) for many convex hull algorithms.
As soon as it touch red points, you get before and after.

Find overlapped area between a circle and a group of polygons

I found some solutions to find the intersection between the circle and 1 triangle.
I am searching for a solution to find a more general solution, to find the area of the circle overlapped by the polygons present in the plan.
No 2 polygons intersect each other nor self-intersect.
A circle can be intersected (overlapped) by multiple polygons.
I would appreciate a C# solution.
Triangulate your polygons.
For each triangle i, compute the intersection area of your circle and triangle i. Call it A[i].
(You said you know how to do this part.)
The area of the intersection between your circle and your polygons is sum(A[i] for all triangles i).

Algorithm to compute the remaining polygon after subtraction

I have a big polygon (Pa). Inside the polygon there are a lot of small "holes", as shown:
Here are a few condition for the holes:
The holes cannot overlap one another
The holes cannot go outside the outer polygon
However, the holes can touch the outer polygon edge
How to obtain the remaining polygon ( or the polygon list) in an efficient manner? The easiest way ( brute force way) is to take the Pa, and gradually computing the remaining polygon by subtracting out the holes. Although this idea is feasible, but I suspect that there is a more efficient algorithm.
Edit: I'm not asking about how to perform polygon clipping ( or subtraction) algorithm! In fact that's something I would do by brute force. I'm asking in addition to the polygon clipping method ( take the main polygon and then gradually clip the holes out), is there other more efficient way?
This is very hard to do in a general manner. You can find source code for a solution here:
General Polygon Clipper (GPC)
Well, if you use the right representation for your polygon you would not need to do anything. Just append the list of edges of the holes to the list of edges of Pa.
The only consideration you should have is that if some hole vertex or edge can touch Pa edge, you will have to perform some simplification there.
A different problem is rendering that polygon into a bitmap!
You can do like this.
Draw the main polygon with a color in a bitmap.
Draw the holes with another color in the same bitmap.
Then extract the polygon by running marching square algorithm with the main polygons color as threshold.
The output will contain all the points that belong to that polygon.
You can sort the points if you want it as a continous closed polygon.
I agree with salva, but my post is going to address the drawing part. Basically, you can add up all lines of the main and the hole polygons together and thereby get a single complex polygon.
The algorithm itself is not very complicted and it is nicely explained in the Polygon Fill Teaching Tool.

Subdividing 3D mesh into arbitrarily sized pieces

I have a mesh defined by 4 points in 3D space. I need an algorithm which will subdivide that mesh into subdivisions of an arbitrary horizontal and vertical size. If the subdivision size isn't an exact divisor of the mesh size, the edge pieces will be smaller.
All of the subdivision algorithms I've found only subdivide meshes into exact powers of 2. Does anyone know of one that can do what I want?
Failing that, my thoughts about a possible implementation is to rotate the mesh so that it is flat on the Z axis, subdivide in 2D and then translate back into 3D. That's because my mind finds 3D hard ;) Any better suggestions?
Using C# if that makes any difference.
If you only have to work with a rectangle in 3D, then you simply need to obtain the two edge vectors and then you can generate all the interior points of the subdivided rectangle. For example, say your quad is defined by (x0,y0),...,(x3,y3), in order going around the quad. The edge vectors relative to point (x0,y0) are u = (x1-x0,y1-y0) and v = (x3-x0,y3-y0).
Now, you can generate all the interior points. Suppose you want M edges along the first edge, and N along the second, then the interior points are just
(x0,y0) + i/(M -1)* u + j/(N-1) * v
where i and j go from 0 .. M-1 and 0 .. N-1, respectively. You can figure out which vertices need to be connected together by just working it out on paper.
This kind of uniform subdivision works fine for triangular meshes as well, but each edge must have the same number of subdivided edges.
If you want to subdivide a general mesh, you can just do this to each individual triangle/quad. This kind of uniform subdivision results in poor quality meshes since all the original flat facets remain flat. If you want something more sophisticated, you can look at Loop subidivision, Catmull-Clark, etc. Those are typically constrained to power-of-two levels, but if you research the original formulations, I think you can derive subdivision stencils for non-power-of-two divisions. The theory behind that is a bit more involved than I can reasonably describe here.
Now that you've explained things a bit more clearly, I don't see your problem: you have a rectangle and you want to divide it up into rectangular tiles. So the mesh points you want are regularly spaced in both orthogonal directions. In 2D this is trivial, surely ? In 3D it's also trivial though the maths is a little trickier.
Off the top of my head I would guess that transforming from 3D to 2D (and aligning the rectangle with the coordinate axes at the same time) then calculating the mesh points, then transforming back to 3D is probably about as simple (and CPU-time consuming) as working it all out in 3D in the first place.
Yes, using C# means that I'm not able to propose a code to help you.
Comment or edit you question if I've missed the point.

Building an "inner stroke" effect from a Polygon

I'm drawing a whole bunch of Polygons onto a canvas, most of which share an edge with at least one other Polygon. I'd like to indicate a Polygon is "special" by outlining it, however due to the overlapping edges the Stroke on a Polygon tends to be partially drawn over by another Polygon causing the Stroke to look thinner than it should in places. Additionally, depending on draw order, a Polygon may have its Stroke almost entirely covered by those around it. Spacing the Polygons out is not a very attractive option, as visible gaps between Polygons are much less preferable to this "thin stroke" problem.
I reason that the effect I'm looking for can be achieved by generating each Polygon as a pair of Polygons instead, such that one Polygon is completely contained within the other and this smaller Polygon's Stroke runs up to (but does not overlap) the outer Polygon's Stroke. The inner Polygon would have a transparent Fill at all times, and a non-transparent Stroke only when I wish to indicate that the now pair of Polygons are "special".
My question boils down to; how can I derive such an inner Polygon from the outer one, or failing that how else might I achieve this effect?
The Polygon's in question can be constrained to the regular polygons if need be, and the Strokes are simple 1 thickness solid lines with no fancy miters or the like. I'd like to be able to adjust the thickness of the Stroke at some point, but nothing fancier than that.
I think there's actually a few ways to achieve what you want.
First, going with your original idea of drawing a second, slightly smaller polygon within the original polygon; it's not a bad idea. To answer your original question as to how to generate the smaller polygon: you can find the centroid of each polygon, and offset each vertex by a percentage of the distance to that centroid point of the polygon to get the smaller polygon. This should give a pleasing effect.
Another way you could go would be to use z-order, and offset the "thicker" polygon by a slight negative z-distance.
Yet another way to go would be to exploit draw order; draw your thicker polygons last, which will prevent them from being overdrawn by the surrounding polygons.
Yet another idea that might work depending on your needs might be not to draw polygons at all, but to draw a series of lines, simply thickening some of the lines.
There's a lot of options for you to try; hope you find one that works out well!

Categories