added JigsawPuzzle#findClosest(Piece) for use in Puzzle#createTarget(..) (it's awesome now)
1 files changed, 17 insertions(+), 1 deletions(-)

M core/src/net/dermetfan/jigsawPuzzle/puzzle/JigsawPuzzle.java
M core/src/net/dermetfan/jigsawPuzzle/puzzle/JigsawPuzzle.java +17 -1
@@ 70,6 70,22 @@ public class JigsawPuzzle {
 			pieces.add(piece);
 	}
 
+	/** @param piece the piece to which to find the closest other piece */
+	public Piece findClosest(Piece piece) {
+		float distance = Float.POSITIVE_INFINITY;
+		Piece closest = null;
+		for(Piece other : pieces) {
+			if(other == piece)
+				continue;
+			float dist = Vector2.dst(other.getX() + other.getSlotX(), other.getY() + other.getSlotY(), piece.getX() + piece.getSlotX(), piece.getY() + piece.getSlotY());
+			if(dist < distance) {
+				distance = dist;
+				closest = other;
+			}
+		}
+		return closest;
+	}
+
 	/** @param tolerance the distance by which each piece is allowed to be off
 	 *  @return if the puzzle is solved */
 	public boolean isSolved(float tolerance) {

          
@@ 140,7 156,7 @@ public class JigsawPuzzle {
 				Actor dragged = payload.getDragActor();
 				Scene2DUtils.addAtStageCoordinates(dragged, board);
 				if(dragged instanceof Piece) {
-					Piece piece = (Piece) dragged, ref = pieces.first();
+					Piece piece = (Piece) dragged, ref = findClosest(piece);
 					if(piece.isPlacedCorrectly(ref, Constants.tolerance)) {
 						piece.place(ref); // snap it into its perfect position in relation to some already placed piece
 						if(isSolved(Constants.tolerance) && solvedCallback != null)