I'm looking for solve for this task in c#.
There is an X and Y coordinate system. The task is to find the equation that matches the red line at the same angle and print the value of this line through the specified number of steps on the X scale.
Image below is an example of what I need to get. Given these points
For example:
Also if you can, explain please the code. The problem is that line in task could be at random angle in each task.
If you want to get a strait line, you can put
private static Func<double, double> Linear(double x1, double y1, double x2, double y2) {
if (x1 == x2) // Special case : vertical line
if (y1 == y2)
return (x) => y1 + x - x1;
else
return (x) => x == x1 ? 0.0 : double.NaN;
else
return (x) => y1 + (y2 - y1) / (x2 - x1) * (x - x1);
}
Demo (single point)
var func = Linear(1, 10, 10, 50);
Console.Write(func(20));
Outcome:
94.4444444444444
Table:
string report = string.Join(Environment.NewLine, Enumerable
.Range(0, 21)
.Select(x => $"{x,2} : {func(x)}"));
Console.Write(report);
Outcome:
0 : 5.55555555555556
1 : 10 <- Given Point
2 : 14.4444444444444
3 : 18.8888888888889
4 : 23.3333333333333
5 : 27.7777777777778
6 : 32.2222222222222
7 : 36.6666666666667
8 : 41.1111111111111
9 : 45.5555555555556
10 : 50 <- Given Point
11 : 54.4444444444444
12 : 58.8888888888889
13 : 63.3333333333333
14 : 67.7777777777778
15 : 72.2222222222222
16 : 76.6666666666667
17 : 81.1111111111111
18 : 85.5555555555556
19 : 90
20 : 94.4444444444444 <- Required Point