You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7KB

  1. /* Copyleft (C) 2002 David Griffiths <dave@pawfal.org>
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  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. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #include "Bezier.h"
  18. void Bezier(Vec2 &Out, Vec2 a, Vec2 b, Vec2 c, Vec2 d, float t)
  19. {
  20. float tc=t*t*t;
  21. float ts=t*t;
  22. Out.x=(-a.x+3*b.x-3*c.x+d.x)*tc + (3*a.x-6*b.x+3*c.x)*ts + (-3*a.x+3*b.x)*t + a.x;
  23. Out.y=(-a.y+3*b.y-3*c.y+d.y)*tc + (3*a.y-6*b.y+3*c.y)*ts + (-3*a.y+3*b.y)*t + a.y;
  24. }
  25. void CalculateBezierCurve(vector<Vec2> *List, float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4,int Sections)
  26. {
  27. Vec2 g1(x1,y1); Vec2 g2(x2,y2); Vec2 g3(x3,y3); Vec2 g4(x4,y4);
  28. float t=0;
  29. Vec2 p(0,0),lp(0,0);
  30. for (int n=0; n<=Sections; n++)
  31. {
  32. Bezier(p,g1,g2,g3,g4,t);
  33. List->push_back(Vec2(p.x,p.y));
  34. lp.x=p.x; lp.y=p.y;
  35. t+=0.1;
  36. }
  37. }
  38. void AddToSpline(const Vec2 &CV)
  39. {
  40. Spline.push_back(CV);
  41. }
  42. void CalculateBezierSpline(vector<Vec2> *List,int Sections)
  43. {
  44. if (Spline.size()==0) return;
  45. Vec2 SplineSection[4];
  46. SplineSection[0].x=Spline[0].x;
  47. SplineSection[0].y=Spline[0].y;
  48. SplineSection[1].x=Spline[0].x;
  49. SplineSection[1].y=Spline[0].y;
  50. SplineSection[2].x=Spline[0].x;
  51. SplineSection[2].y=Spline[0].y;
  52. SplineSection[3].x=Spline[0].x;
  53. SplineSection[3].y=Spline[0].y;
  54. int SCount=0;
  55. for(vector<Vec2>::iterator i=Spline.begin();
  56. i!=Spline.end(); ++i)
  57. {
  58. SplineSection[3].x=i->x;
  59. SplineSection[3].y=i->y;
  60. if (SCount>3)
  61. {
  62. vector<Vec2> BezierLineList;
  63. CalculateBezierCurve(&BezierLineList,
  64. SplineSection[0].x,SplineSection[0].y,
  65. SplineSection[1].x,SplineSection[1].y,
  66. SplineSection[2].x,SplineSection[2].y,
  67. SplineSection[3].x,SplineSection[3].y,
  68. Sections);
  69. for (vector<Vec2>::iterator bi=BezierLineList.begin();
  70. bi!=BezierLineList.end(); bi++)
  71. {
  72. List->push_back(*bi);
  73. }
  74. SCount=1;
  75. }
  76. for (int m=0; m<3; m++)
  77. {
  78. SplineSection[m].x=SplineSection[m+1].x;
  79. SplineSection[m].y=SplineSection[m+1].y;
  80. }
  81. SCount++;
  82. }
  83. Spline.clear();
  84. }