Click here to Skip to main content
12,554,442 members (82,863 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# .NET geometry
HI All,

i have two points (x1,y1) and (x2,y2). I want to know the point which lies at specific distance from the first point(x1,y1) assuming a straight line between above two points.

for suppose, if i say 10 meters distance, then the output point should be on the same straight line of (x1,y1)(x2,y2) and it should be 10 meters away from (x1,y1).

Can any one help how to get this in C# or any other language?

Thank You.
Posted 12-Feb-13 2:20am
Updated 12-Feb-13 2:45am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But some hints:
This is basic geometry: Pythagoras in fact:
The horizontal distance X is the absolute value of the different between x1 and x2, and the vertical Y is similarly y2 and y1.

The length of the line between them is Z = Root(Square(X) + Square(Y)), so all you have to do is extend that line to the the distance you want and scale X and Y appropriately. You can then add the scaled X and Y values onto the (x1, y1) point and you are done.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


Let us talk in some simple terms.
There are 2 points (x1,y1) and (x2,y2) and you need to find a point in between these above points which is at a distance z from the first point.
I think you will have to provide the distance between (x1,y1) and (x2,y2) also, coz after that the number of equations and number of variables will be equal and the problem will be solved.
Below is the code snippet which will give an equation in variables a & b.Likewise you can have another equation in a & b which will be the result of the distance between points (a,b) and (x2,y2).
After that write another function to solve both the equations for variables a & b and it will give your solution.

This is the first stuff which struck my mind and hope it helps...:)

public double FindDistance(){
double a=a;
double b=b;
double distance=Math.Sqrt((x1-a)*(x1-a)+(y1-b)*(y1-b))
return distance;

In the above code,(a,b) is the intermediate point.

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Let's assume you have written an extremely useful library for co-ordinate manipulation, which includes
PointF Add(PointF p0, PointF p1)
PointF Sub(PointF p0, PointF p1)
PointF Multiply(PointF p0, float factor)
PointF Multiply(float factor, PointF p0) // Commutativeness is not for free
float Length(PointF p0)
But you did it overriding the appropriate operators so we can conveniently use them from now on. Maybe you had to create a custom PointF class for that since System.Drawing.PointF may not be capable of being tuned in that way.

P2, seen from P1 has the coordinates
P2 - P1
which itself can be an instance of the super-cool point class you invented above.

Seen from the origin of coordinates instead as from P1, P2 would be
P1 + P2 - P1
Sounds weird, as P1 is added and subtracted, but it will help later on. Because we can look at the problem nearly entirely from P1's perspective. At the very end, we'll have to add P1's co-ordinates again and have a correct result.


So we can say this
PointF P2FromP1 = P2 - P1;
float P2DistP1 = Length(P2FromP1);
PointF NormalInP2Direction = (1 / P2DistP1) * P2FromP1;
PointF XFromP1 = xTargetDistanceFromP1 * NormalInP2Direction;
We now can add P1's coordinates and have our result for X:
PointF X = XFromP1 + P1;
And you're done. Plus you have a spectacular tool at your fingertips for future daddling-around-with-geometry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100