Zoek de coördinaten van de driehoek, gegeven het middelpunt van elke zijde

Zoek de coördinaten van de driehoek, gegeven het middelpunt van elke zijde

Gegeven drie coördinaten (xy) die het middelpunt zijn van de zijden van de driehoek. De taak is om de coördinaten van de driehoek te vinden. 

Voorbeelden:   

Input : midx 1  = 5 midy 1  = 3 midx 2  = 4 midy 2  = 4 midx 3  = 5 midy 3  = 5 Output : x 1  = 4 y 1  = 2 x 2  = 4 y 2  = 6 x 3  = 6 y 3  = 4 

coördinaten van de driehoek gegeven middelpunt van elke zijde



Solution can be verified by the figure. 

Laten we de X-coördinaten en Y-coördinaten afzonderlijk oplossen. Voor de X-coördinaat van hoekpunten geldt dat ze x zijn 1 X 2 X 3 . Dan is de X-coördinaat van de middelpunten (x 1 +x 2 )/2 (x 2 +x 3 )/2 (x 3 +x 1 )/2. Merk op dat de som van deze 3 uitdrukkingen gelijk is aan de som van de X-coördinaten. Nu hebben we de som van 3 variabelen en 3 uitdrukkingen voor de som van elk paar. Ontdek de waarden van coördinaten door vergelijkingen op te lossen. 
Op dezelfde manier lossen we Y-coördinaten op.

Hieronder vindt u de implementatie van deze aanpak:  

C++
   // C++ program to find coordinate of the   // triangle given midpoint of each side   #include       #define N 3   using     namespace     std  ;   // Return after solving the equations and   // finding the vertices coordinate.   vector   <  int  >     solve  (  int     v  [])   {      vector   <  int  >     res  ;      // Finding sum of all three coordinate.      int     all3     =     v  [  0  ]     +     v  [  1  ]     +     v  [  2  ];      // Solving the equation.      res  .  push_back  (  all3     -     v  [  1  ]  *  2  );      res  .  push_back  (  all3     -     v  [  2  ]  *  2  );      res  .  push_back  (  all3     -     v  [  0  ]  *  2  );      return     res  ;   }   // Finds vertices of a triangles from given   // middle vertices.   void     findVertex  (  int     xmid  []     int     ymid  [])   {      // Find X coordinates of vertices.      vector   <  int  >     V1     =     solve  (  xmid  );      // Find Y coordinates of vertices.      vector   <  int  >     V2     =     solve  (  ymid  );      // Output the solution.      for     (  int     i     =     0  ;     i      <     3  ;     i  ++  )      cout      < <     V1  [  i  ]      < <     ' '       < <     V2  [  i  ]      < <  endl  ;   }   // Driver code   int     main  ()   {      int     xmid  [  N  ]     =     {     5       4       5     };      int     ymid  [  N  ]     =     {     3       4       5     };      findVertex  (  xmid       ymid  );      return     0  ;   }   
Java
   import     java.util.Vector  ;   // Java program to find coordinate of the    // triangle given midpoint of each side    class   GFG     {   //static final int N = 3;    // Return after solving the equations and    // finding the vertices coordinate.       static     Vector   <  Integer  >     solve  (  int     v  []  )     {      Vector   <  Integer  >     res     =     new     Vector   <  Integer  >  ();      // Finding sum of all three coordinate.       int     all3     =     v  [  0  ]     +     v  [  1  ]     +     v  [  2  ]  ;      // Solving the equation.       res  .  add  (  all3     -     v  [  1  ]     *     2  );      res  .  add  (  all3     -     v  [  2  ]     *     2  );      res  .  add  (  all3     -     v  [  0  ]     *     2  );      return     res  ;      }   // Finds vertices of a triangles from given    // middle vertices.       static     void     findVertex  (  int     xmid  []       int     ymid  []  )     {      // Find X coordinates of vertices.       Vector   <  Integer  >     V1     =     solve  (  xmid  );      // Find Y coordinates of vertices.       Vector   <  Integer  >     V2     =     solve  (  ymid  );      // Output the solution.       for     (  int     i     =     0  ;     i      <     3  ;     i  ++  )     {      System  .  out  .  println  (  V1  .  get  (  i  )     +     ' '     +     V2  .  get  (  i  ));      }      }   // Driver code       public     static     void     main  (  String  []     args  )     {      int     xmid  []     =     {  5       4       5  };      int     ymid  []     =     {  3       4       5  };      findVertex  (  xmid       ymid  );      }   }   // This code is contributed by   // PrinciRaj1992   
