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. slope of a line

slope of a line

Scheduled Pinned Locked Moved C#
csharpwpfhelpquestion
7 Posts 3 Posters 2 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.
  • N Offline
    N Offline
    Naman2007
    wrote on last edited by
    #1

    I have a text file containing the following content:

    0 12
    1 15
    2 6
    3 4
    4 3
    5 6
    6 12
    7 8
    8 8
    9 9
    10 13

    the first column specifies the x coordinate of a point and second column specifies the y coordinate.i need to read this file and find slope between all points. like first i find slope between (0,12) and (1,15) and then between (0,15) and (2,6) and so on.i am basically building a wpf application.can anyone help?

    A 1 Reply Last reply
    0
    • N Naman2007

      I have a text file containing the following content:

      0 12
      1 15
      2 6
      3 4
      4 3
      5 6
      6 12
      7 8
      8 8
      9 9
      10 13

      the first column specifies the x coordinate of a point and second column specifies the y coordinate.i need to read this file and find slope between all points. like first i find slope between (0,12) and (1,15) and then between (0,15) and (2,6) and so on.i am basically building a wpf application.can anyone help?

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      The basic forumula for a slope is (y2 - y1) / (x2 - x1). Since you have all the co-ordinates, you can put them into an array and then run a loop over the array. You can also input them via a textbox on WPF.

      WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

      N 1 Reply Last reply
      0
      • A Abhinav S

        The basic forumula for a slope is (y2 - y1) / (x2 - x1). Since you have all the co-ordinates, you can put them into an array and then run a loop over the array. You can also input them via a textbox on WPF.

        WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

        N Offline
        N Offline
        Naman2007
        wrote on last edited by
        #3

        i tried to put x and y coordinates in two different arrays but don't understand how to write a code that calculates the slope between points and displays them in a textbox. i have written the following code snippet:

        var r = File.ReadAllLines(path)
        .Select(line => line.Split(' '))
        .Select(arr => new
        {
        Column0 = Int32.Parse(arr[0]),
        Column1 = Int32.Parse(arr[1])
        // etc
        })
        .ToArray();
        int[] column0 = r.Select(x => x.Column0).ToArray();
        int[] column1 = r.Select(x => x.Column1).ToArray();

        A 1 Reply Last reply
        0
        • N Naman2007

          i tried to put x and y coordinates in two different arrays but don't understand how to write a code that calculates the slope between points and displays them in a textbox. i have written the following code snippet:

          var r = File.ReadAllLines(path)
          .Select(line => line.Split(' '))
          .Select(arr => new
          {
          Column0 = Int32.Parse(arr[0]),
          Column1 = Int32.Parse(arr[1])
          // etc
          })
          .ToArray();
          int[] column0 = r.Select(x => x.Column0).ToArray();
          int[] column1 = r.Select(x => x.Column1).ToArray();

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          Try

          for (int i=0; i<=column1.Count - 2;i++)
          {
          float slope = (column1[i+1] - column1[i])/(column0[i+1] - column0[i])
          }

          WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

          L N 2 Replies Last reply
          0
          • A Abhinav S

            Try

            for (int i=0; i<=column1.Count - 2;i++)
            {
            float slope = (column1[i+1] - column1[i])/(column0[i+1] - column0[i])
            }

            WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Shouldn't you cast at least one of the operands of the division to float then?

            1 Reply Last reply
            0
            • A Abhinav S

              Try

              for (int i=0; i<=column1.Count - 2;i++)
              {
              float slope = (column1[i+1] - column1[i])/(column0[i+1] - column0[i])
              }

              WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

              N Offline
              N Offline
              Naman2007
              wrote on last edited by
              #6

              i'm getting 'divide by zero' exception here.what is the method to fix this error?

              A 1 Reply Last reply
              0
              • N Naman2007

                i'm getting 'divide by zero' exception here.what is the method to fix this error?

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #7

                If (x2 - x1) is 0, the slope is infinity. Handle this case in code. Check and make sure (x2 - x1) is not 0 by a simple validation condition.

                WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

                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