bezctx_x3.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. ppedit - A pattern plate editor for Spiro splines.
  3. Copyright (C) 2007 Raph Levien
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301, USA.
  16. */
  17. #include <x3.h>
  18. #include "zmisc.h"
  19. #include "bezctx.h"
  20. #include "bezctx_x3.h"
  21. typedef struct {
  22. bezctx base;
  23. x3dc *dc;
  24. int is_open;
  25. } bezctx_x3;
  26. static void
  27. bezctx_x3_moveto(bezctx *z, double x, double y, int is_open) {
  28. bezctx_x3 *bc = (bezctx_x3 *)z;
  29. if (!bc->is_open) x3closepath(bc->dc);
  30. x3moveto(bc->dc, x, y);
  31. bc->is_open = is_open;
  32. }
  33. void
  34. bezctx_x3_lineto(bezctx *z, double x, double y) {
  35. bezctx_x3 *bc = (bezctx_x3 *)z;
  36. x3lineto(bc->dc, x, y);
  37. }
  38. void
  39. bezctx_x3_quadto(bezctx *z, double x1, double y1, double x2, double y2)
  40. {
  41. bezctx_x3 *bc = (bezctx_x3 *)z;
  42. double x0, y0;
  43. x3getcurrentpoint(bc->dc, &x0, &y0);
  44. x3curveto(bc->dc,
  45. x1 + (1./3) * (x0 - x1),
  46. y1 + (1./3) * (y0 - y1),
  47. x1 + (1./3) * (x2 - x1),
  48. y1 + (1./3) * (y2 - y1),
  49. x2,
  50. y2);
  51. }
  52. void
  53. bezctx_x3_curveto(bezctx *z, double x1, double y1, double x2, double y2,
  54. double x3, double y3)
  55. {
  56. bezctx_x3 *bc = (bezctx_x3 *)z;
  57. x3curveto(bc->dc, x1, y1, x2, y2, x3, y3);
  58. }
  59. void
  60. bezctx_x3_finish(bezctx *z)
  61. {
  62. bezctx_x3 *bc = (bezctx_x3 *)z;
  63. if (!bc->is_open)
  64. x3closepath(bc->dc);
  65. zfree(bc);
  66. }
  67. bezctx *
  68. new_bezctx_x3(x3dc *dc) {
  69. bezctx_x3 *result = znew(bezctx_x3, 1);
  70. result->base.moveto = bezctx_x3_moveto;
  71. result->base.lineto = bezctx_x3_lineto;
  72. result->base.quadto = bezctx_x3_quadto;
  73. result->base.curveto = bezctx_x3_curveto;
  74. result->base.mark_knot = NULL;
  75. result->dc = dc;
  76. result->is_open = 1;
  77. return &result->base;
  78. }