Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

Node * CNItem::getClosestNode ( const QPoint &  pos  )  [inherited]

Returns the closest node that is associated with the CNItem

Definition at line 300 of file cnitem.cpp.

Referenced by ConnectorDraw::grabEndStuff().

{
      // Work through the nodes, finding the one closest to the (x, y) position
      Node *shortestNode = 0L;
      double shortestDistance = 1e10; // Nice large distance :-)
      
      const NodeMap::iterator end = m_nodeMap.end();
      for ( NodeMap::iterator it = m_nodeMap.begin(); it != end; ++it )
      {
            Node *node = p_icnDocument->nodeWithID(it.data().id);
            if (node)
            {
                  // Calculate the distance
                  // Yeah, it's actually the distance squared; but it's all relative, so doesn't matter
                  double distance = std::pow(node->x()-pos.x(),2) + std::pow(node->y()-pos.y(),2);
                  
                  if ( distance < shortestDistance )
                  {
                        shortestDistance = distance;
                        shortestNode = node;
                  }
            }
      }
      
      return shortestNode;
}


Generated by  Doxygen 1.6.0   Back to index