Results 1 to 9 of 9

Thread: right triangles

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Question right triangles

    can anyone show me the formula to work out the lengths of 1 of the legs + the hypotenuse in a right angle triangle? i know the length of 1 of the legs in pixels + also know all 3 angles.

    so assuming i know the length of side a + all 3 angles, how do i work out the length of sides b + c?
    Attached Images Attached Images  

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: right triangles

    Call the angle opposite side b angle B. Using the same convention, angle C would then be 90 degrees in your setup. From there you use trig functions. The mneumonic "soh-cah-toa" helps me remember them. In this setup, "cah" means "cosine = adjacent / hypotenuse". Then we can solve for c using

    cos(B) = a / c = adjacent side to angle B / hypotenuse of the triangle.

    Since we know a and angle B, solve for c by cross multiplying, giving

    c = a / cos(B).

    From here there are three ways that come to mind to solve for side b. I'll use the Pythagorean Theorem, which says that the side lengths of a right triangle are related through

    a^2 + b^2 = c^2.

    Since we can get c using the above formula, and we know a, we can solve for b:

    b^2 = c^2 - a^2
    b = sqrt(c^2-a^2).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: right triangles

    thanks jemidiah.
    i haven't tried it yet but i'm sure it'll work.

    how about if i know the length of side b + all of the angles?

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: right triangles

    i can't get your formula to work. in the attached screenshot i'm trying to draw a line from the centre to the 12 o'clock position on the 2nd rectangle(that part is working), then another line from the centre to the 1 o'clock position on the 2nd rectangle, which is way off.

    what am i doing wrong?


    heres the code:

    vb Code:
    1. '351,151 - centre
    2. '10 + sf.Height - 2nd rectangle top
    3. '30 degrees - 1 o'clock
    4. Dim lX2 As Single = 351 + Math.Sin(Math.PI * 30 / 180) * 351 / Math.Cos(30)
    5. Dim lY2 As Single = 151 - Math.Cos(Math.PI * 30 / 180) * (151 - (10 + sf.Height)) / Math.Cos(30)
    6. gr.DrawLine(Pens.Red, 351, 151, 351, (10 + sf.Height))
    7. gr.DrawLine(Pens.Red, 351, 151, lX2, lY2)
    Attached Images Attached Images  

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: right triangles

    You've used Cos(30) twice, forgetting to convert to radians. The rest is a bit puzzling to me. For instance,

    Code:
    Dim lX2 As Single = 351 + Math.Sin(Math.PI * 30 / 180) * 351 / Math.Cos(30)
    Should probably be

    Code:
    Dim lX2 As Single = 351 + b / Math.Cos(Math.PI * 30 / 180)
    where b is the length of the vertical red line. This comes from my formula above, noting that you can just swap a, b, A, and B for an analogous problem.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: right triangles

    lx2 is the left position in pixels of the endpoint of the diagonal line
    ly2 is the top position in pixels of the endpoint of the diagonal line

    thanks for helping, but i'm still a bit confused

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: right triangles

    Yup, I understood what lx2 and ly2 were. I confused the hypotenuse and opposite side, though, in my previous post--doh! I could use my two formulas together to get the correct x offset, but using the tangent function is shorter so I'll do that. It's the second of the three possible solutions I mentioned at first to solving for the length of the unknown leg of the triangle.

    Since tangent = opposite / adjacent, tan(30 degrees) = x offset / b where b is the red vertical line's length. In Paint I found b=105 pixels; this is probably 10 + sf.Height - 151, though I don't use .NET. Then, the x offset is b*tan(30 degrees), or

    lx2 = 351 + 105*tan(30 deg) = 351 + 105*0.577 = 412.

    The y offset is simply b, so ly2 = 151 - 105 = 46. I've drawn a line from (351, 151) to (412, 46) on the attached image in blue, which is I believe what you're after. One important thing to note, though, is that if you want the blue line I've drawn to "lock" to the outer rectangle, you'll have to use different equations for each side of the rectangle, since your triangle problem changes each time.
    Attached Images Attached Images  
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: right triangles

    i tried it. theres a tan problem. by my calculations math.tan(30) = -6.40533119664628

    i tried converting the angle to radians. it works. excellent. thanks
    Last edited by .paul.; Jun 6th, 2009 at 07:58 PM.

  9. #9
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: right triangles

    Yes, I wrote tan(30 degrees), though programming languages don't use units in their numbers. I could have said tan(1800 minutes) for the same thing, since 30 degrees = 30*60 or 1800 minutes. The upshot of it is, .NET wants you to use tan(___ radians), so you have to convert to radians.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width