Matrix_and_Polygon.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Matrix_and_Polygon.cpp
  2. *
  3. * Copyright (C) 1992-2005,2011,2012,2015-2018 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Matrix_and_Polygon.h"
  19. autoPolygon Matrix_to_Polygon (Matrix me) {
  20. try {
  21. Melder_require (my nx == 2 || my ny == 2,
  22. U"The Matrix should have exactly 2 rows or columns.");
  23. autoPolygon thee;
  24. if (my ny == 2) {
  25. /*
  26. The matrix has two rows.
  27. The first row will be interpreted as x values, the second as y values.
  28. */
  29. thee = Polygon_create (my nx);
  30. VECcopy_preallocated (thy x.get(), my z.row (1));
  31. VECcopy_preallocated (thy y.get(), my z.row (2));
  32. } else {
  33. /*
  34. The matrix has two columns.
  35. The first column will be interpreted as x values, the second as y values.
  36. */
  37. thee = Polygon_create (my ny);
  38. for (integer i = 1; i <= my ny; i ++) {
  39. thy x [i] = my z [i] [1];
  40. thy y [i] = my z [i] [2];
  41. }
  42. }
  43. return thee;
  44. } catch (MelderError) {
  45. Melder_throw (me, U": not converted to Polygon.");
  46. }
  47. }
  48. autoMatrix Polygon_to_Matrix (Polygon me) {
  49. try {
  50. /*
  51. The matrix will have two rows:
  52. the first column will represent x, the second will represent y.
  53. */
  54. autoMatrix thee = Matrix_create (1.0, my numberOfPoints, my numberOfPoints, 1.0, 1.0, 1.0, 2.0, 2, 1.0, 1.0);
  55. VECcopy_preallocated (thy z.row (1), my x.get());
  56. VECcopy_preallocated (thy z.row (2), my y.get());
  57. return thee;
  58. } catch (MelderError) {
  59. Melder_throw (me, U": not converted to Matrix.");
  60. }
  61. }
  62. /* End of file Matrix_and_Polygon.cpp */