How to calculate normal to a point on random contour ?

20 visualizzazioni (ultimi 30 giorni)
Hi all,
I am trying to automate a calculation to find points in the vicinity of a point using its normal to the contour. For example if i had a circle and wanted to find points for P(1,0) that are h=0.05 away on the normal lines it would be like this:
Normally I have random contours that are created with spline functions, which returns me set of points on the contour and their derivatives.
Now for every point on the contour i should find this set of points on the normal line.
Is there a function that I can use for this purpose or for every point should i find the equation of normal line to the point and find 2 points on that line with given h?
Any suggestion is appreciated.
  1 Commento
Bruno Luong
Bruno Luong il 6 Nov 2018
Modificato: Bruno Luong il 6 Nov 2018
Normally I have random contours that are created with spline functions, which returns me set of points on the contour and their derivatives.
You should provide your MATLAB code for us to know exactly the storage format of your data and how you get them.
Wording like that is no help.

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 6 Nov 2018
Modificato: John D'Errico il 6 Nov 2018
Not that difficult. It helps if you have the curve fitting toolbox, as that is where cscvn resides. Else, you would need something like my SLM Toolbox. but cscvn is entirely sufficient.
For example, consider these points around an ellipse.
theta = linspace(0,2*pi,10);
xypoints = [2 1;-1 3]*[cos(theta);sin(theta)];
plot(xypoints(1,:),xypoints(2,:),'r-o')
grid on
axis equal
So a very simple ellipse, but one canted at an angle, and with axes that are not the same lengths . The first and last point will be the same, so cscvn will create a periodic spline that passes through them.
pp = cscvn(xypoints)
pp =
struct with fields:
form: 'pp'
breaks: [0 1.4729 2.8237 4.0485 5.3913 6.8621 8.2914 9.5575 10.817 12.241]
coefs: [18×4 double]
pieces: 9
order: 4
dim: 2
So you can see we can plot it, and the curve is a nice smooth one.
fnplt(pp)
hold on
axis equal
grid on
plot(xypoints(1,:),xypoints(2,:),'ro')
First, you need to appreciate that this is a function of cumulative linear arclength that set of points around the curve. So the arclength is that which you get from the connect the dots curve I drew before. In fact, we can see the arclength at each of the original points around that curve.
pp.breaks
ans =
0 1.4729 2.8237 4.0485 5.3913 6.8621 8.2914 9.5575 10.817 12.241
In fact, we can reconstruct the data itself from
fnval(pp,pp.breaks)
ans =
2 2.1749 1.3321 -0.13397 -1.5374 -2.2214 -1.866 -0.63751 0.8893 2
-1 1.1623 2.7808 3.0981 1.9658 -0.086368 -2.0981 -3.1281 -2.6944 -1
Differentiate the spline, creating a new function:
ppd = fnder(pp);
Now, lets pick a point midway between the first two data points, and compute two new points that are off the curve, at a distance of 0.1 units away from the curve.
t12 = mean(pp.breaks([1 2]));
N12 = [0 1;-1 0]*fnval(ppd,t12); % tangent vector, rotated 90 degrees
N12 = N12/norm(N12); % unit normalized vector
Now, we find two new points along the curve, offset by 0.5 units in either direction from the interpolated curve.
delta = 0.5;
xy12_offset = fnval(pp,t12) + N12*delta*[-1 1];
And with that last figure still there with hold on, we get
plot(xy12_offset(1,:),xy12_offset(2,:),'gs-')
Thus, two new points, normal to the curve, midway between points 1 and 2. offset by a distance of 0.5 units.
Note that I was VERY careful to make the axes such that they use the same units. Otherwise, the normal vector would not appear to be normal to the curve.

Più risposte (1)

KSSV
KSSV il 6 Nov 2018
  4 Commenti
KSSV
KSSV il 6 Nov 2018
This function will work like charm. You need not to modify any.
ttopal
ttopal il 6 Nov 2018
Thank you KSSV, i realized now that I could calculate the points inside and outside the contour using this.

Accedi per commentare.

Categorie

Scopri di più su Spline Construction in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by