Finding intersection points of line paths - algorithm

I have two Bezier curves which share an end point. Each of these curves has an "extension" on both the left and right sides, similar to the edges of a road. The extensions are made of line segments that approximate the Bezier curve.
I want to find the closest intersection point of these paths to the shared end point of the bezier curves.
Here is a diagram I've drawn of the problem
Each line path has over 100 vertices, so intersecting each line and keeping the closest intersection point can become very slow, given that this must run in real-time.
I've run a bounding sphere intersection test on the lines before checking for an intersection point to speed things up a little, but it's still not quick enough. My next approach would be to use some sort of quadtree structure.
I've looked up the Bentley-Ottmann algorithm but it seems to deal with finding all intersections in one set of lines, which isn't what I need. I've also looked up Bezier curve intersection algorithms but they seem to require subdivision into line segments, which I already have.
Are there any useful algorithms for this problem, or perhaps any ideas on how it might be optimised?

Given two curves A and B with extension widths Aw and Bw. Find the point A' which is the distance Bw along A from the shared node N. Find the point B' which is likewise the distance Aw along B from the shared node N. Now, given the points N, A' and B', find the fourth point N' which forms a parallelogram with the other three nodes. This point N' is the intersection.

Related

calculate intersection area of two triangle

I have been trying to find an algorithm which computes the intersecting area of two triangles but I failed to find any. Can anybody give a clue how to write this algorithm?
I would like something like:
double getAreaOfIntersection(Vector2 p1,Vector2 p2, Vector2 p3,Vector2 p4,Vector2 p5,Vector2 p6 )
where pX represents the 2 triangles.
You could first compute the polygon which describes the intersection area by a clipping algorithm, e.g.:
Sutherland-Hodgman algorithm
Then you would compute the area of the resulting convex polygon, which is rather easy, see, e.g., here:
Area of a Convex Polygon
Determining wether or not a point lies within a given polygon is easy (and even easier for triangles since those are simple polygons). You can use the winding number algorithm (and the crossing number algorithm for simple polygons) which is implemented and well explained here.
Using this you can obtain all the vertices of your intersection polygon:
The vertices pX of a triangle that are contained in the other triangle as well
The points where the two triangles intersect (see intersection of line segments)
You will need to loop over your edges to find all the intersection points, so this should be quick enough as long as you only want to determine intersections of triangles but i would not suggest to try to find intersections of arbitrary polygons this way.

Cut the Cake, or polygon decomposition

I'm facing the following problem: I'm given a set of coordinates on an integer grid that define the vertices of a polygon. The polygon is guaranteed to be convex. It's proven that such a polygon can always be cut into 4 equal area parts by 2 orthogonal lines. Let's call the point of these lines' intersection P.
Given that set, I should calculate the coordinates of P within the polygon and the angle the lines need to be turned on so that the lines cut the polygon into 4 equal parts.
I realise that, put generally, the cake cutting problem has no "good" solution. But this particular case of it should.
I've searched for an algorithm to solve that problem, but found nothing useful.
Where should I look?
My approach would be to calculate the coordinates of the centre of the polygon (that can be done more or less easily), place Pthere and then "wiggle" the lines until the areas of the parts match. But that sounds too inelegant.
UPD: that's the problem I'm dealing with. Perhaps this question should be suspended until I come up with actual code questions.
Here is a partial sketch of the solution:
Choose an arbitrary direction and find the line parallel to that direction that splits the polygon in two. To achieve this, draw a line by every vertex to decompose the polygon in slabs. The respective areas of the slabs will tell you what slab the desired line intersects. Simple linear interpolation will give the exact location of the line.
Now your polygon is split in two convex polygons. For each halve, repeat the above procedure using the perpendicular direction. In general, you will get two distinct splitters, and what remains to be done is to find the direction such that they do coincide.
In the given direction, the splitters intersect four specific edges of the polygon. If you slightly rotate, they still intersect the same four edges. You can decompose a full turn in angular ranges such that the four intersected edges remain the same.
Knowing the four intersected edges, you can establish the relation that tells you the distance between the two perpendicular splitters as a function of the angle. Then you can compute the angle at which the two splitters coincide, and check if this angle belongs to the range defined for these edges.
By trying all ranges in turn, you will find the solution.
Note: the limits of the angular ranges correspond to directions parallel or perpendicular to the lines joining two vertexes.

Finding intersection between sets of points describing a curve

Say I have two sets of points
p1, p2, p3,... ,pn
and
q1, q2, q3,..., qn
which describe two paths (curves) in a plane. The points may not be evenly sampled from the curve, but they are "in order" (with regard to parameterizations of the curves). What is a good way to find out where these two curves intersect?
So for example, I may have just two points each
(0,0) (1,0)
and
(-5,1) (-4,-1)
in which case their intersection is (-4.5,0).
The most rudimentary way to do this would be to draw the edges between every two points, extend them, and see whether any two pairs of edges intersect in a suitable patch of land. I'm curious if there's a better way.
The most efficient way to find such intersection is by means of sweepline algorithms, that can achieve O(n log n + k) running time (n line segments having k intersections), better than the O(n²) by exhaustive comparisons. See http://www.ti.inf.ethz.ch/ew/lehre/CG09/materials/v9.pdf. Unfortunately, such solutions are rather sophisticated.
A possible alternative, much simpler to implement, is to use hierarchichal bounding: take the bounding box of every segment, merge the boxes two by two (consecutive segments), then four by four and so on. starting from N segments, you'll form hierarchy of N-1 bounding boxes.
Then, to intersect two curves, check interference of their top-level bounding boxes. If the do overlap, check interference of the sub-boxes, and so on recursively.
Unless your curves are closely intertwined, you can spare a large number of segment comparisons.
You can preprocess each polyline (chain of segments) and find a minimal bounding rectangle for each of them. Also you can build a hierarchical data structure for each polyline - a rectangle for the whole one, then a rectangle for each half and so on. You can use other geometrical forms instead of rectangle - circle or ellipse, for instance.
Then you can use Clipping and Culling to accelerate intersections search.
You can calculate bounding box around a set of points, say every 100 pair of points and intersect only those in a n x n manner. Bounding box intersections can be done very efficiently. If two bounding boxes (one from each curve) intersect, you can test for intersection just the edges involved inside of those boxes.
This will handle the case when there's more than one intersection between the curves. Just mind the boundary cases, when the point of intersection is actually one of the vertices defining an edge.

