I am working on a project in Matlab
and you need to find the area between two lines inside the square [-1,+1]x[-1,+1]
intersecting at the point (xIntersection,yIntersection)
. Thus, the idea is to subtract two lines and integrate between [-1, xIntersection] and [xIntersection, +1], summarize the results and, if they are negative, change your sign.
For more information on how to find the intersection of two lines, check out this link.
I use Matlab's
integral()
function, here is a code snippet:
xIntersection = ((x_1 * y_2 - y_1 * x_2) * (x_3 - x_4) - (x_1 - x_2) * (x_3 * y_4 - y_3 * x_4) ) / ((x_1 - x_2) * (y_3 - y_4) - (y_1 - y_2) * (x_3 - x_4)); d = @(x) g(x) - f(x); result = integral(d, -1, xIntersection) - int( d, xIntersection, 1) if(result < 0), result = result * -1; end
Please note that I defined g(x)
and f(x)
earlier in the code, but did not report this in the snippet.
The problem is that I soon realized that lines can intersect either inside or outside the square, in addition, they can intersect the square on either side of it, and the number of possible combinations grows very quickly.
Ie:
![enter image description here](https://fooobar.com//img/a48044ff281d747ad73f2a3a9f30a4a0.jpg)
![enter image description here](https://fooobar.com//img/8bcdaf034198265edf0c7d23760f81c4.jpg)
![enter image description here](https://fooobar.com//img/11c928fb823b42de05f3f0414d6c5a52.jpg)
![enter image description here](https://fooobar.com//img/38cb482e5ac85590ea10063440aeceb7.jpg)
These are just 4 cases, but considering that f (+1), f (-1), g (+1), g (-1) can be inside the interval [-1, + 1], above or below it, and that the intersection may be inside or outside the square, the total number is 3 * 3 * 3 * 3 * 2 = 162.
Obviously, in each case, the explicit function for integration in order to get the area between the two lines is different, but I can’t think of writing a switch case for each of them.
Any ideas?