mitk::Point2D mitk::PlanarCross::InternalApplyControlPointConstraints(unsigned int index, const Point2D &point)
{
  // Apply constraints depending on current interaction state
  switch (index)
  {
    case 2:
    {
      // Check if 3rd control point is outside of the range (2D area) defined by the first
      // line (via the first two control points); if it is outside, clip it to the bounds
      const Point2D p1 = this->GetControlPoint(0);
      const Point2D p2 = this->GetControlPoint(1);

      Vector2D n1 = p2 - p1;
      n1.Normalize();

      const Vector2D v1 = point - p1;
      const double dotProduct = n1 * v1;
      const Point2D crossPoint = p1 + n1 * dotProduct;
      ;
      const Vector2D crossVector = point - crossPoint;

      if (dotProduct < 0.0)
      {
        // Out-of-bounds on the left: clip point to left boundary
        return (p1 + crossVector);
      }
      else if (dotProduct > p2.EuclideanDistanceTo(p1))
      {
        // Out-of-bounds on the right: clip point to right boundary
        return (p2 + crossVector);
      }
      else
      {
        // Pass back original point
        return point;
      }
    }

    case 3:
    {
      // Constrain 4th control point so that with the 3rd control point it forms
      // a line orthogonal to the first line (constraint 1); the 4th control point
      // must lie on the opposite side of the line defined by the first two control
      // points than the 3rd control point (constraint 2)
      const Point2D p1 = this->GetControlPoint(0);
      const Point2D p2 = this->GetControlPoint(1);
      const Point2D p3 = this->GetControlPoint(2);

      // Calculate distance of original point from orthogonal line the corrected
      // point should lie on to project the point onto this line
      Vector2D n1 = p2 - p1;
      n1.Normalize();

      const Vector2D v1 = point - p3;
      const double dotProduct1 = n1 * v1;

      const Point2D pointOnLine = point - n1 * dotProduct1;

      // Project new point onto line [p1, p2]
      const Vector2D v2 = pointOnLine - p1;
      double dotProduct2 = n1 * v2;

      const Point2D crossingPoint = p1 + n1 * dotProduct2;

      // Determine whether the projected point on the line, or the crossing point should be
      // used (according to the second constraint in the comment above)
      if ((pointOnLine.SquaredEuclideanDistanceTo(p3) > crossingPoint.SquaredEuclideanDistanceTo(p3)) &&
          (pointOnLine.SquaredEuclideanDistanceTo(p3) > pointOnLine.SquaredEuclideanDistanceTo(crossingPoint)))
      {
        return pointOnLine;
      }
      else
      {
        return crossingPoint;
      }
    }

    default:
      return point;
  }
}