I am running simulations, and the output will be a line y = mx+b. I am interested in the area below the line between x=0 and x=1. I am only interested in the area that is below the diagonal y = x.

I have figured out how to determine this area by finding areas of triangles. But to do so, I have to define 6 cases. This requires many if-else statements in my computer program, and is inefficient.

I was wondering if there is an elegant solution to this problem which will not require such a complex program?

In the diagram below, the diagonal line is solid, my line of interest is the dotted line.

enter image description here


Let $y = mx+b$ be the equation of your line, and then find the point of intersection with $y=x$, which will be $(p,p)$.

Then you need to find two integrals: the integral from $0$ to $p$ of $x-(mx+b)$ and the integral from $p$ to $1$ of the same function. Then pick whichever one is nonnegative!

EDIT: Just realized the lines don't have intersect. If that happens, just take the integral from $0$ to $1$ of $x-(mx+b)$ and if it's negative, return 0.

I guess I only simplified a few cases.


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.