# assuming polygon is 1-2-3-(4=1 implied) for a square #Get both shapes going in the same direction # to make things simpler later Order array of shape 1 so that its a clockwise closed shape Order array of shape 2 so that its a clockwise closed shape foreach line in shape 1 { foreach line in shape 2 { are line1 and line two the same { store which line this is. } } } splice into array of shape1 at point of common line all of shape 2 removing the common line.