How do you implement a program to find the shortest path in a 2d plane?

If on a 2d plane there are a no. of obstacles of all possible 2d shapes(circles, quadrilaterals, triangles, irregular shapes...) then how do you implement a mechanism to find the shortest path around the obstacles? I'm considering visual c++, as it provides many graphical classes to draw such figures.
I have come quite far
1) Firstly i'll be using A* search(A-star) to find the path with least cost
2) The path with the least displacement from the straight path will be considered for best path. (not really sure though)
3) The shortest path to get around a figure, for eg from the start, is a line from that point to :
a) the farthest vertex in case of a polygon/quadrilateral
b) a point on the circumference such that the line drawn would be tangential to the circle, in case of a circle or arc
c) (not sure about irregular figures)
Now coming back to the 2) point- least displacement between 2 or more paths can be determined by comparing perpendiculars from those lines to the farthest points of an object on their respective sides. (hope i've made myself understood) .
So then- how do we draw perpendiculars to the straight path?
x1,x2,y1,y2,k and l are known. We just have to find a,b.
Slope of the straight path * slope of it's perpendicular = -1
=> (y2-y1)/(x2-x1) * (b-l)/(1-k) = -1
hence, b = [(x1-x2)/(y2-y1) * (a-k)] + l
I've imagined that by using pythagoras theorem we can find the other equation in terms of the co-ordinates. The lengths of each line can be found by this way:
dx = x1-x2
dy = y1-y2
dist = sqrt(dxdx + dydy)
And then by solving these 2 eqns we can find the correct values of a,b.
I can't think of anything further- any ideas or suggestions?
Is it possible that you use polygonal (i.e. straight line segment) approximations for all shapes?
This would simplify the implementation of the algorithm a lot.
Assuming that this is indeed possible: If you want to use A* then you'll need a graph representation
of the possible paths that you can take. The nodes of this graph are the combination of:
all the vertices of all shapes[1], and
the start and end points
all intersection points between {the straight line segment between the start and end point} and all shapes.
The edges in this graph, then, are between each pair of nodes only if
there exists a straight line between their corresponding two vertices
that doesn't intersect any of the shapes[2].
The length of each edge in the graph is then simply the (euclidean) distance between the two vertices it represents, and the shortest path is always a subset of these edges (I think), which you can find by applying A* to this graph.
[1] - To reduce the number of vertices, you can make all concave shapes convex (unless this causes the start
or end point to lie inside such a shape, then it should be kept concave).
[2] - You can use a variety of data structures to speed up these queries, such as kD or quad trees, or maybe use a sweep line algorithm (such as http://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm) in combination with a doubly-connected edge list.
Well i am not pretty sure if this might help but anyways every irregular object can be split into a combination of regular objects like a circle just that the radius of the curve keeps changing.So you can consider it to be a combination of arcs corresponding to different radii.
For the second point if have the points (l,k). Consider two points located on the line(x1,y1),(x2,y2) which are equidistant from(l,k). So the perpendicular will be the combination of all points which are equidistant from (x1,y1)and (x2,y2).

rectilinear polygon intersection

I am looking for / trying to develop an optimal algorithm for rectilinear polygon intersection with rectangles. The polygons I am testing do not have holes.
Answers like those given here and here are for very general polygons, and the solutions are understandably quite complex.
Hoping that the S.O. community can help me document algorithms for the special cases with just rectilinear polygons.
I am looking for the polygon filled in green in the image below:
The book Computational Geometry: an Introduction by Preparata and Shamos has a chapter on rectilinear polygons.
Use a sweep line algorithm, making use of the fact that a rectilinear polygon is defined by its vertices.
Represent the vertices along with the rectangle that they belong to, i.e. something like (x, y, #rect). To this set of points, add those points that result from the intersections of all edges. These new points are of the form (x, y, final), since we already know that they belong to the resulting set of points.
Now:
sort all points by their x-value
use a sweep line, starting at the first x-coordinate; for each new point:
if it's a "start point", add it to a temporary set T. Mark it "final" if it's a point from rectangle A and between y-coordinates from points from rectangle B in T (or vice versa).
if it's an "end point", remove it and its corresponding start point from T.
After that, all points that are marked "final" denote the vertices of the resulting polygon.
Let N be the total number of points. Further assuming that testing whether we should mark a point as being "final" takes time O(log(n)) by looking up T, this whole algorithm is in O(N*log(N)).
Note that the task of finding all intersections can be incorporated into the above algorithm, since finding all intersections efficiently is itself a sweep line algorithm usually. Also note that the resulting set of points may contain more than one polygon, which makes it slightly harder to reconstruct the solution polygons out of the "final" vertices.

Resources