Python3
   # Python3 program to find coordinate of the   # triangle given midpoint of each side   N   =   3   # Return after solving the equations and   # finding the vertices coordinate.   def   solve  (  v  ):   res   =   []   # Finding sum of all three coordinate.   all3   =   v  [  0  ]   +   v  [  1  ]   +   v  [  2  ]   # Solving the equation.   res  .  append  (  all3   -   v  [  1  ]   *   2  )   res  .  append  (  all3   -   v  [  2  ]   *   2  )   res  .  append  (  all3   -   v  [  0  ]   *   2  )   return   res   # Finds vertices of a triangles from given   # middle vertices.   def   findVertex  (  xmid     ymid  ):   # Find X coordinates of vertices.   V1   =   solve  (  xmid  )   # Find Y coordinates of vertices.   V2   =   solve  (  ymid  )   # Output the solution.   for   i   in   range  (  0     3  ):   print  (  V1  [  i  ]   end  =  ' '  )   print  (  V2  [  i  ])   # Driver code   if   __name__  ==  '__main__'  :   xmid   =   [  5     4     5  ]   ymid   =   [  3     4     5  ]   findVertex  (  xmid     ymid  )   # This code is contributed by   # Sanjit_Prasad   
C#
   // C# program to find coordinate of the    // triangle given midpoint of each side    using     System  ;   using     System.Collections  ;   class     GFG      {         //static final int N = 3;       // Return after solving the equations and       // finding the vertices coordinate.       static     ArrayList     solve  (  int     []  v  )      {         ArrayList     res     =     new     ArrayList  ();         // Finding sum of all three coordinate.       int     all3     =     v  [  0  ]     +     v  [  1  ]     +     v  [  2  ];         // Solving the equation.       res  .  Add  (  all3     -     v  [  1  ]     *     2  );         res  .  Add  (  all3     -     v  [  2  ]     *     2  );         res  .  Add  (  all3     -     v  [  0  ]     *     2  );         return     res  ;         }         // Finds vertices of a triangles from given       // middle vertices.       static     void     findVertex  (  int     []  xmid       int     []  ymid  )      {         // Find X coordinates of vertices.       ArrayList     V1     =     solve  (  xmid  );         // Find Y coordinates of vertices.       ArrayList     V2     =     solve  (  ymid  );         // Output the solution.       for     (  int     i     =     0  ;     i      <     3  ;     i  ++  )      {         Console  .  WriteLine  (  V1  [  i  ]     +     ' '     +     V2  [  i  ]);         }         }         // Driver code       public     static     void     Main  ()         {         int     []  xmid     =     {  5       4       5  };         int     []  ymid     =     {  3       4       5  };         findVertex  (  xmid       ymid  );         }      }      // This code is contributed by mits   
PHP
      // PHP program to find coordinate of the    // triangle given midpoint of each side    $N   =   3  ;   // Return after solving the equations and    // finding the vertices coordinate.    function   solve  (  $v  )   {   $res   =   array  ();   // Finding sum of all three coordinate.    $all3   =   $v  [  0  ]   +   $v  [  1  ]   +   $v  [  2  ];   // Solving the equation.    array_push  (  $res     $all3   -   $v  [  1  ]   *   2  );   array_push  (  $res     $all3   -   $v  [  2  ]   *   2  );   array_push  (  $res     $all3   -   $v  [  0  ]   *   2  );   return   $res  ;   }   // Finds vertices of a triangles from    // given middle vertices.    function   findVertex  (  $xmid     $ymid  )   {   // Find X coordinates of vertices.    $V1   =   solve  (  $xmid  );   // Find Y coordinates of vertices.    $V2   =   solve  (  $ymid  );   // Output the solution.    for   (  $i   =   0  ;   $i    <   3  ;   $i  ++  )   print  (  $V1  [  $i  ]   .   ' '   .   $V2  [  $i  ]   .   '  n  '  );   }   // Driver code    $xmid   =   array  (  5     4     5  );   $ymid   =   array  (  3     4     5  );   findVertex  (  $xmid     $ymid  )   // This code is contributed by mits   ?>   
JavaScript
    <  script  >      // JavaScript program to find coordinate of the      // triangle given midpoint of each side      // Return after solving the equations and      // finding the vertices coordinate.      function     solve  (  v  )     {      var     res     =     [];      // Finding sum of all three coordinate.      var     all3     =     v  [  0  ]     +     v  [  1  ]     +     v  [  2  ];      // Solving the equation.      res  .  push  (  all3     -     v  [  1  ]     *     2  );      res  .  push  (  all3     -     v  [  2  ]     *     2  );      res  .  push  (  all3     -     v  [  0  ]     *     2  );      return     res  ;      }      // Finds vertices of a triangles from given      // middle vertices.      function     findVertex  (  xmid       ymid  )     {      // Find X coordinates of vertices.      var     V1     =     solve  (  xmid  );      // Find Y coordinates of vertices.      var     V2     =     solve  (  ymid  );      // Output the solution.      for     (  var     i     =     0  ;     i      <     3  ;     i  ++  )     {      document  .  write  (  V1  [  i  ]     +     ' '     +     V2  [  i  ]     +     '  
'
); } } // Driver code var xmid = [ 5 4 5 ]; var ymid = [ 3 4 5 ]; findVertex ( xmid ymid ); < /script>

Uitgang:  

6 4 4 2 4 6 

Tijdcomplexiteit: O(1)

Hulpruimte : O(1) omdat het een constante ruimte gebruikt


 

Quiz maken

Top Artikelen

Categorie

Interessante Artikelen