public Shape getSelectedShape(double x, double y, int tolerance,
			double scale) {

		for (int i = shapes.size() - 1; i >= 0; i--) {

			if (shapes.get(i) instanceof Line) {

				Line line = (Line) shapes.get(i);

				double length = Math.sqrt((line.getX2() - line.getX1())
						* (line.getX2() - line.getX1())
						+ (line.getY2() - line.getY1())
						* (line.getY2() - line.getY1()));
				double dx = (line.getX2() - line.getX1()) / length;
				double dy = (line.getY2() - line.getY1()) / length;

				double dotProduct = (x - line.getX1()) * dx
						+ (y - line.getY1()) * dy;

				if (0 <= dotProduct && dotProduct <= length) {

					double projectionX = line.getX1() + dotProduct * dx;
					double projectionY = line.getY1() + dotProduct * dy;

					if ((x - projectionX) * (x - projectionX)
							+ (y - projectionY) * (y - projectionY) <= (tolerance / scale)
							* (tolerance / scale)) {
						return line;
					}
				}

				continue;
			}

			double originalX = x;
			double originalY = y;

			Point2D point = Transformation.worldToObject(shapes.get(i), x, y);
			x = point.getX();
			y = point.getY();

			if (shapes.get(i) instanceof Square) {

				Square square = (Square) shapes.get(i);

				if (Math.abs(x) <= square.getSize() / 2
						&& Math.abs(y) <= square.getSize() / 2) {
					return square;
				}
			} else if (shapes.get(i) instanceof Rectangle) {

				Rectangle rectangle = (Rectangle) shapes.get(i);

				if (Math.abs(x) <= rectangle.getWidth() / 2
						&& Math.abs(y) <= rectangle.getHeight() / 2) {
					return rectangle;

				}

			} else if (shapes.get(i) instanceof Circle) {

				Circle circle = (Circle) shapes.get(i);

				if ((x) * (x) + (y) * (y) <= circle.getRadius()
						* circle.getRadius()) {
					return circle;
				}

			} else if (shapes.get(i) instanceof Ellipse) {

				Ellipse ellipse = (Ellipse) shapes.get(i);

				if ((x)
						* (x)
						/ ((ellipse.getWidth() / 2) * (ellipse.getWidth() / 2))
						+ (y)
						* (y)
						/ ((ellipse.getHeight() / 2) * (ellipse.getHeight() / 2)) <= 1) {
					return ellipse;
				}

			} else if (shapes.get(i) instanceof Triangle) {

				Triangle triangle = (Triangle) shapes.get(i);

				double normalX = -(triangle.getY2() - triangle.getY1());
				double normalY = triangle.getX2() - triangle.getX1();
				double dotProduct1 = normalX * (x - triangle.getX1()) + normalY
						* (y - triangle.getY1());

				normalX = -(triangle.getY3() - triangle.getY2());
				normalY = triangle.getX3() - triangle.getX2();
				double dotProduct2 = normalX * (x - triangle.getX2()) + normalY
						* (y - triangle.getY2());

				normalX = -(triangle.getY1() - triangle.getY3());
				normalY = triangle.getX1() - triangle.getX3();
				double dotProduct3 = normalX * (x - triangle.getX3()) + normalY
						* (y - triangle.getY3());

				if (dotProduct1 >= 0 && dotProduct2 >= 0 && dotProduct3 >= 0) {
					return triangle;
				} else if (dotProduct1 <= 0 && dotProduct2 <= 0
						&& dotProduct3 <= 0) {
					return triangle;
				}
			}

			x = originalX;
			y = originalY;
		}

		return null;
	}