fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4.  
  5. struct Point
  6. {
  7. float x;
  8. float y;
  9.  
  10. float distance( Point & p )
  11. {
  12. return sqrt( ( ( p.x - x ) * ( p.x - x ) ) + ( ( p.y - y ) * ( p.y - y ) ) );
  13. }
  14. };
  15.  
  16.  
  17. struct Line
  18. {
  19. Point start;
  20. Point end;
  21.  
  22. float length()
  23. {
  24. return start.distance( end );
  25. }
  26.  
  27. Point midpoint()
  28. {
  29. return { ( start.x + end.x ) / 2, ( start.y + end.y ) / 2 };
  30. }
  31. };
  32.  
  33.  
  34.  
  35. Line lines[2]
  36. {
  37. { { 0, 0 }, { 30, 40 } },
  38. { { 10, 10 }, { 20, 20 } },
  39. };
  40.  
  41.  
  42. int main()
  43. {
  44. for ( int i = 0; i < 2; i++ )
  45. {
  46. Line & line = lines[i];
  47.  
  48. Point start = line.start;
  49. Point end = line.end;
  50. float length = line.length();
  51. Point midpoint = line.midpoint();
  52.  
  53. printf(" Length of line [ (%5.2f, %5.2f), (%5.2f, %5.2f) ] is %.2f, midpoint is (%5.2f, %5.2f)\n",
  54. start.x,
  55. start.y,
  56. end.x,
  57. end.y,
  58. length,
  59. midpoint.x,
  60. midpoint.y
  61. );
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
 Length of line [ ( 0.00,  0.00), (30.00, 40.00) ] is 50.00, midpoint is (15.00, 20.00)
 Length of line [ (10.00, 10.00), (20.00, 20.00) ] is 14.14, midpoint is (15.00, 15.00)