MATLAB: Surface area of a plane within a cube

geometry

Hi,
I'm trying to solve a geometric problem. I have grid blocks which are represented as a cube. I also have rectangles on a plane that are orientated in and around the cube. I need to solve how much of the plane is within the cube.
If these planes were infinite, it would be easy. Since they are not infinite, it's not so trivial. Sometimes the corner points lie within the cube, sometimes outside the cube.
I've posted an example of one possible scenario.
This is a representation of a quartz vein in a block of rock. I'm simulating lots of these so the scenario can be varied.
So far, I check if all 4 points are within the bounds of the cube. If they are, the area is just calculated as is. If one point falls outside the cube (usually this is the case) then I project planes normal to x, normal to y and normal to z and this gives me the line of intersection. From here, I don't know how to solve for area outside the cube.
I'm really stuck on how to solve this issue. Some help would be great.

Best Answer

I don't have a code fragment handy, but a simple way to do this is with the Sutherland-Hodgman clipping algorithm.
Basically, you treat your plane as a ordered list of points and your cube as a set of 6 planes. For each plane, you loop through the list of points keeping track of when they cross the plane, and make a new list of points. You then use that new list for the next plane. When you're done, you've got the polygon which is the intersection of the cube and the plane. In this case, that polygon could be anywhere from empty to 6-sided.
If you google, you'll probably find MATLAB implementations of Sutherland-Hodgman, but it's the simplest of the polygon clipping algorithms to implement if you'd like to give it a go yourself. Note that it's limited to the case where the clipping volume is convex, but that's true of your cube in this case.
Related Question