4

Suppose you have the following three points A, B, and C as shown in the following picture:

enter image description here

The points are always sorted according to their vertical offset, so the top most point is always A. Sometimes B and C could have the same y coordinate.

I'm trying to find the x coordinate for point D. I can find the Y coordinate for D by interpolating points A.y and C.y at (B.y / (C.y - A.y)). I'm doing my interpolation using the following formula (in C++)

float linearInterpolation(float a, float b, float t)
{
    return a + (t * (b - a));
}

So in other words, D.y = linearInterpolation(A.y, C.y, (B.y - A.y) / (C.y - A.y))

So to summarize, my question is: how do I find D.x?

Thanks

--

Answer:

Just to clarify, here's the solution that was suggested and worked:

D.x = A.x + (B.y - A.y) * (C.x - A.x) / (C.y - A.y);
D.y = B.y;

As illustrated in the image below:

enter image description here

rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
  • 2
    You have this backwards. The y coordinate of D equals the y coordinate of B (substitute your formula into linearInterpolation and you will find everything cancels, leaving B.y). You need the linear interpolation only for x... – Nemo Jan 16 '12 at 23:46

3 Answers3

7

It it is the x coordinate that requires interpolation. The y coordinates of B and D are equal on your diagram.

D.x = A.x + (B.y - A.y) * (C.x - A.x) / (C.y - A.y);
D.y = B.y;

You should also make a provision for the case of C.y == A.y, where D.x could be anywhere between A.x and C.x. One way to do this is to not draw triangles, for which abs(C.y - A.y) < delta, with the delta being on the order of magnitude of 1 pixel.

Don Reba
  • 13,814
  • 3
  • 48
  • 61
  • Fairly sure the first line should be `A.x + (A.y - D.y) * (A.x - C.x)/(A.y - C.y).` –  Jan 16 '12 at 23:54
  • @EthanSteinberg, if the slope is positive and D.y is greater than A.y, then D.x must be greater than A.x. So, the first term should be (B.y - A.y). Switching the signs on the slope calculation (or on any pair of terms) makes no difference. – Don Reba Jan 17 '12 at 00:00
2
D.y = B.y

delta_x = C.x - A.x 
delta_y = C.y - A.y 

dist_y = B.y - A.y

percent = dist_y / delta_y

D.x = A.x + percent * delta_x
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

The function for the line AC is y = mx + b.

m = (A.y - C.y)/(A.x - C.x)

You can then substitute A in: A.y = A.x * m + b

b = A.y - A.x *m

You need to calculate x from y, so swap the function around.

mx = y -b

x = (y -b)/m

Those are three steps to find the x from the y along that side of a triangle. Note that you don't need to do any interpolation to find Dy. Simply, D.y = B.y.

Note that you can probably optimize what I have just written into a smaller series of steps. I think its better to write easier to read code though.

  • You could think of it in terms of the line function or you could think of it in terms of unit cancellation. Then you immediately know that the *x* offset is the product of another *x* offset and a ratio of two *y* offsets. :) – Don Reba Jan 17 '12 at 00:08