Intersection points between a long list of segments - matlab

I'm writing a Matlab program that has to do the following thing I'm stuck with.
I have a file containing the names of two points on every line (the points have its respective coordinates in the space). Let's say the segments are airways and the points are airports.
I have plotted all the airways, and obviously some of them intersect at some point. I would like to draw a cross 'X' at those points of intersection. How can I do this? Is there any already-implemented Matlab function that allows me to find the intersection points of segments?
And another question related to this, provided the file (with about 100 lines, where in every line a segment is defined by two points) should I do a loop that checks all the intersecting points of the first segment with the other 99 segments, and then the second segment with the other 98 segments? Or there is a more efficient way in order to do that? Thank you!!!

Related

Find co-ordinates where LineString intersects a Polygon border in turfjs

Is there a way in Turfjs to determine the co-ordinates at which a LineString intersects with the border of a polygon?
There's a number of ways to find out if a point is within a polygon and a number of ways to find out if a point is on a line and so on, but I can't seem to figure out a way to ask "at what point does this line intersect this polygon's border".
I could enumerate the points in the polygon using a line intersection algorithm to find that point but I was wondering if there's a more "turf" way of doing this.
For context, I've loaded a GPX track and want to estimate the location/time at which the track enters/exits a defined area.
Because a GPX track only records locations at specific intervals it usually the case that pN recorded at time tN is outside the area and pN+1 recorded at time tN+1 is inside the area.
If I can get the point at which line (pN, pN+1) intersects the polygon's border I can estimate the exact time the track crosses into the polygon.
Ultimately, turfjs does not seem to have an API for doing this.
I was able to get the answer I wanted by enumerating the points in the polygon from the GeoJSON object to construct a sequence of line segments and then I used maxogden/geojson-js-utils linesIntersect function to test for intersection points.
I don't see a Turf function that does exactly that, but there is intersect, which finds the area of intersection between two polygons.
You could:
Construct a polygon by joining the line to itself reversed (so ABC becomes ABCBA)
Find the intersection of ABCBA and P, the original polygon using Intersect.
The intersection should be a zero-area polygon that is the part of ABCBA inside P. Somehow compute the length of it (strangely there's no perimeter function).
Subtract that length from the original length of ABC.
Not exactly elegant, true. :)
EDIT
Tried this. Turns out Turf-intersect doesn't return intersections if one of the polygons is zero-area.

how to perform hough transformfor finding hand curve

hi i want to detect fingertips point and valleypoint of hand by using hough transform.Simply the Question is what is the [H,theta,rho]=hough(BW) is good for extract these point.
the image is here:
https://www.dropbox.com/sh/n1lz7b5eedzbui7/AADwy5O1l7sWf5aOx7KWAmhOa?dl=0
tnx
The standard hough transformation is just for detecting straight lines. Not more and not less. The Matlab function hough (please see here) returns the so-called hough space H, a parametric space which is used to find these lines and the parametric representation of each line: rho = x*cos(theta) + y*sin(theta).
You will have to do more than this to detect your desired points. Since your fingers usually won't consist of straight lines, I think you should think of something else anyway, e.g. if you can assume such a perfect curve as the one in your image maybe this is interesting for you.
Another simple technique you might consider is to compare the straight line distance between two points on your hand line to the distance between those two points along the perimeter (geodesic distance). For this you would need an ordered list of points along the perimeter.
Along regions of high curvature, the straight line distance between two points will be smaller than the number of pixels between those two points along the perimeter.
For example, you could check perimeter pixels separate by 10 pixels. That is, you would search through the list and compare the point at index N and the point index N+10. (You'll need to loop back around to the beginning of the list as you approach the end.) If the straight line distance between these two points is nearly 10 pixels as well, then you know those points lie on a straight section of the perimeter. If the straight line distance is much smaller than 10, then you know the perimeter curves in some fashion between those points. Whether you check pixels that are 5, 10, 20, or 30 items apart in the list will depend on the resolution of your image and the curves you're looking for.
This technique is useful because it's simple and quick to implement. Maybe it would work well enough for your needs.
Yet another way: simplify the outline to small line segments, and then you can calculate the line-line angle between adjacent segments. To simplify the curves, implement the Ramer-Douglas-Puecker algorithm. A little experimentation will reveal what parameter settings will work for your application.
https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
Finally, you could look into piecewise curve fitting: a curve would be fitted to small segments of the outline. This can get very complicated, and researchers continue to find ways to decompose complex figures into a limited number of more basic shapes or curves. I recommend trying the simplest technique and then only adding complexity if you need it.

Intersection of segment with polygon

I have to create a function in MATLAB that performs the following task:
Input:
p polygon in the form
p = [x1,y1; x2,y2; x3,y3; x4,y4...]
s struct with the segment from A to B
s = struct('A',[x,y],'B'[u,w])
Return:
1) An integer indicating how many intersections there are between the segment and the polygon (e.g., 0,1,2)
2) A new segment from A to B, where A is the first intersection or the initial point of the input segment and B the second point of the intersection or the last point of the segment input.
I have an idea on how to do it by using the function inpolygon. I have been reading how to use this function, and know that to use that, I should provide a query point and the coordinates of the polygon vertices. It will return 1 or 0 depending on whether it is inside or not.
My question is, how can I get the query point of the segment that is placed exactly in the boundary (in the case that the segment intersects with it)?
If you have the Mapping Toolbox installed, you could use polyxpoly. As this is a rather basic problem, there are quite a few free MATLAB-codes out there on the File Exchange. Here is what I found for the search term 'polygon intersect':
2D Polygon edges intersection by Bruno Luong
Find the intersection points of the edges of two 2D polygons, a simple function made to follow up a Newsgroup discussion
Curve Intersect 2 by Sebastian Hölz
This file is based on the Curve Intersect function by Duane Hanselman. It extends the scope of the function to handle arbitrary lines / polygons, which may also have vertical segments or segments with non-increasing x-values.
Curve intersections by NS
While a few other functions already exist in FEX that compute the
intersection points of curves, this short piece of code was written
with speed being the highest priority. No loops are used throughout,
taking full advantage of MATLAB's vectorization capabilities
Fast and Robust Curve Intersections by Douglas Schwarz
This function computes the (x,y) locations where two curves intersect. The curves can be broken with NaNs or have vertical segments. It is also very fast (at least on data that represents what I think is a typical application).
geom2d by David Legland
[...] derive new shapes: intersection between 2 lines, between a line and a circle, parallel and perpendicular lines

