Linear line
-
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!
-
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!
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
-
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!
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.
-
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
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] }
-
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.
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] }
-
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] }
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.
-
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
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?
-
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] }
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.
-
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?
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
-
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
ahh-ahhh..... the penny has dropped. Thanks again.
-
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!
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
-
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!
:)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
-
:)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
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
-
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!
-
To understand if a line is 'approzimately' a line you can use the
Hough transform
. Hope helps
Russell
-
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!
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...