10
The Task
Write a function L() that takes two Tuple arguments of coordinates in the form (x, y), and returns their respective linear function in the form (a, c), where a is the co-efficent of the x term and c is the y-intercept.
You can assume that the input will not be a line perpendicular the the x axis, and that the two inputs are separate points.
Scoring
This is Code Golf: shortest program wins.
Please Note: No use of any mathematical functions apart from basic operators (+,-,/,*).
Example
Here is my un-golfed solution in Python.
def L(Point1, Point2):
x = 0
y = 1
Gradient = (float(Point1[y]) - float(Point2[y])) / (float(Point1[x]) - float(Point2[x]))
YIntercept = Point1[y] - Gradient * Point1[x]
return (Gradient, YIntercept)
Output:
>>> L( (0,0) , (1,1) )
(1.0, 0.0)
>>> L( (0,0) , (2,1) )
(0.5, 0.0)
>>> L( (0,0) , (7,1) )
(0.14285714285714285, 0.0)
>>> L( (10,22.5) , (5,12.5) )
(2.0, 2.5)
4
L( (0,0) , (0,1) )
? – Howard – 2014-05-12T12:57:48.3101You can assume that the input is not a line parallel to the X axis. – Harry Beadle – 2014-05-12T13:25:17.387
2You can assume that the input is not a line parallel to the X axis. Do you mean Y axis? – Howard – 2014-05-12T13:53:09.097
Sorry, the edit on the post was correct, perpendicular to the X axis. – Harry Beadle – 2014-05-12T13:55:04.810
2
L((0,0),(0,0))
? – user12205 – 2014-05-12T13:58:20.070You guys are so good at digging holes... Two inputs must be different. – Harry Beadle – 2014-05-12T13:59:38.870
This is extremely close to being a cut-down version of Shamir's secret-sharing.
– Peter Taylor – 2014-05-12T14:17:00.457@PeterTaylor This is also close to being a duplicate of http://codegolf.stackexchange.com/questions/5429/rosetta-stone-challenge-find-the-rule-for-a-series.
– Howard – 2014-05-12T16:07:15.303