-2

I'm looking for solve for this task in .

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

  • P1 (1/10)
  • P2 (10/50)

Example

For example:

  • What will be Y value when the X value will go to 20?
  • How can I code it?

Also if you can, explain please the code. The problem is that line in task could be at random angle in each task.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Shinkov
  • 21
  • 5
  • 1
    please provide some code of your approaches to get better help with – Marco Rehmer Jan 09 '20 at 12:11
  • I would love to, but unfortunately I'm only learning programming. I don't have any ideas how to do this :( – Shinkov Jan 09 '20 at 12:13
  • 1
    And what does your knowledge about straight lines in mathematics tell you? (or in [geometry](https://en.wikipedia.org/wiki/Line_(geometry))) – Luuk Jan 09 '20 at 12:15
  • Could you do this on a piece of paper? If not, it's not a programming problem. Computers cannot do magic. You will need to figure out how to do this yourself, only then can you come up with a way to make a computer do it. – nvoigt Jan 09 '20 at 12:20
  • I only know the equation of a line y = mx + c . But how to use in the code. I know that I need to use it to calculate the next value. But don't know how exactly – Shinkov Jan 09 '20 at 12:23
  • Do I need to calculate ratio between 50 & 10 ( divide 50/10 = 5 ) and then multiply result on the 20 (as in example). I will get value of 100. Is it that point? – Shinkov Jan 09 '20 at 12:27

2 Answers2

0

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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Based on Equation of a line from 2 points the slope can be calcualted using

Slope m

m  =  (change in y) / (change in x)
m  =  (y2 − y1)     / (x2 − x1)
m  =  (50 - 10)     / (10 - 1) 
m =     40          / 9         = 40/9

The "point-slope" form of the equation of a straight line is

y − y1 = m(x − x1)  // we will use P1(1/10) with x1 = 1, y1 = 10
y - 10 = m(x - 1)
y      = m(x - 1) + 10 

Test the equation with P2 (10/50)

50      = m(10-1) + 10  // y = 50 and x = 10
40      = m(9)
40/9    = m 

To get a function that represents the equation (y = f(x))

y    = m(x - 1) + 10  // we know m = 40/9 and use f(x) instead of y
f(x) = 40/9(x - 1) + 10 
f(x) = (40/9)x - 40/9 + 90/9 
f(x) = (40/9)x + 90/9 - 40/9 
f(x) = (40/9)x + 50/9

Create a matching function

Func<decimal, decimal> lineEquation = (x) =>{
    return ((decimal)40/(decimal)9)*x + ((decimal)50/(decimal)9);
};

Test it in

Linqpad Demo

surfmuggle
  • 5,527
  • 7
  • 48
  • 77