Reputation: 35
I am working in c++ application. And its new to me. Here I write a function which gain two coordinates of a line. I have to process these coordinates and find another point which in that same line.
I will gain A(x1,y1) and B(x2,y2)
coordinates.
need to find
C(x3,y3)
coordinates.
Therefore I calculate the Slope of given line.
Double slope = (x1-x2)/(y1-y2);
And I know the distance of 3rd point from A point.
Double dis = sqrt(pow(x2-x1) + pow(y2-y1)) * 1.35 ;
I want to find new coordinates x3 ,y3 using Slope
and dis
.
Can anyone help me to solve this please.
To calculate the x3 I can use mathematical part,
x3 = slope * y3 -------------------1
dis = sqrt(pow(x3-x1) + pow(y3-y1)) ------------2
using these 2 equations which generating in run time , I want to calculate x3
and y3
.
Upvotes: 1
Views: 3086
Reputation: 114579
Unless you are working with a "1.5d" graph y=y(x)
you should never use formulas based on y=m*x+q
because that doesn't work for vertical lines (and works poorly for near-vertical lines).
In your case the best approach is to use the parametric equation for a line
x = x1 + t * dx
y = y1 + t * dy
where dx = x2 - x1
and dy = y2 - y1
are proportional to the components of the direction unit vector oriented from P1 to P2 and are used instead of m
and q
to define the line (avoiding any problem with vertical or almost vertical lines).
If you need a point on a specific distance then you just need to find the actual unit vector components with
double dx = x2 - x1;
double dy = y2 - y1;
double dist = sqrt(dx*dx + dy*dy);
dx /= dist;
dy /= dist;
and then the coordinates of the point you need are
double x3 = x1 + prescribed_distance * dx;
double y3 = y1 + prescribed_distance * dy;
or using -prescribed_distance
instead depending on which side you want the point: toward P2 or away from it?
If however the prescribed distance is proportional to the current distance between the two points the normalization is not needed and the result can be the simpler:
double x3 = x1 + (x2 - x1) * k;
double y3 = y1 + (y2 - y1) * k;
where k
is the ratio between the prescribed distance and the distance between the two points (once again with positive or negative sign depending on which side you are interested in).
By using parametric equations x=x(t), y=y(t)
instead of explicit equations y=y(x)
in addition to not having artificial singularity problems that depend on the coordinate system you also get formulas that are trivial to extend in higher dimensions. For example for a 3d line you just basically need to add z
coordinate to the above formulas in the very same way x
and y
are used...
Upvotes: 5
Reputation: 10353
If you substitute the first equation "y3 = slope * x3"
into the 2nd equation "dis = sqrt(pow(x3-x1) + pow(y3-y1))"
, and square both sides, you get a quadratic which you can solve using the quadratic formula.
After substitution you get:
dis^2 = (x3-x1)^2 + (slope*x3 - y1)^2
Square both sides:
(slope^2+1)*x3^2 + (-2*slope*y1-2*x1) + 2*y1^2 = dis^2
Solve for x3 using the quadratic formula:
x3 = (2*slope*y1+2*x1) +/- sqrt((2*slope*y1+2*x1)^b - 4*(slope^2+1)*(2*y1^2-dis^2))/(2*(slope^2+1))
Substitute x3 into the first equation to get y3:
y3 = slope * x3
Upvotes: 0
Reputation: 799450
Too much math.
x3 = (x1 - x2) * 1.35 + x2
y3 = (y1 - y2) * 1.35 + y2
Upvotes: 6