You can also do this by creating BSP trees for each of your polygons lines.
From those trees you can send each line down the other's tree - splitting it where is intersects and keeping/throwing away the portions outside/inside.
Then you put all lines left into a new polygon by matching up joining ends and finally reduce the lines by joining those which are inline.
There may be more efficient methods like Dreamora says, I expect there is a more efficient scanline method, but I personally have done this with BSP without much fuss. (C++ I'm afraid and the code doesn't belong to me)
From those trees you can send each line down the other's tree - splitting it where is intersects and keeping/throwing away the portions outside/inside.
Then you put all lines left into a new polygon by matching up joining ends and finally reduce the lines by joining those which are inline.
There may be more efficient methods like Dreamora says, I expect there is a more efficient scanline method, but I personally have done this with BSP without much fuss. (C++ I'm afraid and the code doesn't belong to me)