Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. working out a triangle hypotenuse

working out a triangle hypotenuse

Scheduled Pinned Locked Moved C#
csharpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10235928
    wrote on last edited by
    #1

    I am looking for my C# class to firstly work out distance between two coordinate points on the x plane and then work out the distance between the same coordinated points on the y plane. There are potentially several ways of doing this. I was thinking: Firstly calculate distance on the x plane (x1->x2): in Excel form:

    if (x2 > x1, x2 - x1, x1 - x2)
    (then) (else)

    Then calculate the distance for the y plane (y1->y2):

    if (x2 > x1, x2 - x1, x1 - x2)

    if we then think of a triangle (with right angle) we have A(squared) + B(squared) = C(squared) We would have the length A and B and therefore would be able to calculate C. Is there an easy way of doing this in C# or is it best to code out the equations? Any advise appreciated.

    A B 2 Replies Last reply
    0
    • U User 10235928

      I am looking for my C# class to firstly work out distance between two coordinate points on the x plane and then work out the distance between the same coordinated points on the y plane. There are potentially several ways of doing this. I was thinking: Firstly calculate distance on the x plane (x1->x2): in Excel form:

      if (x2 > x1, x2 - x1, x1 - x2)
      (then) (else)

      Then calculate the distance for the y plane (y1->y2):

      if (x2 > x1, x2 - x1, x1 - x2)

      if we then think of a triangle (with right angle) we have A(squared) + B(squared) = C(squared) We would have the length A and B and therefore would be able to calculate C. Is there an easy way of doing this in C# or is it best to code out the equations? Any advise appreciated.

      A Offline
      A Offline
      Anurag Sinha V
      wrote on last edited by
      #2

      Feels like you need to find out the distance between 2 points on a 2D plane. Point A (x1,y1) Point B (x2,y2) Distance between them would be=[((x1-x2)^2)+((y1-y2)^2)]^(1/2) Use Math.Abs method to get the absolute value of the distance regardless of x1>x2 or x1

      U 1 Reply Last reply
      0
      • A Anurag Sinha V

        Feels like you need to find out the distance between 2 points on a 2D plane. Point A (x1,y1) Point B (x2,y2) Distance between them would be=[((x1-x2)^2)+((y1-y2)^2)]^(1/2) Use Math.Abs method to get the absolute value of the distance regardless of x1>x2 or x1

        U Offline
        U Offline
        User 10235928
        wrote on last edited by
        #3

        This looks like a good approach. I am really new to C# (after 10 years away), do you have a coded example?

        A 1 Reply Last reply
        0
        • U User 10235928

          This looks like a good approach. I am really new to C# (after 10 years away), do you have a coded example?

          A Offline
          A Offline
          Anurag Sinha V
          wrote on last edited by
          #4

          Its basic maths buddy.. :) Let me try to write a simple app for you. Distance between points (5,5) and the origin (0,0) should be: public CalculateDistance() { double D; int x1=5; int x2=0; int y1=5; int y2=0; D=Math.Abs(((x1-x2)^2)+((y1-y2)^2))^(1/2); Console.WriteLine(D); Console.ReadKey(); } The above should do good. -Anurag

          1 Reply Last reply
          0
          • U User 10235928

            I am looking for my C# class to firstly work out distance between two coordinate points on the x plane and then work out the distance between the same coordinated points on the y plane. There are potentially several ways of doing this. I was thinking: Firstly calculate distance on the x plane (x1->x2): in Excel form:

            if (x2 > x1, x2 - x1, x1 - x2)
            (then) (else)

            Then calculate the distance for the y plane (y1->y2):

            if (x2 > x1, x2 - x1, x1 - x2)

            if we then think of a triangle (with right angle) we have A(squared) + B(squared) = C(squared) We would have the length A and B and therefore would be able to calculate C. Is there an easy way of doing this in C# or is it best to code out the equations? Any advise appreciated.

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Your question states that what you want is the size of the distance between two x co-ordinates, and the size of the distance between two y co-ordinates, but your code suggests that what you really want is the length of a line between the two points: which is the hypotenuse of a right triangle. Assuming it's obvious to you how to get the distances on x, and y, and since you already know how to calculate a hypotenuse, then the answer to your question is: no, C# does not contain a built-in function to calculate a hypotenuse. Yes, you have to code it. And, the real answer to your question is that you need to focus on mastery of the basic mathematics operators in C#, and you need to examine the functions available in the Math library, and get familiar with what it offers, and what it does not offer. A small effort on your part now to learn the basic mathematics operators, and commonly used functions in the Math library, will enable you to express any equation. It's a good idea to think carefully about what you are asking, before you post a question.

            Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups