Reputation: 395
I have this simple data set:
Class1 = [1 0; 0.5 1; 0.5 1.5];
How do I go about finding the point on the 2d space that is of equal Euclidean distance from all three points of the data set?
The point highlighted in orange should roughly be where the calculated point is.
Upvotes: 3
Views: 2002
Reputation: 1927
If you are looking for a point with equal distances from the three points, you can create the corresponding symbolic equations and solve them. Let's separate the points to P1 = [1 0]
, P2 = [0.5 1]
and P3 = [0.5 1.5]
.
syms x y
% [x, y] has equal distance from P1 and P2:
eqn1 = (P1(1)-x).^2 + (P1(2)-y).^2 == (P2(1)-x).^2 + (P2(2)-y).^2;
% [x, y] has equal distance from P1 and P3:
eqn2 = (P1(1)-x).^2 + (P1(2)-y).^2 == (P3(1)-x).^2 + (P3(2)-y).^2;
[a, b] = solve([eqn1, eqn2], [x, y])
% a =
%
% 9/4
%
%
% b =
%
% 5/4
Generally, working with symbolic variables and equations is not a reasonable choice if you are dealing with a large amount of data. Otherwise, it is convenient for writing math in a compact and simple way.
Upvotes: 1
Reputation: 35080
If you want to find the point which is equally far from all 3 points (which I concluded from your hints in comments), you're essentially looking for the center of the circle that contains all three points. Note that there is either 1 or 0 such circle, depending on whether your three points are collinear (i.e. whether they are aligned on a line).
Your simplest choice is looking at two radii of the circle, and looking at their intersection. If you imagine for a moment that you know the circle, you can easily see that any two of your points define secants of your circle, so the perpendicular bisector of these secants will intersect in the center of the circle. If the points are aligned on a line, all these bisectors will be parallel, i.e. they won't have an intersection (which makes sense: a line is a circle with infinite radius).
So you need to look at the equations of the perpendicular bisectors of your points, p1 = [x1,y1]
, p2 = [x2,y2]
and p3 = [x3,y3]
. The first perpendicular bisector defined by the first two points goes through q1 = (p1+p2)/2
, and is perpendicular to n1 = p1-p2
, so its direction vector is v1 = [y1-y2,x2-x1]
. Now you need to do the same for another pair of points:
q2 = (p1+p3)/2;
v2 = [y1-y3,x3-x1];
Then the points of your two perpendicular bisectors are defined by
q1 + t1*v1
q2 + t2*v2
with t1
, t2
real parameters, respectively. What you need to do is solve for t1
and t2
such that the two lines intersect in O = [ox,oy]
:
q1 + t1*v1 = q2 + t2*v2
t1*v1 - t2*v2 = q2 - q1
This latter is a 2d vector equation: two inhomogeneous linear equations for the two parameters t1
and t2
. If you solve it in the usual way, either component of the solution [tsol1; tsol2]
will give you the coordinates of the origin:
O = q1 + tsol1*v1 == q2 + tsol2*v2
The linear equation will not have a solution exactly when the two bisectors are parallel and don't go through the same point (i.e. your three points are aligned along a line). The linear equation will be trivial and have infinitely many solutions exactly when the two bisectors coincide (i.e. if two of your points coincide).
In non-degenerate cases the equations should have a unique solution, from which you can derive the center of the circle.
Upvotes: 4