0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ $X_2$ and $Y_2$ are not independent. You have a system of two equations in two unknowns. Solve it by back-substitution, if nothing else. $\endgroup$ – amd Feb 5 at 0:51
2
$\begingroup$

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$.

$\endgroup$
  • $\begingroup$ This worked great! I like that it also solves for Y₂, since I'm sure that other people interested in this question may be curious about that as well. $\endgroup$ – Nosajimiki Feb 5 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.