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. Algorithms
  4. Linear line

Linear line

Scheduled Pinned Locked Moved Algorithms
c++helpquestion
16 Posts 7 Posters 7 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.
  • F Offline
    F Offline
    flippydeflippydebop
    wrote on last edited by
    #1

    Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

    D CPalliniC H R S 6 Replies Last reply
    0
    • F flippydeflippydebop

      Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      EDIT: Not sure if this is needed or not: if the slope between x_1,y_1 and x_5,y_5 is greater than 1, swap the x and y values for each coordinate pair. This swap is needed to keep the delta value in the next step smaller than 1. Create a function for the line y = f(x) using points 1 and 5. Then of each of the 3 points in the middle calculate f(x_n), if ABS(f(x_n) - y_n) < 1, then x_n,y_n is on on the line (give or take the rounding error from forcing integral values).

      -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

      L F 2 Replies Last reply
      0
      • F flippydeflippydebop

        Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Try to apply Linear Regression to your points. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • D Dan Neely

          EDIT: Not sure if this is needed or not: if the slope between x_1,y_1 and x_5,y_5 is greater than 1, swap the x and y values for each coordinate pair. This swap is needed to keep the delta value in the next step smaller than 1. Create a function for the line y = f(x) using points 1 and 5. Then of each of the 3 points in the middle calculate f(x_n), if ABS(f(x_n) - y_n) < 1, then x_n,y_n is on on the line (give or take the rounding error from forcing integral values).

          -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi Dan, Same idea, but without any math showing, the lazy approach: - Create a GraphicsPath - add the line connecting the end points - widen with a pen (I guess width=3, more gives more tolerance) - check all points fall within (that's GraphicsPath.IsVisible) :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          F 1 Reply Last reply
          0
          • CPalliniC CPallini

            Try to apply Linear Regression to your points. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi CPallini, it depends on what the points represent: - for ideal (x,y) points, yes - for graphic approximation (such as Bresenham), not so good Greetings

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            CPalliniC 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi CPallini, it depends on what the points represent: - for ideal (x,y) points, yes - for graphic approximation (such as Bresenham), not so good Greetings

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              It appears to me like a mathematical problem rather than a (digital) graphics one. Anyway, only the OP can clarify the point (I hope!) :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • D Dan Neely

                EDIT: Not sure if this is needed or not: if the slope between x_1,y_1 and x_5,y_5 is greater than 1, swap the x and y values for each coordinate pair. This swap is needed to keep the delta value in the next step smaller than 1. Create a function for the line y = f(x) using points 1 and 5. Then of each of the 3 points in the middle calculate f(x_n), if ABS(f(x_n) - y_n) < 1, then x_n,y_n is on on the line (give or take the rounding error from forcing integral values).

                -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                F Offline
                F Offline
                flippydeflippydebop
                wrote on last edited by
                #7

                Cheers Dan. This is great.. Im just a little confused about the functions f(x) and f(x_n). What is the body of these functions?

                D 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi Dan, Same idea, but without any math showing, the lazy approach: - Create a GraphicsPath - add the line connecting the end points - widen with a pen (I guess width=3, more gives more tolerance) - check all points fall within (that's GraphicsPath.IsVisible) :)

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  F Offline
                  F Offline
                  flippydeflippydebop
                  wrote on last edited by
                  #8

                  Hiya Luc. Thanks also for your reply. This is an interesting approach here, however GraphicsPath is a .net class while i am using native C++/MFC.

                  1 Reply Last reply
                  0
                  • F flippydeflippydebop

                    Cheers Dan. This is great.. Im just a little confused about the functions f(x) and f(x_n). What is the body of these functions?

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #9

                    The same function, f(x) is the generic function, x_n is x_1, x_2,... (the x points of your test coordinates). Probably best done using the slope-intercept form. Google if you need help getting the values from two points.

                    -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                    F 1 Reply Last reply
                    0
                    • D Dan Neely

                      The same function, f(x) is the generic function, x_n is x_1, x_2,... (the x points of your test coordinates). Probably best done using the slope-intercept form. Google if you need help getting the values from two points.

                      -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                      F Offline
                      F Offline
                      flippydeflippydebop
                      wrote on last edited by
                      #10

                      ahh-ahhh..... the penny has dropped. Thanks again.

                      1 Reply Last reply
                      0
                      • F flippydeflippydebop

                        Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

                        H Offline
                        H Offline
                        Habib Ahmed Bhutto
                        wrote on last edited by
                        #11

                        hi flippydeflippydebop :doh: "entering 5 readings" u mean input 5 values then in each reading u will get a point's value mean(x,y) it means int x[5], y[5] for( i=0; i<5; i++ ) read x[i], y[i] resultX = x[0] - x[1] - x[2] - x[3] - x[4] resultY = y[0] - y[1] - y[2] - y[3] - y[4] if( ( resultX == 0 ) and ( resultY == 0 ) ) print "line is linear" else print "line is not linear" i think this is the solution if it is true plz inform me if it is wrong then also inform me plz.. in case it is true u can further enhance and can be maked easy and powerful then this. plz inform me about its correctness

                        habib bhutto

                        1 Reply Last reply
                        0
                        • F flippydeflippydebop

                          Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

                          H Offline
                          H Offline
                          Habib Ahmed Bhutto
                          wrote on last edited by
                          #12

                          :)hi flippydeflippydebop :doh: "entering 5 readings" u mean input 5 values then in each reading u will get a point's value mean(x,y) it means int x[5], y[5] for( i=0; i<5; i++ ) read x[i], y[i] resultX = x[0] - x[1] - x[2] - x[3] - x[4] resultY = y[0] - y[1] - y[2] - y[3] - y[4] if( ( resultX == 0 ) and ( resultY == 0 ) ) print "line is linear" else print "line is not linear" i think this is the solution if it is true plz inform me if it is wrong then also inform me plz.. in case it is true u can further enhance and can be maked easy and powerful then this. plz inform me about its correctness

                          habib bhutto

                          D 1 Reply Last reply
                          0
                          • H Habib Ahmed Bhutto

                            :)hi flippydeflippydebop :doh: "entering 5 readings" u mean input 5 values then in each reading u will get a point's value mean(x,y) it means int x[5], y[5] for( i=0; i<5; i++ ) read x[i], y[i] resultX = x[0] - x[1] - x[2] - x[3] - x[4] resultY = y[0] - y[1] - y[2] - y[3] - y[4] if( ( resultX == 0 ) and ( resultY == 0 ) ) print "line is linear" else print "line is not linear" i think this is the solution if it is true plz inform me if it is wrong then also inform me plz.. in case it is true u can further enhance and can be maked easy and powerful then this. plz inform me about its correctness

                            habib bhutto

                            D Offline
                            D Offline
                            Dan Neely
                            wrote on last edited by
                            #13

                            won't work. points 10,10 11,11 12,12 13,13 14,14. Obviously linear, but resultx and resulty are both -50.

                            -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                            1 Reply Last reply
                            0
                            • F flippydeflippydebop

                              Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

                              R Offline
                              R Offline
                              Russell
                              wrote on last edited by
                              #14

                              To understand if a line is 'approzimately' a line you can use the Hough transform. Hope helps


                              Russell

                              R 1 Reply Last reply
                              0
                              • R Russell

                                To understand if a line is 'approzimately' a line you can use the Hough transform. Hope helps


                                Russell

                                R Offline
                                R Offline
                                Russell
                                wrote on last edited by
                                #15

                                here is a link to the algorithm. http://en.wikipedia.org/wiki/Hough_transform[^] :-D


                                Russell

                                1 Reply Last reply
                                0
                                • F flippydeflippydebop

                                  Hiya guys.. Using C++ does anyone know how i can tell if a line is linear or not? I want the user to enter 5 readings which i can plot on a chart. However i would like a function to find out if the plotted line is linear. Any help would be greatly appreciated!

                                  S Offline
                                  S Offline
                                  stevepqr
                                  wrote on last edited by
                                  #16

                                  You could use the equation of a line y=mx+c, we don't know the value of c (and we can't calculate it because the line might not be straight) but we can force it to be zero by shifting all the x,y coordinates by the same amount such that the first point is 0,0 now c=0 and we can calculate m for each point, m=y/x (except for the first point since this will result in divide by zero!) If the line is straight then the value for m will be the same for each point. Should be relatively easy and quick to code... :)

                                  Apathy Rules - I suppose...

                                  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