How can I traverse a circle while identifying given points?

My input data are three points, lets say p1, p2 and p3. I can obtain the equation of a circle that goes through p1, p2 and p3. Now what I want to do is traverse the circle in my direction of choice, starting always at the "top" of the circle (max y), identifying each time one of the three initial points is found. As a result I want a three row array with its row number matching the order in which the three initial points p1, p2 and p3 where found. These means the first row must contain the first point found, the second row contains the second point found, and the third row contains the last point found.
Than you for your time!

 Réponse acceptée

Roger Stafford
Roger Stafford le 16 Avr 2016
Modifié(e) : Roger Stafford le 16 Avr 2016
Let P1 = (x1,y1), P2 = (x2,y2), P3 = (x3,y3) be the three points and their respective coordinates. Perform these computations:
A4 = 2*((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)); % Four times triangle's signed area
R2 = [x1^2+y1^2,x2^2+y2^2,x3^2+y3^2]/A4;
x0 = dot([y2-y3,y3-y1,y1-y2],R2); % Coordinates of circle's center
y0 = dot([x3-x2,x1-x3,x2-x1],R2);
a1 = mod(atan2(-(x1-x0),y1-y0),2*pi); % Angles counterclockwise
a2 = mod(atan2(-(x2-x0),y2-y0),2*pi); % from "top"
a3 = mod(atan2(-(x3-x0),y3-y0),2*pi);
[~,ix] = sort([a1,a2,a3]);
M = [x1,y1;x2,y2;x3,y3];
M = M(ix,:); % Points in order counterclockwise from top
Array M will be the coordinate pairs listed in counterclockwise order around from the "top" of the circle. If you want to go in clockwise order, just apply 'fliplr' to 'ix' or else do a descending 'sort'.

2 commentaires

Here is a somewhat more efficient code to accomplish the same thing. As before, let the three points, P1, P2, and P3 have the respective coordinates, (x1,y1), (x2,y2), and (x3,y3). Do this:
x21 = x2-x1; y21 = y2-y1;
x31 = x3-x1; y31 = y3-y1;
A4 = 2*(x21*y31-y21*x31); % 4 times triangle's signed area
R2 = [x21^2+y21^2,x31^2+y31^2];
x01 = dot([y31,-y21],R2)/A4; % Circle's offset center coordinates
y01 = dot([-x31,x21],R2)/A4;
a = mod(atan2([x01,x01-x21,x01-x31],[-y01,y21-y01,y31-y01]),2*pi);
[~,ix] = sort(a); % Sort angles in ascending order
M = [x1,y1;x2,y2;x3,y3];
M = M(ix,:);
As before, M will have the coordinate rows of the points in counterclockwise order around from the "top" of the circle. To have clockwise order, do 'fliplr' on 'ix' or use a descending sort.
Thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by