I'm attempting to determine if one four sided shape is above another one, and returning the ratio of the amount that is uncovered. My definition of "uncovered" is if there is no other shape above the one being examined on the y-axis, and if there is how much they are intersecting on the x-axis.
Example:
In the above image, the desired output should be 0.6363. Out of the 110 pixels, 70 are intersecting with the object above it, and 70/110=0.6363
Another Example:
In this example the desired output would be 0.3636.
What I have attempted so far is that I am starting with double out = 1; and then if the y-axis of shape 2 is less than shape 1, I subtract the amount the two shapes intersect on the x axis from the ratio with some variant of out-=(c.getX2()-b.getX2())/Math.abs(c.getX1()-c.getX2());
However, this doesn't seem to be working, and my attempts at correcting the code seem to just be adding more and more unnecessary complexity. I assume there is a much easier way to do what I'm trying to do, but I'm not great with geometry.
c is the current shape being examined, and b is the one it is being compared to.
if(((b.getX2() < c.getX2()) && (b.getX2()>c.getX1()))||((b.getX2()>c.getX2())&&(b.getX2()<c.getX1()))||((b.getX1()>c.getX2())&&(b.getX1()<c.getX1()))) {
if(b.getY2() < c.getY2()) {
if((b.getX2() < c.getX2()) && (b.getX2()>c.getX1())) {
out-= (c.getX2()-b.getX2())/Math.abs(c.getX1()-c.getX2());
}
if(((b.getX2()>c.getX2())&&(b.getX2()<c.getX1()))) {
out-=(b.getX2()-c.getX2())/Math.abs(c.getX1()-c.getX2());
}
if(((b.getX1()>c.getX2())&&(b.getX1()<c.getX1()))) {
out-=(c.getX2()-b.getX2())/Math.abs(c.getX1()-c.getX2());
}
}
}


I think your approach is way to complicated, and you might just have got lost in all the different possible conditions and configurations.
If I understood you correctly, then the simplest solution would be to base the computations on the minimum and maximum X-values of both shapes.
Side note:
You did not say what type your "shape" objects
bandcare. From the methods that you are calling, they might be ajava.awt.geom.Line2Dobjects, but these are not really "four sided". In any case, you can compute the minimum and maximum values asIn the program below, I'm using actual
Shapeobjects and call thegetBounds2Dmethod to obtain the bounding boxes, which offer these min/max values conveniently. But you can also do this manually.When you have these minimum/maxmimum values, you can rule out the cases where an object is either entirely left or entirely right of the other object. If this is not the case, they are overlapping. In this case, you can compute the minimum and maximum of the overlap, and then divide this by the width of the object in question.
Here is a program where a example objects are created based on the coordinates you gave in the question. You can drag around the objects with the mouse. The overlap is painted and its value is printed.
The computation takes place in the
computeOverlapmethod.