# include # include # include # include # include # include using namespace std; # include "ellipse.hpp" int main ( ); void ellipse_area1_test ( ); void ellipse_area2_test ( ); void ellipse_area3_test ( ); void ellipse_aspect_ratio_test ( ); void ellipse_eccentricity_test ( ); void ellipse_flattening_test ( ); void ellipse_point_dist_2d_test ( ); void ellipse_point_near_2d_test ( ); void ellipse_points_2d_test ( ); void ellipse_points_arc_2d_test ( ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // ellipse_test() tests ellipse(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 07 April 2022 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "ellipse_test:\n"; cout << " C++ version\n"; cout << " Test ellipse()\n"; ellipse_area1_test ( ); ellipse_area2_test ( ); ellipse_area3_test ( ); ellipse_aspect_ratio_test ( ); ellipse_eccentricity_test ( ); ellipse_flattening_test ( ); ellipse_point_dist_2d_test ( ); ellipse_point_near_2d_test ( ); ellipse_points_2d_test ( ); ellipse_points_arc_2d_test ( ); // // Terminate. // cout << "\n"; cout << "ellipse_test():\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //*****************************************************************************/ void ellipse_area1_test ( ) //*****************************************************************************/ // // Purpose: // // ellipse_area1_test() tests ellipse_area1(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 08 November 2016 // // Author: // // John Burkardt // { double A[2*2] = { 5.0, 1.0, 1.0, 2.0 }; double area; double r; cout << "\n"; cout << "ellipse_area1_test():\n"; cout << " ellipse_area1() computes the area of an ellipse.\n"; r = 10.0; area = ellipse_area1 ( A, r ); cout << "\n"; cout << " R = " << r << "\n"; cout << " Matrix A = \n"; cout << " [" << A[0+0*2] << "," << A[0+1*2] << "]\n"; cout << " [" << A[1+0*2] << "," << A[1+1*2] << "]\n"; cout << " Area = " << area << "\n"; return; } //*****************************************************************************/ void ellipse_area2_test ( ) //*****************************************************************************/ // // Purpose: // // ellipse_area2_test() tests ellipse_area2(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 08 November 2016 // // Author: // // John Burkardt // { double a; double area; double b; double c; double d; cout << "\n"; cout << "ellipse_area2_test():\n"; cout << " ellipse_area2() computes the area of an ellipse.\n"; a = 5.0; b = 2.0; c = 2.0; d = 10.0; area = ellipse_area2 ( a, b, c, d ); cout << "\n"; cout << " Ellipse: " << a << " * x^2 + " << b << " * xy + " << c << " * y^2 = " << d << "\n"; cout << " Area = " << area << "\n"; return; } //*****************************************************************************/ void ellipse_area3_test ( ) //*****************************************************************************/ // // Purpose: // // ellipse_area3_test() tests ellipse_area3(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 09 November 2016 // // Author: // // John Burkardt // { double area; double r1; double r2; cout << "\n"; cout << "ellipse_area3_test():\n"; cout << " ellipse_area3() computes the area of an ellipse.\n"; r1 = 10.0; r2 = 10.0 / 3.0; area = ellipse_area3 ( r1, r2 ); cout << "\n"; cout << " Ellipse: (x/" << r1 << ")^2 + (y/" << r2 << ")^2 = 1\n"; cout << " Area = " << area << "\n"; return; } //****************************************************************************80 void ellipse_aspect_ratio_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_aspect_ratio_test() tests ellipse_aspect_ratio(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 October 2022 // // Author: // // John Burkardt // { double a; double b; double e; int i; int n; cout << "\n"; cout << "ellipse_aspect_ratio_test():\n"; cout << " ellipse_aspect_ratio() computes the aspect ratio of an ellipse.\n"; cout << "\n"; cout << " A B Ratio\n"; cout << "\n"; a = 1.0; n = 10; for ( i = 0; i <= n; i++ ) { b = ( double ) ( i ) / ( double ) ( n ); e = ellipse_aspect_ratio ( a, b ); cout << " " << setw(5) << a << " " << setw(5) << b << " " << setw(5) << e << "\n"; } return; } //****************************************************************************80 void ellipse_eccentricity_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_eccentricity_test() tests ellipse_eccentricity(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 March 2021 // // Author: // // John Burkardt // { double a; double b; double e; int i; int n; cout << "\n"; cout << "ellipse_eccentricity_test():\n"; cout << " ellipse_eccentricity() computes the eccentricity of an ellipse.\n"; cout << "\n"; cout << " A B Ecc\n"; cout << "\n"; a = 1.0; n = 10; for ( i = 0; i <= n; i++ ) { b = ( double ) ( i ) / ( double ) ( n ); e = ellipse_eccentricity ( a, b ); cout << " " << setw(5) << a << " " << setw(5) << b << " " << setw(5) << e << "\n"; } return; } //****************************************************************************80 void ellipse_flattening_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_flattening_test() tests ellipse_flattening(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 October 2022 // // Author: // // John Burkardt // { double a; double b; double e; int i; int n; cout << "\n"; cout << "ellipse_flattening_test():\n"; cout << " ellipse_flattening() computes the flattening of an ellipse.\n"; cout << "\n"; cout << " A B Flat\n"; cout << "\n"; a = 1.0; n = 10; for ( i = 0; i <= n; i++ ) { b = ( double ) ( i ) / ( double ) ( n ); e = ellipse_flattening ( a, b ); cout << " " << setw(5) << a << " " << setw(5) << b << " " << setw(5) << e << "\n"; } return; } //****************************************************************************80 void ellipse_point_dist_2d_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_point_dist_2d_test() tests ellipse_point_dist_2d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 February 2007 // // Author: // // John Burkardt // { # define DIM_NUM 2 double dist; int i; int n = 10; double p[DIM_NUM]; double r1 = 3.0; double r2 = 2.0; cout << "\n"; cout << "ellipse_point_dist_2d_test():\n"; cout << " ellipse_point_dist_2d() is given a point P, and\n"; cout << " finds the distance to an ellipse in 2D.\n"; cout << "\n"; cout << " The ellipse is (X/R1)^2 + (Y/R2)^2 = 1\n"; cout << "\n"; cout << " R1 = " << r1 << "\n"; cout << " R2 = " << r2 << "\n"; cout << "\n"; cout << " P DIST\n"; cout << "\n"; for ( i = -3; i <= n + 3; i++ ) { p[0] = ( double ( n - i ) * 0.0 + double ( i ) * 4.0 ) / double ( n ); p[1] = ( double ( n - i ) * 3.0 + double ( i ) * 0.0 ) / double ( n ); dist = ellipse_point_dist_2d ( r1, r2, p ); cout << " " << setw(8) << p[0] << " " << setw(8) << p[1] << " " << setw(8) << dist << "\n"; } return; # undef DIM_NUM } //****************************************************************************80 void ellipse_point_near_2d_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_point_near_2d_test() tests ellipse_point_near_2d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 February 2007 // // Author: // // John Burkardt // { # define DIM_NUM 2 int i; int n = 10; double p[DIM_NUM]; double *pn; double r1 = 3.0; double r2 = 2.0; cout << "\n"; cout << "ellipse_point_near_2d_test():\n"; cout << " ellipse_point_near_2d() is given a point P, and\n"; cout << " finds the nearest point on an ellipse in 2D.\n"; cout << "\n"; cout << " The ellipse is (X/R1)^2 + (Y/R2)^2 = 1\n"; cout << "\n"; cout << " R1 = " << r1 << "\n"; cout << " R2 = " << r2 << "\n"; cout << "\n"; cout << " P PN\n"; cout << "\n"; for ( i = -3; i <= n + 3; i++ ) { p[0] = ( double ( n - i ) * 0.0 + double ( i ) * 4.0 ) / double ( n ); p[1] = ( double ( n - i ) * 3.0 + double ( i ) * 0.0 ) / double ( n ); pn = ellipse_point_near_2d ( r1, r2, p ); cout << " " << setw(8) << p[0] << " " << setw(8) << p[1] << " " << setw(8) << pn[0] << " " << setw(8) << pn[1] << "\n"; delete [] pn; } return; # undef DIM_NUM } //****************************************************************************80 void ellipse_points_2d_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_points_2d_test() tests ellipse_points_2d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 February 2007 // // Author: // // John Burkardt // { double area; int j; int n; double pc[2] = { 5.0, -2.0 }; double psi = 3.141592653589793 / 6.0; double r1 = 3.0; double r2 = 1.0; double *v; cout << "\n"; cout << "ellipse_points_2d_test():\n"; cout << " ellipse_points_2d() returns points on an ellipse;\n"; cout << " Ellipse center is [" << pc[0] << "," << pc[1] << "]\n"; cout << "\n"; cout << " radii R1 = " << r1 << " R2 = " << r2 << "\n"; cout << " and angle PSI = " << psi << "\n"; area = ellipse_area3 ( r1, r2 ); cout << " and area = " << area << "\n"; n = 16; v = new double[2*n]; ellipse_points_2d ( pc, r1, r2, psi, n, v ); cout << "\n"; cout << " Sample points:\n"; cout << "\n"; for ( j = 0; j < n; j++ ) { cout << "[" << v[0+j*2] << "," << v[1+j*2] << "]\n"; } delete [] v; return; } //****************************************************************************80 void ellipse_points_arc_2d_test ( ) //****************************************************************************80 // // Purpose: // // ellipse_points_arc_2d_test() tests ellipse_points_arc_2d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 February 2007 // // Author: // // John Burkardt // { # define N 13 int j; double p[2*N]; double pc[2] = { 5.0, -2.0 }; double psi = 3.141592653589793 / 6.0; double r1 = 3.0; double r2 = 1.0; double theta1 = 3.141592653589793 / 2.0; double theta2 = 2.0 * 3.141592653589793; cout << "\n"; cout << "ellipse_points_arc_2d_test():\n"; cout << " ellipse_points_arc_2d() returns points on an\n"; cout << " elliptical arc.\n"; cout << "\n"; cout << " The ellipse has center " << pc[0] << " " << pc[1] << "\n"; cout << " radii R1 = " << r1 << " R2 = " << r2 << "\n"; cout << " and angle PSI = " << psi << "\n"; cout << "\n"; cout << " The arc extends from THETA1 = " << theta1 << "\n"; cout << " to THETA2 = " << theta2 << "\n"; ellipse_points_arc_2d ( pc, r1, r2, psi, theta1, theta2, N, p ); cout << "\n"; cout << " Sample points:\n"; cout << "\n"; for ( j = 0; j < N; j++ ) { cout << "[" << p[0+j*2] << "," << p[1+j*2] << "]\n"; } return; # undef N } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }