g_ellipse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file also contains the ellipse method, which is a GNU extension to
  16. libplot. It draws an object: an ellipse with center xc,yc and semi-axes
  17. of length rx and ry (the former at a specified angle with the x-axis).
  18. Ellipses are one of the three types of primitive closed path that
  19. libplot supports, along with boxes and circles.
  20. In this generic version, we usually draw the ellipse by drawing four
  21. elliptic arcs (quarter-ellipses) into the path buffer's segment list.
  22. Plotters that don't support elliptic arcs draw polygonal approximations
  23. instead. Plotters that are sufficiently powerful that they support
  24. ellipses as primitive drawing elements place an ellipse primitive in the
  25. path buffer, instead.
  26. Note that some Plotters support the drawing of any ellipse; some require
  27. that they be aligned with the coordinate axes. Some don't support the
  28. drawing of ellipses at all. The constraints on the user frame -> device
  29. frame map (e.g., it must preserve coordinate axes) are specified by the
  30. internal `allowed_ellipse_scaling' parameter, which this code checks. */
  31. #include "sys-defines.h"
  32. #include "extern.h"
  33. int
  34. _API_fellipse (R___(Plotter *_plotter) double xc, double yc, double rx, double ry, double angle)
  35. {
  36. if (!_plotter->data->open)
  37. {
  38. _plotter->error (R___(_plotter)
  39. "fellipse: invalid operation");
  40. return -1;
  41. }
  42. /* If a simple path is under construction (so that endsubpath() must not
  43. have been invoked), flush out the whole compound path. (It may
  44. include other, previously drawn simple paths.) */
  45. if (_plotter->drawstate->path)
  46. _API_endpath (S___(_plotter));
  47. if (!_plotter->drawstate->points_are_connected)
  48. /* line type is `disconnected', so do nothing (libplot convention) */
  49. {
  50. }
  51. else
  52. /* general case */
  53. {
  54. plPoint pc;
  55. bool clockwise;
  56. /* determine whether ellipse's axes are aligned with the coordinate
  57. axes in the user frame, so that (if the device->user frame map
  58. preserves axes) the same will be true in the device frame */
  59. bool aligned_ellipse = false;
  60. int iangle = IROUND(angle);
  61. if (iangle < 0)
  62. iangle += (1 + (-iangle / 90)) * 90;
  63. if (iangle % 90 == 0 && angle == (double)iangle)
  64. aligned_ellipse = true;
  65. /* begin a new path */
  66. _plotter->drawstate->path = _new_plPath ();
  67. /* place ellipse in path buffer */
  68. pc.x = xc;
  69. pc.y = yc;
  70. clockwise = _plotter->drawstate->orientation < 0 ? true : false;
  71. if ((_plotter->data->allowed_ellipse_scaling == AS_ANY)
  72. ||
  73. (_plotter->data->allowed_ellipse_scaling == AS_AXES_PRESERVED
  74. && _plotter->drawstate->transform.axes_preserved
  75. && aligned_ellipse))
  76. /* place ellipse as a primitive, since this Plotter supports
  77. drawing ellipses as primitives */
  78. _add_ellipse (_plotter->drawstate->path,
  79. pc, rx, ry, angle, clockwise);
  80. else if (_plotter->data->allowed_ellarc_scaling == AS_ANY
  81. || (_plotter->data->allowed_ellarc_scaling == AS_AXES_PRESERVED
  82. && _plotter->drawstate->transform.axes_preserved
  83. && aligned_ellipse))
  84. /* draw ellipse by placing four elliptic arcs into path buffer
  85. (allowed since this Plotter supports elliptic arcs) */
  86. _add_ellipse_as_ellarcs (_plotter->drawstate->path,
  87. pc, rx, ry, angle, clockwise);
  88. else if (_plotter->data->allowed_cubic_scaling == AS_ANY)
  89. /* draw ellipse by placing four cubic Beziers into path buffer
  90. (allowed since this Plotter supports cubic Beziers) */
  91. _add_ellipse_as_bezier3s (_plotter->drawstate->path,
  92. pc, rx, ry, angle, clockwise);
  93. else
  94. /* draw a polygonal approximation to the ellipse */
  95. _add_ellipse_as_lines (_plotter->drawstate->path,
  96. pc, rx, ry, angle, clockwise);
  97. if (_plotter->drawstate->path->type == PATH_SEGMENT_LIST)
  98. /* pass all the newly added segments to the Plotter-specific
  99. function maybe_paint_segments(), since some Plotters plot paths
  100. in real time, i.e., prepaint them, rather than waiting until
  101. endpath() is called */
  102. _plotter->maybe_prepaint_segments (R___(_plotter) 0);
  103. }
  104. /* move to center (libplot convention) */
  105. _plotter->drawstate->pos.x = xc;
  106. _plotter->drawstate->pos.y = yc;
  107. return 0;
  108. }