Fit several connected lines to points

I have an 2d-image and I want to fit several lines to the object that is represented by this image. The lines are connected and can only have angles in certain intervals between each other.
I know, that you can fit one line to data points using least squares. But I do not know how to fit several connected lines simultaneously to points while at the same time obeying the angle intervals.
How would you solve this problem programmatically? I would also accept an answer, given me catchwords (and maybe links) that will point me to my solution.
Here is an example image. For instance, I might want to fit 4 lines with length x,y,z,w to the object represented by the largest component in the image. Unfortunately, the object is not always as clearly visible as it is here, but this will do for now :)
Green lines approximate lines I would be looking for (sorry, they are not very straight ;) ).
You can fit a degree 1 B-spline curve to data points extracted from your image. A degree 1 B-spline curve is conceptually a composition of multiple line segments, which matches what you want. Additional angle constraints between lines can be imposed onto control points of this degree 1 B-spline curve but doing so will turn your unconstrained fitting into a constrained one, which will increase the complexity of algorithm.

Drawing a bezier from multiple points

I have a set with 50 points in x,y. I need to draw the smoothest bezier that passes in all points, or in other words, the bezier that will best fit the points.
How do I do that? thanks
I am undergoing a similar problem in 3D. It is slightly easier in 2D because lines will always intersect if not parallel.
Firstly, read up on quadratic bezier curves. Each curve is represented by three points. The line will not pass through the middle point. Thus, your middle point cannot be one of the points you are trying to fit, or it won't go through it.
Instead, the beginning and end point of your quadratic bezier curve must be two consecutive points you want it to pass through. So what is your middle point going to be?
One way of solving this (never tried it myself HENCE it might not look perfect, but Im thinking off the top of my head) is to calculate the tangents from your -1st data point to your 0th data point, and find the intersection between that and the 1st data point to the 2nd data point. Then draw the line between the 0th data point and the 1st data point using this intersection as the middle bezier curve value.
Obviously you may have trouble at the ends of the curves, that may require some inventive thinking to make them look good. (the first point has no -1st point).
Sorry about the lack of diagrams. I would draw one but I'm on an iPad.
Imagine 3-point bezier curve (start-A, middle-B, end-C)
Imagine a straight line from A to C.
Imagine a straight line that is perpendicular to AC and goes through point B.
Those two lines cross in point D.
Bezier curve will go through EXACTLY half way from D to B. In other words if you want bezier curve that goes through 3 points, you must make the second point 2 times further from start and end than the actual second point.