0

So, I hope this still classifies as coding, not math... Oh well, aren't they same anyway ;) So, my problem is this: I would want to calculate coordinates of a point on line.

For example, if I had point A(0, 0) and point B(5, -3), I'd like to calculate coordinates of point C, witch is 2 (can also be something else than 2) from A and on the line AB. I hope you realized what I mean.

I know how to calculate distance between A and B, but locating C.. I literally don't know where to start. And even less on how to implement int in java. Some help for newbie?

Heiski
  • 111
  • 2
  • 10
  • There are two points on *line* AB that are within a distance `d` from `A`. Do you need both of them? Maybe you meant a segment (and then there could be zero points, if |AB| – amit Sep 04 '14 at 12:55

2 Answers2

1

Here is a more mathematical approach: (I hope you understand my drawing)

enter image description here

You know A and B and the distance from A to C. The angle can be calculated by calculatin the slope of [AB]. From here you should be able to figure the lenght of the 2 segments (marked with blue), by using sin and cos.

DeiAndrei
  • 947
  • 6
  • 16
0

Use the parametric equation of the line: P = (1-t).A + t.B (P, A, and B are points, defined by pairs of coordinates). If you set t=0, you get A; if you set t=1, you get B; if you set other values, you get points along the line AB.

Now the distance issue: you want P to be at distance d of A. Then d² = AP² = t²AB², or t = d/AB.

Programmatically:

ABx= Bx - Ax; 
ABy= By - Ay;
AB= Sqrt(ABx * ABx + ABy * ABy);

t= d / AB;

Xc= Xa + t * ABx;
Yc= Ya + t * ABy;