July 4, 2011 12:24 PM Subscribe

Geometry question -> Determine X and Y from distance along a straight line, or, how do I find the lengths of A and B if I know the length of C and the ratio of A to B in a right triangle?

I'm writing a little script that does some graphing operations based on parameters that are fed into it.

I have no problem finding the length of a given diagonal line defined by two sets of x and y coordinates using the pythagorean theorem. I can also get f(x) for that line.

What I've forgotten how to do is figure out the x and y coordinates based on travelling a defined number of units along that diagonal line.

Say I have a line with the slope f(x) = (x + 1.5) / 6. Let's say I have starting coordinates of x1=0 and y1=0. Let's say I need to move along my line a total of 7 units. How do I figure out x2 and y2 at that point?

Help a dummy out :-)

I'm writing a little script that does some graphing operations based on parameters that are fed into it.

I have no problem finding the length of a given diagonal line defined by two sets of x and y coordinates using the pythagorean theorem. I can also get f(x) for that line.

What I've forgotten how to do is figure out the x and y coordinates based on travelling a defined number of units along that diagonal line.

Say I have a line with the slope f(x) = (x + 1.5) / 6. Let's say I have starting coordinates of x1=0 and y1=0. Let's say I need to move along my line a total of 7 units. How do I figure out x2 and y2 at that point?

Help a dummy out :-)

Of course, the coordinates (0,0) aren't on the line f(x) = (x + 1.5) / 6.

So modify the question to use starting coordinates of (0,0.5) to make it, ya know, possible in this universe.

Where's the edit function?

posted by syzygy at 12:33 PM on July 4, 2011

So modify the question to use starting coordinates of (0,0.5) to make it, ya know, possible in this universe.

Where's the edit function?

posted by syzygy at 12:33 PM on July 4, 2011

From the slope, you know that *y* = (1/6)**x* everywhere along the line. Again, using the Pythagorean formula, 7² = (*x*/6)² + *x*². The calculator gives me *x* = 6.9 or so. This gives you the "extra" run in the horizontal direction. The "extra" rise is one-sixth of that. Add them to your starting point, whatever you choose it to be, to get your *x*_{2} and *y*_{2}.

posted by Nomyte at 12:37 PM on July 4, 2011

posted by Nomyte at 12:37 PM on July 4, 2011

Along the line starting at (0,0), of course. The point is, the formula gives you the additional distance in each direction, and you can add it to any starting coordinates.

posted by Nomyte at 12:38 PM on July 4, 2011

posted by Nomyte at 12:38 PM on July 4, 2011

The way I'd approach this is to find a unit vector in the same direction as the line, then multiply that vector by the distance you want to travel.

Finding the unit vector is easy: take the (dx,dy) of the line (a non-unit vector) and divide dx and dy by the length of that vector (normalizing it to unit 1).

posted by hattifattener at 12:38 PM on July 4, 2011 [2 favorites]

Finding the unit vector is easy: take the (dx,dy) of the line (a non-unit vector) and divide dx and dy by the length of that vector (normalizing it to unit 1).

posted by hattifattener at 12:38 PM on July 4, 2011 [2 favorites]

(Oh, and: the advantage of thinking in terms of geometrical and linear-algebra concepts like that is, it often avoids fake singularities in the math. For example, if your line were vertical, and you were using a computation based on its slope, you'd have an infinity to deal with and your program might break, but it's just an artifact of the math. The unit-vector approach only has one singularity— where x1=x2 and y1=y2, you can't derive a unit vector— and that's because you really don't have a defined direction in that case.)

posted by hattifattener at 12:44 PM on July 4, 2011

posted by hattifattener at 12:44 PM on July 4, 2011

I agree that hattifattener's method is the best here, because that way you never end up dividing by zero unless the two points are the same in which case you have no business asking the question.

Here's a numerical example: you're on the line (x+1.5)/6, starting at (0, 0.25), and you'd like to move 7 units.

Since the line has slope 1/6, the unit vector is (1/sqrt(1+(1/6)^2), (1/6)/sqrt(1+(1/6)^2) - in decimals, (0.9864, 0.1644).

So to move 7 units you'd move to

(0 + (0.9864 * 7), 0.25 + (0.1644 * 7))

or (6.9048, 1.4008).

posted by madcaptenor at 12:52 PM on July 4, 2011 [1 favorite]

Here's a numerical example: you're on the line (x+1.5)/6, starting at (0, 0.25), and you'd like to move 7 units.

Since the line has slope 1/6, the unit vector is (1/sqrt(1+(1/6)^2), (1/6)/sqrt(1+(1/6)^2) - in decimals, (0.9864, 0.1644).

So to move 7 units you'd move to

(0 + (0.9864 * 7), 0.25 + (0.1644 * 7))

or (6.9048, 1.4008).

posted by madcaptenor at 12:52 PM on July 4, 2011 [1 favorite]

Thanks for the help. I marked hattifattener's answer as the best because it fits right into my code with two tiny changes and a minimum of mental gymnastics.

Seems to work fine in a few quick tests.

posted by syzygy at 12:54 PM on July 4, 2011

Seems to work fine in a few quick tests.

posted by syzygy at 12:54 PM on July 4, 2011

This thread is closed to new comments.

Equation 1: (x2 - x1)^2 + (y2 - y1)^2 = 7

Equation 2: y2 = (x2 + 1.5)/6

Solve using your favourite method, e.g. substitute the right hand side of equation 2 into equation 1, so you have (x2 - x1)^2 + [(x2 + 1.5)/6 - y1]^2 = 7. Now insert the values of x1 and y1. Now solve for x2 using the quadratic formula. Now you know x2, use equation 2 to figure out y2.

There are doubtless more elegant methods, but this works.

posted by caek at 12:31 PM on July 4, 2011