views:

1349

answers:

3

Hi, I have no clue about trigonometry, despite learning it in school way back when, and I figure this should be pretty straightforward, but trawling through tons of trig stuff on the web makes my head hurt :) So maybe someone could help me...

The title explains exactly what I want to do, I have a line: x1,y1 and x2,y2 and want a function to find x3,y3 to complete an isosceles triangle, given the altitude.

Just to be clear, the line x1,y2 -> x2,y2 will be the base, and it will not be aligned any axis (it will be at a random angle..)

Does anyone have a simple function for this??

+1  A: 

The third point is on the perpendicular bisector of your base, and is altitude units away from the line.

  1. Calculate the midpoint of the base by averaging the x and y coordinates.
  2. Calculate the slope of your altitude: -dx/dy (perpendicular to dy/dx). You now have your line (point and slope).
    • y - my = -dx/dy * (x - mx)
  3. Substitute your variables in the distance formula: d = sqrt(dx^2 + dy^2)
    1. d = sqrt((x - mx)^2 + (y - my)^2)
    2. d = sqrt((x - mx)^2 + (-dx/dy * (x - mx))^2)
    3. d^2 = (x - mx)^2 + (-dx/dy * (x - mx))^2
    4. d^2 - (x - mx)^2 = (-dx/dy * (x - mx))^2
    5. ±sqrt(d^2 - (x - mx)^2) = -dx/dy * (x - mx)
    6. ±sqrt(d^2 - (x - mx)^2) * dy/dx = x - mx
    7. ±sqrt(d^2 - (x - mx)^2) * dy/dx + mx = x
    8. x = ±sqrt(d^2 - (x - mx)^2) * dy/dx + mx
  4. Calculate the other variable (y here) using your line equation (from #2).
  5. You now have two points; pick whichever you want...

In pseudocode:

dx = x1 - x2
midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)
slope = -dx / (y1 - y2)
x = sqrt(altitude*altitude - dx*dx) / slope + midpoint.x
y = slope * (x - midpoint.x) + midpoint.y

This is probably not the most optimal method. Not sure if it even works. xD

strager
Looks pretty efficient to me, unless you're concerned about degenerate cases (dx=0 or dy=0). Nice explanation!
Adam Liss
Made an update with the math pre-done. Thanks for your complement, Adam.
strager
This is the kind of answer is was hoping for (pseudo code xD) but it seems theres a little snag, the value of altitude*altitude - dx*dx usualy turns out as a negative number, which of course we cant get the sqrt of... any ideas??
@Aidan, hmm, never considered that. Perhaps take the easy route and do sqrt(abs(...))?
strager
A: 

Al I can remember is that an isosceles triangle will have sides of equal length, and equal angles at the base. If you have the height, then you have the final coordinate because this will be the point of intersection, right?

IrishChieftain
+2  A: 

construct a normal to the vector (x1,y1)->(x2,y2). place it at the midpoint ((x1+x2)/2,(y1+y2)/2) and go out a distance h.

the normal will look like (-(y2-y1),x2-x1). make this a unit vector (http://en.wikipedia.org/wiki/Unit_vector).

add h times this unit vector to the midpoint.

Ray Tayek
Nice idea... never thought about that.
strager
Thanks guys, this and strager's answer were very helpful, eventually i ended using the unit vector method and its working great.