How to tell the run of a line segment where the area underneath it, starting point, and slope are known.

47 Views Asked by At

I have a program where I am currently solving for the difference of X₁ and X₂ of a line-segment where the area under the line-segment (A), the slope (s), and the starting point (X₁,Y₁) are known.

Example: For A=65, s=0.5, X₁=8, Y₁=4, X₂=?, Y₂=? Find X₂

example graph

The algorithm I am using works by recursively assigning bigger and bigger displacements of X₂ using A=(Y₁+Y₂)/2*(X₂-X₁) to see if see if my target area is between its current iteration and previous iteration, and once it finds an X range that contains the target area, it recursively cuts the X range in half adjusting the range into either the upper or lower half of the previous range until it digs down to a range small enough to get the integer of (X₂-X₁) for the target area.

Much like sorting functions, this works fine on a small scale, but can bog down a computer as you start to need to run this function on a lot of data-sets over a short period of time.

Is there a way to solve for this with either an equation, or a algorithm that does not need so many passes?

1

There are 1 best solutions below

1
On BEST ANSWER

Assuming the area is between the line and the x axis and $x_1$ and $y_1$ can be non integer.

$$\frac{y_2 - y_1}{x_2 - x_1} = s$$

$$A = \frac{(y_1 + y_2)(x_2 - x_1)}{2} = \frac{(y_1 + y_2)}{2}\frac{(y_2 - y_1)}{s} = \frac{y_2^2 - y_1^2}{2s}$$

$$y_2 = \sqrt{y_1^2 + 2As}$$

$$x_2 = \frac{(y_2 - y_1)}{s} + x_1 = \frac{\sqrt{y_1^2 + 2As} - y_1}{s} + x_1$$

If you want the nearest integer solution you can pick $[x_2]$ and $[y_2]$ or $[x_2] + 1$ and $[y_2] + 1$.