MATLAB Answers

How to divide a distance into some equal parts?

31 views (last 30 days)
Zara Khan
Zara Khan on 25 Jan 2020
Commented: Zara Khan on 31 Jan 2020
Suppose there are two points (x1,y1) and (x2,y2). I am finding out the distance between these two points. Diving the distance in 2,3, 4,5 and so on. I want to mark the places also. The distance is varying in every input cases.

  0 Comments

Sign in to comment.

Answers (2)

Mohammad Sami
Mohammad Sami on 25 Jan 2020
Assuming the coordinates are variable p1, p2
% p1 = [x1 y1];
% p2 = [x2 y2];
midpoint = p1 + 0.5.* (p2-p1); % halfway point
% just change 0.5 to something else for other point along the line between p1 and p2

  1 Comment

Zara Khan
Zara Khan on 25 Jan 2020
how to get all the points coordinates? I mean each division coordinates

Sign in to comment.


John D'Errico
John D'Errico on 25 Jan 2020
Edited: John D'Errico on 25 Jan 2020
You have two points. Call they xy1 and xy2, where the xy are row vectors of length 2. For example...
xy1 = [-1,3];
xy2 = [2,5];
Now, you wish to create new points, that are equally spaced in distance along the line that connects the points. Lets say you want to divide the line segment into n equal parts. That means, including the two original points, you will have n+1 points as a result, with n-1 additional points created. I'll pick, for example, n=5 here. So there will be 5 segments of equal length, so 4 new points to be created in addition.
n = 5;
t = linspace(0,1,n+1)';
xy = (1-t)*xy1 + t*xy2;
plot(xy(:,1),xy(:,2),'b-o')
xy
xy =
-1 3
-0.4 3.4
0.2 3.8
0.8 4.2
1.4 4.6
2 5
As you can see, xy is an array with n+1 rows and 2 columns. The first and last rows are the original points, with the desired 4 new rows in between.
If you want to appreciate why it works so simply, note that I have taken a weighted linear combination of xy1 and xy2. Some would call this a convex linear combination, I suppose. The important thing to understand is that the weights, thus (1-t) and t respectively, sum to 1, and they are created using a tool like linspace, so they uniformly vary from 0 to 1.

  6 Comments

Show 3 older comments
John D'Errico
John D'Errico on 31 Jan 2020
Just knowing the distance between points is not sufficient information to do anything except to draw a circle, because a circle is the set of all points that lie at a known distance from another point.
Are you asking, given a pair of points (xy1 and xy2) to find a point along the line that is at a known distance from xy1, in the direction of xy2, thus along that ray?
xy1 = [-1,3];
xy2 = [2,5];
Now find a new point along that line that lies at a distance of 3 units from xy1.
d = 3;
V = xy2 - xy1;
V = V/norm(V);
xy3 = xy1 + V*d;
xy3 =
1.496150883013531 4.664100588675687
Your questions are confusing however, so it is impossible to know what you really want or need.
Zara Khan
Zara Khan on 31 Jan 2020
we have xy1 and xy2. we are getting some intermediate coordinates between them. Now I want to draw circles that will pass through all these inteemediate coordinates. Is this possible ?
Note: This one is the extra question I am asking.
Zara Khan
Zara Khan on 31 Jan 2020
More clearly I want to get all the distances from point xy1 to other points. Let assume xy1 is the center then I want to get other radius. We have all intermediate coordinates. How to calculate the distance? Like you assumed d=3, but I want to calculate distance from center to other points. This will help to draw circles with different radius around the circle.

Sign in to comment.

Sign in to answer this question.