This isn't a full solution, but it does show how to position a square or triangle to another square or triangle. I did in in a random way for testing, so it's not interactive like you're after yet.
All the sides of the shapes need to be the same length. So for the triangle it's height= sqrt(3)/2*width. Except I rotated it to point right to make some of the math easier.
Also the sprite's origins need to be in the center of the shapes. The triangle's should be (width/3, height/2)
The angle from the center of the shape to the sides would be
sprite.angle+(0,90,180 or 270) for the square
sprite.angle+(60,180 or 300) for the triangle
and the distance from the center of the shape to the edge is width/2 for the square and sqrt(3)/6*height for the triangle.
To position shape B onto an edge of shape A you'd
1. position B onto A
2. set B's angle to an angle to to edge of A
3. move B forward by A's distance to center
4. move B forward by B's distance to center
The final step is to see if the new shape actually fits. We do that with an overlap check between the new object and all the existing ones. In the capx I'm purposely spacing things since objects in contact count as overlapping but that gap can be removed once the new shape is found to fit.
dropbox.com/s/eybebzudn59vns6/box_tri.capx
To make it interactive you'd want to do basically oosyrag's idea. Loop over all the shapes, and create new ones at every edge. They stay marked as new and are only added when clicked on, which marks that as new and deletes all the other new ones.