Suppose you have an arbitrary closed curve (endpoint returns relatively close to first point) generated through a bunch of dataset coordinates, how do you find the centerpoint and the boundaries of the resulting shape?
There are two possible interpretations (perhaps more) for your question.
The first one was already addressed by #AakashM, and we may depict it in the following plot:
Where the red square is the "boundary".
I'll cite #AakashM here, because I understand his remark VERY important:"(I note that for you to have a closed curve, you need the endpoint to be not just 'close to', but coincident with the first point)"
As for the centerpoint, you have at least two "natural ways" for calculating it with this definitions:
Centerpoint = Middle Point of the Red Square
Centerpoint = { Mean of x coordinates of your curve, Mean of y coordinates of your curve}
Both of them may serve as a center point, but the results will be different.
The other way of dealing with the problem is finding the Convex Hull of your curve, as depicted below:
If you google for it, you will find algorithms for finding the Convex Hull, a nice introduction is here.
Again, you have two "natural ways for calculating the centerpoint:
Centerpoint = { Mean of x coordinates of your curve, Mean of y coordinates of your curve}
Centerpoint = { Mean of x coordinates of the CH points, Mean of y coordinates of the CH points}
HTH!
(I note that for you to have a closed curve, you need the endpoint to be not just 'close to', but coincident with the first point)
If by 'centerpoint' you mean center of mass, and you are assuming uniform density, then this question has what you want.
If by 'boundaries' you mean bounding rectangle with sides parallel to the axes, you just need the minimum and maximum x and y values on the curve.
If either of those aren't what you mean, please say...
For the boundaries, you can refer to the answers given by #belisarius and #AakashM.
As for centerpoint, you want "center of mass". Good 'ol Wikipedia has explanations and recipes at http://en.wikipedia.org/wiki/Center_of_mass and http://en.wikipedia.org/wiki/Centroid.
In general, you get a different result calculating the centroid than calculating the average of the vertices. This difference will be pronounced if the vertices are not uniformly distributed.
Related
I am dealing with a reverse-engineering problem regarding road geometry and estimation of design conditions.
Suppose you have a set of points obtained from the measurement of positions of a road. This road has straight sections as well as curve sections. Straight sections are, of course, represented by lines, and curves are represented by circles of unknown center and radius. There are, as well, transition sections, which may be clothoids / Euler spirals or any other usual track transition curve. A representation of the track may look like this:
We know in advance that the road / track was designed taking this transition + circle + transition principle into account for every curve, yet we only have the measurement points, and the goal is to find the parameters describing every curve on the track, this is, the transition parameters as well as the circle's center and radius.
I have written some code using a nonlinear optimization algorithm, where a user can select start and end points and fit a circle that to the arc section between them, as it shows in next figure:
However, I don't find a suitable way to take the transition into account. After giving it some thought I came to think that this s because, given a set of discrete points -with their measurement error- representing a full curve, it is not entirely clear where to consider it "begins" and where it "ends" and, moreover, it is less clear where to consider the transition, the proper circle and the exit transition "begin" and "end".
Is there any work on this subject which I may have missed? is there a proper way to fit the whole transition + curve + transition structure into the set of points?
As far as I know, there's no method to fit a sequence clothoid1-circle-clothoid2 into a given set of points.
Basic facts are that two points define a straight, and three points define a unique circle.
The clothoid is far more complex, because you need: The parameter A, the final radius Rf, an initial point px,py, the radius Ri at that point, and the tangent T (angle with X-axis) at that point.
These are 5 data you may use to find the solution.
Due to clothoid coords are calculated by expanded Fresnel integrals (see https://math.stackexchange.com/a/3359006/688039 a little explanation), and then apply a translation & rotation, there's no an easy way to fit this spiral into a set of given points.
When I've had to deal with your issue, what I've done is:
Calculate the radius for triplets of consecutive points: p1p2p3, p2p3p4, p3p4p5, etc
Observe the sequence of radius. Similar values mean a circle, increasing/decreasing values mean a clothoid; Big values would mean a straight.
For each basic element (line, circle) find the most probably characteristics (angles, vertices, radius) by hand or by some regression method. Many times the common sense is the best.
For a spiral you may start with aproximated values, taken from the adjacent elements. These values may very well be the initial angle and point, and the initial and final radius. Then you need to iterate, playing with Fresnel and 'space change' until you find a "good" parameter A. Then repeat with small differences in the other values, those you took from adjacents.
Make the changes you consider as good. For example, many values (A, radius) use to be integers, without decimals, just because it was easier for the designer to type.
If you can make a small applet to do these steps then it's enough. Using a typical roads software helps, but doesn't avoid you the iteration process.
If the points are dense compared to the effective radii of curvature, estimate the local curvature by least square fitting of a circle on a small number of points, taking into account that the curvature is most of the time zero.
You will obtain a plot with constant values and ramps that connect them. You can use an estimate of the slope at the inflection points to figure out the transition points.
I am trying to draw a star on a sphere. To determine the points belonging to the star I need to count the number of halfspaces that the points on the sphere belong to out of the 5 halfspaces defined by alternating points of the regular pentagon drawn on the x-y plane. My problem is determining the equations of those planes. I know 2 points, 2 alternating points of hexagons: (v0, v2), (v0, v3), (v1, v3), (v1, v4), (v2, v4). Assuming all planes are parallel to the z-axis, it seems to me intuitively this is enough info to find the plane equation, but my math is a little rusty and cannot do it. Appreciate any leads of how to calculate the equations or pointing out the flaw in my assumption...
If you're going to draw a star an a sphere, it will probably look better if the arcs are geodesics. That makes it easier for you, since the planes that define those arcs (by intersecting the sphere) will then all pass through the sphere's center.
Finding those planes is easy, too. You just take the cross product of the vectors from the center to any two points to find the normal. If the sphere's center is at (0,0,0), you don't need anything else.
Bezier curve is a parametric curve, meaning that there is a paramater t at which one can evaluate the polynomials in order to find out the positions of points laying on the curve.
Polynomials for some common cases can be found at en.wikipedia.org/wiki/B%C3%A9zier_curve#Specific_cases
To draw a Bezier curve on screen, one could evaluate the polynomials from 0 to 1 at ever increasing t by tiny little steps. However, that would very wasteful because, in general, the parameter "space" does not correspond to screen "space", i.e. several little steps may fall onto just one pixel.
My question is: how to find the smallest step which increases Cartesian distance from previous point at least by 1 pixel?
To put it in other way: I would like to draw a Bezier curve on screen. How to choose the (uniform) step by which t should grow so that I never draw at one pixel more the once? I don't mind the "holes" when the t grows too quickly, I just don't want to redraw already drawn pixels.
Edit
By "how to find" I mean O(1). Yes, I could use De Casteljau's algorithm but I was hoping there is a way to "guess" the optimal t step quickly.
The comment above (jozxyqk) gives you a hint. I would give it a try with a recursive binary division of the spline drawing.
Lets say you start with a coarse resolution of the parameter space (delta_t = 0.1), that gives you 11 points on the spline curve s , s(t=0), s(t=0.1), ..., s(t=0.9), s(t=1).
Calculate the distance between s(t_i) and s(t_i+1). If it is >1 than make a binary subdivision between those two points. And so on...
But honestly, I guess it is faster to calculate all points a a higher resolution without any recursive loops or subdivisions. Especially if you are using multithread programming.
I'm trying to code the Ritter's bounding sphere algorithm in arbitrary dimensions, and I'm stuck on the part of creating a sphere which would have 3 given points on it's edge, or in other words, a sphere which would be defined by 3 points in N-dimensional space.
That sphere's center would be the minimal-distance equidistant point from the (defining) 3 points.
I know how to solve it in 2-D (circumcenter of a triangle defined by 3 points), and I've seen some vector calculations for 3D, but I don't know what the best method would be for N-D, and if it's even possible.
(I'd also appreciate any other advice about the smallest bounding sphere calculations in ND, in case I'm going in the wrong direction.)
so if I get it right:
Wanted point p is intersection between 3 hyper-spheres of the same radius r where the centers of hyper-spheres are your points p0,p1,p2 and radius r is minimum of all possible solutions. In n-D is arbitrary point defined as (x1,x2,x3,...xn)
so solve following equations:
|p-p0|=r
|p-p1|=r
|p-p2|=r
where p,r are unknowns and p0,p1,p2 are knowns. This lead to 3*n equations and n+1 unknowns. So get all the nonzero r solutions and select the minimal. To compute correctly chose some non trivial equation (0=r) from each sphere to form system of n+1 =equations and n+1 unknowns and solve it.
[notes]
To ease up the processing you can have your equations in this form:
(p.xi-p0.xi)^2=r^2
and use sqrt(r^2) only after solution is found (ignoring negative radius).
there is also another simpler approach possible:
You can compute the plane in which the points p0,p1,p2 lies so just find u,v coordinates of these points inside this plane. Then solve your problem in 2D on (u,v) coordinates and after that convert found solution form (u,v) back to your n-D space.
n=(p1-p0)x(p2-p0); // x is cross product
u=(p1-p0); u/=|u|;
v=u x n; v/=|v|; // x is cross product
if memory of mine serves me well then conversion n-D -> u,v is done like this:
P0=(0,0);
P1=(|p1-p0|,0);
P2=(dot(p2-p0,u),dot(p2-p0,v));
where P0,P1,P2 are 2D points in (u,v) coordinate system of the plane corresponding to points p0,p1,p2 in n-D space.
conversion back is done like this:
p=(P.u*u)+(P.v*v);
My Bounding Sphere algorithm only calculates a near-optimal sphere, in 3 dimensions.
Fischer has an exact, minimal bounding hyper-sphere (N dimensions.) See his paper: http://people.inf.ethz.ch/gaertner/texts/own_work/seb.pdf.
His (C++/Java)code: https://github.com/hbf/miniball.
Jack Ritter
jack#houseofwords.com
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 Latitude, Longitude, and a direction of travel in degrees true north. I would like to calculate if I will intersect a line defined by two more Lat/Lon points.
I figure the two points defining the line would create my great circle and my location and azimuth would define my Rhumb line.
I am only interested in intersections that will occur with a few hundred kilometers so I do not need every possible solution.
I have no idea where to begin.
The simple answer is yes -- if you start from any lat/lon and continue traveling along some great circle, you will eventually cross any and all other great circles on the earth. Every two great circles on earth cross each other in exactly two points (with the notable exception of two identical great circles, which, well, cross each other in all their points.)
But I guess you are not merely asking a yes/no question. You may be wondering where, exactly, those two great circles intersect. We can use the following strategy to find that out:
Each great circle lies on a plane that goes through the center of the earth.
The intersection of those planes is a line (assuming they are not both the exact same plane.)
That intersecting line crosses the surface of the earth at two points -- exactly where our two great circles intersect.
Our mission is thus: (1) find the planes. (2) find their intersection line. (3) find the two intersection points, and finally, (4) express those intersection points in terms of lat/long. (5) extra credit for figuring out which intersecting point is closer to the lat/lon you started at.
Sounds good? The following does this with trig and vector math. To simplify the math somewhat, we'll:
work with the unit sphere, the one centered at the origin of our (x,y,z) coordinate system, and has a radius of 1: x^2+y^2+z^2=1.
we'll assume the earth is a perfect sphere. Not a geoid. Not even a flattened sphere.
we'll ignore elevation.
Step 1 -- find the planes:
All we really care about is the plane normals. Here's how we go and find them:
--One great circle is defined by two points on the earth that it crosses
The normal will be the cross product of the (x,y,z) vectors of each point from the origin (0,0,0). Given the lat/lon of each point, using spherical coordinates conversion, the corresponding (x,y,z) are:
x=cos(lat)*sin(lon)
y=cos(lat)*cos(lon)
z=sin(lat)
With that, and our two points given as lat1/lon1 and lat2/lon2, we can find out the vectors P1=(x1,y1,z1) and P2=(x2,y2,z2).
The first great circle normal is then the cross product:
N1=P1 x P2
--The other great circle is defined by a point on the earth and an azimuth
We have a point P3 and an azimuth T. We'll find a point P4 along the great circle going through P3 at azimuth T at a distance of PI/4 by using the spherical law of cosines (also solved for our convenience here):
lat4=asin(cos(lat3)*cos(T))
lon4=lon3+atan2(sin(T)*cos(lat3),-sin(lat3)*sin(lat4))
Then the normal is as before:
N2=P3 x P4
Step 2: find the planes intersecting line:
Given the two plane normals, their cross product defines their intersecting line:
L=N1 x N2
Step 3: find the intersection points:
Just normalize the vector L to get one of the intersection points on the unit sphere. The other point is on the opposite side of the sphere:
X1=L/|L|
X2=-X1
Step 4: express the intersection points in terms of lat/lon:
Given X=(x,y,z), using spherical coordinate conversion again, and taking into account the point is on the unit sphere:
lat=asin(z)
lon=atan2(y,x)
Step 5: which of the two points is closer?
Use the haversine formula to figure out the distance from your point to X1 and X2, choose the nearer one.
I think your best bet is to use stereographic projection to map the problem from the sphere to the plane. This projection is very useful in that it maps your rhumb line to a logarithmic spiral of form R=exp(θa) (aka a loxodrome) and maps your great circle to a circle on the plane. So your problem reduces to finding the intersection of a logarithmic spiral and a circle (after which you map back to the sphere).
This is just a sketch to get you started. If you need more details, just ask.