home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / TimGA 1.2.1 / .h / CEdge.h < prev    next >
Encoding:
Text File  |  1997-07-16  |  6.0 KB  |  212 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CEdge.h            ©1995-97 Timo Eloranta            All rights reserved.
  3. // ===========================================================================
  4. //    A simple edge class. The edge is built of two pointers pointing to the
  5. //    nodes between which this edge exists...
  6.  
  7. #pragma once
  8.  
  9. #include "CNode.h"
  10.  
  11. class CEdge
  12. {
  13.     protected:
  14.         CNodePtr    mNode1;        // Pointer to the 1st node
  15.         CNodePtr    mNode2;        // Pointer to the 2nd node
  16.         short        mLength;    // Square of the length...
  17.  
  18.     public:
  19.         CEdge();
  20.     
  21.         void    Set(             const CNodePtr      inNode1,
  22.                                 const CNodePtr      inNode2);
  23.         
  24.         void     GetEdgeNodes(    CNodePtr        & outPtr1,
  25.                                 CNodePtr        & outPtr2 )    const;
  26.  
  27.         void    GetNodeNumbers(    short            & outNbr1,
  28.                                 short            & outNbr2)    const;
  29.  
  30.         Boolean    operator==(        const CEdge        & inEdge)    const;
  31.         Boolean Intersects(        const CEdge        & inEdge)    const;
  32.         Boolean HasMutualNode(    const CEdge        & inEdge)    const;
  33.         
  34.         Boolean OtherNode(        const CNodePtr    & inNode,
  35.                                 CNodePtr        & outNode)    const;
  36.  
  37.         Int16    SetLength( );
  38.         Int16    GetLength( ) const { return mLength; };
  39.  
  40.         void     Draw( Int16 inOneOneXY, Int16 inSquareSize)    const;
  41.  
  42.         Boolean    HasMoved() const;
  43. };
  44.  
  45. null_template
  46. struct iterator_trait <const CEdge*> {
  47.     typedef ptrdiff_t                    distance_type;
  48.     typedef const CEdge                  value_type;
  49.     typedef random_access_iterator_tag   iterator_category;
  50. };
  51.  
  52. // ===========================================================================
  53. // • Inline Functions
  54. // ===========================================================================
  55.  
  56. // ---------------------------------------------------------------------------
  57. //        • Set
  58. //
  59. //          Called by:    CEdge::CEdge
  60. //                        CGraphDrawing::ValidNewEdge
  61. //                        CGraphDrawing::SetNewEdge
  62. //                        CGraphDrawing::CopyEdges
  63. // ---------------------------------------------------------------------------
  64. //    Define this edge by setting the nodes (inNode1 & 2) 
  65. //    between which the edge exists.
  66.  
  67. inline void 
  68. CEdge::Set( const CNodePtr inNode1, const CNodePtr inNode2)
  69. {
  70.     mNode1 = inNode1;
  71.     mNode2 = inNode2;
  72. }
  73.  
  74. // ---------------------------------------------------------------------------
  75. //        • CEdge
  76. //
  77. //          Called by:    CGraphDrawing::Initialize
  78. //                        CGraphDrawing::ValidNewEdge
  79. //                        CGraphDrawing::CopyEdges
  80. // ---------------------------------------------------------------------------
  81. //    Constructor. Set nodes to nil and length to 0...
  82.  
  83. inline
  84. CEdge::CEdge()
  85. {    
  86.     Set( nil, nil);
  87.     mLength = 0;
  88. }
  89.  
  90. // ---------------------------------------------------------------------------
  91. //        • operator==
  92. //
  93. //          Called by:    CGraphDrawing::ValidNewEdge
  94. // ---------------------------------------------------------------------------
  95. //    Return true if inEdge is the same edge as this one. Otherwise return false.
  96.  
  97. inline Boolean    
  98. CEdge::operator==( const CEdge & inEdge ) const
  99. {
  100.     // Simply comparing node pointers is dirty but fast...
  101.  
  102.     return    ( ( (mNode1 == inEdge.mNode1) && (mNode2 == inEdge.mNode2) )    ||
  103.               ( (mNode1 == inEdge.mNode2) && (mNode2 == inEdge.mNode1) ) );
  104.  
  105. }
  106.  
  107. // ---------------------------------------------------------------------------
  108. //        • HasMutualNode
  109. //
  110. //          Called by:    CEdge::Intersects
  111. // ---------------------------------------------------------------------------
  112. //    Return true, if inEdge has at least one mutual node with this edge.
  113.  
  114. inline Boolean 
  115. CEdge::HasMutualNode( const CEdge & inEdge) const
  116. {
  117.     return    (    (mNode1 == inEdge.mNode1) || 
  118.                 (mNode2 == inEdge.mNode2) ||
  119.                 (mNode1 == inEdge.mNode2) ||
  120.                 (mNode2 == inEdge.mNode1)        );
  121. }
  122.  
  123. // ---------------------------------------------------------------------------
  124. //        • OtherNode
  125. //
  126. //          Called by:    CGraphDrawing::ThreeConnectedNodes
  127. // ---------------------------------------------------------------------------
  128. //    If inNode is one of this edge's two nodes, set outNode to point to this
  129. //    edge's other node and return true. Otherwise leave outNode unchanged and
  130. //    return false.
  131.  
  132. inline Boolean
  133. CEdge::OtherNode( const CNodePtr & inNode, CNodePtr & outNode) const
  134. {
  135.     Boolean theResult = false;
  136.  
  137.     if ( inNode == mNode1 ) {
  138.         outNode = mNode2;
  139.         theResult = true;
  140.     }
  141.     else
  142.         if ( inNode == mNode2 ) {
  143.             outNode = mNode1;
  144.             theResult = true;
  145.         }
  146.  
  147.     return theResult;
  148. }
  149.  
  150. // ---------------------------------------------------------------------------
  151. //        • GetEdgeNodes
  152. //
  153. //          Called by:    CGraphDrawing::EdgeMutation
  154. //                        CGraphDrawing::ThreeConnectedNodes
  155. // ---------------------------------------------------------------------------
  156. //    Fetch this edge's nodepointers to the parameters outPtr1 & 2.
  157.  
  158. inline void 
  159. CEdge::GetEdgeNodes( CNodePtr &outPtr1, CNodePtr &outPtr2 ) const
  160. {
  161.     outPtr1 = mNode1;
  162.     outPtr2 = mNode2;
  163. }
  164.  
  165. // ---------------------------------------------------------------------------
  166. //        • GetNodeNumbers
  167. //
  168. //          Called by:    CGraphDrawing::CopyEdges
  169. // ---------------------------------------------------------------------------
  170. //    Fetch the numbers of this edge's nodes to the parameters outNbr1 & 2.
  171.  
  172. inline void    
  173. CEdge::GetNodeNumbers( short & outNbr1, short & outNbr2) const
  174. {
  175.     mNode1 -> GetNodeNbr( outNbr1 );
  176.     mNode2 -> GetNodeNbr( outNbr2 );
  177. }
  178.  
  179. // ---------------------------------------------------------------------------
  180. //        • SetLength
  181. //
  182. //          Called by:    CGraphDrawing::CountEdgeLengths
  183. // ---------------------------------------------------------------------------
  184. //  Update and return the _square_ of the length of the edge.
  185. //  We don't want to use square roots as that would be TOO SLOW...
  186.  
  187. inline Int16
  188. CEdge::SetLength( )
  189. {
  190.     Int16    p1x, p2x, p1y, p2y;
  191.  
  192.     mNode1 -> GetNodePos( p1x, p1y );
  193.     mNode2 -> GetNodePos( p2x, p2y );
  194.     
  195.     return ( mLength =    ((p1x - p2x) * (p1x - p2x)) + 
  196.                         ((p1y - p2y) * (p1y - p2y))   );
  197. }
  198.  
  199. // ---------------------------------------------------------------------------
  200. //        • HasMoved
  201. //
  202. //          Called by:    CGraphDrawing::SmarterCountSect
  203. // ---------------------------------------------------------------------------
  204. //    Return whether the edge has moved or not
  205. //    (it has, if either of the nodes has moved).
  206.  
  207. inline Boolean
  208. CEdge::HasMoved() const
  209. {
  210.     return ( mNode1 -> GetHasMoved() || mNode2 -> GetHasMoved() );
  211. }
  212.