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 / C++ / MFC
  4. Determine straight line

Determine straight line

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studioquestion
13 Posts 7 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.
  • T Offline
    T Offline
    tagopi
    wrote on last edited by
    #1

    Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

    L A A P C 5 Replies Last reply
    0
    • T tagopi

      Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

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

      The problem is one of mathematics not programming; once you find the mathematical solution, converting it to C/C++ is the easy part.

      One of these days I'm going to think of a really clever signature.

      T 1 Reply Last reply
      0
      • T tagopi

        Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

        A Offline
        A Offline
        Amarnath S
        wrote on last edited by
        #3

        One way would be by using slope. Find the slope of the line corresponding to the first two points (y-coord of p2 - y-coord of p1) / (x-coord of p2 - x-coord of p1). Find the slopes of the lines corresponding to all other points (with respect to the first point), say (y-coord of pk - y-coord of p1) / (x-coord of pk - x-coord of p1), where k varies from 3 to n. All of these slopes should be within a reasonable tolerance band. And, since all of these lines pass through p1, they all would lie on the same straight line if they fell within the tolerance band. The above would not work if the x-coordinates of p2 and p1 are the same (because denominator would be zero). In that case, you choose a point whose x-coordinate is unique as the reference point. Not sure whether this is the most efficient way, but it certainly a way of achieving what you want.

        T 1 Reply Last reply
        0
        • L Lost User

          The problem is one of mathematics not programming; once you find the mathematical solution, converting it to C/C++ is the easy part.

          One of these days I'm going to think of a really clever signature.

          T Offline
          T Offline
          tagopi
          wrote on last edited by
          #4

          Hello Richard, Yes, i know its a mathematics question, i tried here so that someone can help me. Thanks for the reply. Regards, A. Gopinath.

          1 Reply Last reply
          0
          • A Amarnath S

            One way would be by using slope. Find the slope of the line corresponding to the first two points (y-coord of p2 - y-coord of p1) / (x-coord of p2 - x-coord of p1). Find the slopes of the lines corresponding to all other points (with respect to the first point), say (y-coord of pk - y-coord of p1) / (x-coord of pk - x-coord of p1), where k varies from 3 to n. All of these slopes should be within a reasonable tolerance band. And, since all of these lines pass through p1, they all would lie on the same straight line if they fell within the tolerance band. The above would not work if the x-coordinates of p2 and p1 are the same (because denominator would be zero). In that case, you choose a point whose x-coordinate is unique as the reference point. Not sure whether this is the most efficient way, but it certainly a way of achieving what you want.

            T Offline
            T Offline
            tagopi
            wrote on last edited by
            #5

            Hello Amarnath, yes, i tried this method also, but iam getting like 0.000002 or something like difference which fails the condition checking. rounding off will solve the solution ? is that right method? Regards, A. Gopinath.

            A 2 Replies Last reply
            0
            • T tagopi

              Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

              A Offline
              A Offline
              Albert Holguin
              wrote on last edited by
              #6

              Typically this is where you'd post questions regarding algorithms: http://www.codeproject.com/Forums/326859/Algorithms.aspx[^]

              T 1 Reply Last reply
              0
              • T tagopi

                Hello Amarnath, yes, i tried this method also, but iam getting like 0.000002 or something like difference which fails the condition checking. rounding off will solve the solution ? is that right method? Regards, A. Gopinath.

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

                You mean, that your slope value is 0.000002? This means that your line is almost horizontal. Your tolerance values would have to be at least 1/1000 or 1/10000 of your "average" slope value. Then, it would indicate that these points are on a straight line. (You need to use a "double" type to store these numbers, and a "float" may not suffice). By the way, what is the purpose of this exercise? That purpose would in some sense determine the method of straight-line detection.

                T 1 Reply Last reply
                0
                • A Amarnath S

                  You mean, that your slope value is 0.000002? This means that your line is almost horizontal. Your tolerance values would have to be at least 1/1000 or 1/10000 of your "average" slope value. Then, it would indicate that these points are on a straight line. (You need to use a "double" type to store these numbers, and a "float" may not suffice). By the way, what is the purpose of this exercise? That purpose would in some sense determine the method of straight-line detection.

                  T Offline
                  T Offline
                  tagopi
                  wrote on last edited by
                  #8

                  Hello, i will try using "double" type. actually this is related 3D project, need to find straight line or not. Thanks again, A. Gopinath.

                  E 1 Reply Last reply
                  0
                  • A Albert Holguin

                    Typically this is where you'd post questions regarding algorithms: http://www.codeproject.com/Forums/326859/Algorithms.aspx[^]

                    T Offline
                    T Offline
                    tagopi
                    wrote on last edited by
                    #9

                    Thanks Albert, hereafter i will use this page to ask questions related. Regards, A. Gopinath.

                    1 Reply Last reply
                    0
                    • T tagopi

                      Hello Amarnath, yes, i tried this method also, but iam getting like 0.000002 or something like difference which fails the condition checking. rounding off will solve the solution ? is that right method? Regards, A. Gopinath.

                      A Offline
                      A Offline
                      Amarnath S
                      wrote on last edited by
                      #10

                      One more way would be by using the cross product. Take two points, say p1 and p2 as reference. Consider the vector p1-p2. Now, take a point pk, and hence that vector p1-pk. Find the cross product of these two vectors (p1-p2 and p1-pk). That cross product should be near zero (all three components of the cross product) if the points are collinear. Repeat this for all points pk (k belonging to the set 3 to n).

                      1 Reply Last reply
                      0
                      • T tagopi

                        Hello, i will try using "double" type. actually this is related 3D project, need to find straight line or not. Thanks again, A. Gopinath.

                        E Offline
                        E Offline
                        Eugen Podsypalnikov
                        wrote on last edited by
                        #11

                        - Take the first and last points to build the 3D law of their straightness - Define the fluctuation radius as "allowed error" - All other points must lie in the "pipe" => => any point projection onto the law(1) may not be greather as the radius(2) :)

                        They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

                        1 Reply Last reply
                        0
                        • T tagopi

                          Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

                          P Offline
                          P Offline
                          pasztorpisti
                          wrote on last edited by
                          #12

                          With some algorithm you should pick 2 points that are far enough from each other. After this you can create a line object using these 2 points. The line object can be used to easily determine the distance of the other points from this line and you can accept distances less then a defined epsilon value. An O(n) algorithm to determine the 2 endpoints (assuming that the points are in a line) before your create the line object: Initialize an empty bounding AABB and then expand it by adding all points. Declare six point variables and each of them should store the last point that was used to expand the AABB in +x,-x,+y,-y,+z,-z directions (note: the first point should fill all the six variables). When this is done choose a dimension in which the size of the AABB is the biggest (x, y, or z). If the biggest dimension is zero (or near zero) then all points are located at the same spot. Lets say its x: in this case the two endpoints (from the previously mentioned 6 point variables) are the points that were stored when the AABB was expanded last time in -x and +x directions.

                          1 Reply Last reply
                          0
                          • T tagopi

                            Hello everybody, i am having n-number of points like p1(x,y), p2(x,y), p3(x,y), .... pn(x,y). All points lie on a single line.(either a curve or a straight line). i would like to find, using those points, whether that line is straight or not straight. i googled and tried some of the methods given in examples, (straight line equation, finding if collinear) but those are not working fine even for straight line also. i am using visual studio 2008 with win32 (not MFC). Any ideas ? Thanks in advance, A. Gopinath.

                            C Offline
                            C Offline
                            CPallini
                            wrote on last edited by
                            #13

                            That it's simple: if all the points are on a straight line then use two of them to compute the straight line equation. Once you have such equation, test if the other points lie on the line too.

                            Veni, vidi, vici.

                            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