Draw the shadows of buildings Input:

1
      X                                
      X                                
      X                                
      X      XX    XXXXXX     X X X    
      X      XX    XXXXXX     X X X    
XXX   X      XX    XXXXXX     X X X    
XXX   X      XX    XXXXXX     X X X    


Output:

      X.                               
      X..                              
      X...                             
      X....  XX.   XXXXXX.    X.X.X.   
      X..... XX..  XXXXXX..   X.X.X..  
XXX.  X......XX... XXXXXX...  X.X.X... 
XXX.. X......XX....XXXXXX.... X.X.X....


Input:

2
         XX
         XX
         XX
         XX
         XX
     XX  XX
     XX  XX
     XX  XX
     XX  XX


Output:

        .XX
       ..XX
      ...XX
     ....XX
    .....XX
   ..XX..XX
  ...XX..XX
 ....XX..XX
.....XX..XX


Specification:


You must take as input

A flag signifying whether the light is coming from the top left or top right. This can be 1 or 2, -1 or 1, 0 or 65536, or whatever is convenient for you, as long as both flags are integers.
Rows composed of either X or , all of the same length in characters (i.e. padded with )

All Xs will either be on the last row or have an X under them (meaning no floating buildings)


You must output the rows (buildings) with shadows added. This is done with the following procedure:

If the light is coming from the top left, draw a right triangle of .s with the same height and width as the height of the building, starting from one space past its right edge and going rightwards.
Otherwise, if it's from the top right, do the same thing but start from one space past its left edge and pointing left.
Remember, do not alter Xs by changing them to .s; leave them as they are.
There will always be "room" for your shadows, i.e. if there's a 3-space tall building at the end there will be at least 3 spaces of padding after it.

This is code-golf, so the shortest code in bytes will win!
Perl - 85

BEGIN{$d=-&lt;&gt;}$d?s/X /X./g:s/ X/.X/g;s/ /substr($p,$+[0]+$d,1)eq'.'?'.':$&amp;/ge;$p=$_;


EDIT: I totally forgot about the -p flag this needs to be run with. Added 2 to char count.
The flag specified at the first line is 0 for shadows going left and 2 for shadows going right.