bezier.h
上传用户:whjcdz88
上传日期:2011-09-07
资源大小:121k
文件大小:4k
源码类别:

图形图象

开发平台:

WINDOWS

  1. #if _MSC_VER > 1000
  2. #pragma once
  3. #endif // _MSC_VER > 1000
  4. // Copyright Llew S. Goodstadt 1998
  5. // http://www.lg.ndirect.co.uk    mailto:lg@ndirect.co.uk
  6. // you are hereby granted the right tofair use and distribution
  7. // including in both commercial and non-commercial applications.
  8. #include <afxwin.h>
  9. #include <math.h>
  10. void EllipseToBezier(CRect& r, CPoint* cCtlPt);
  11. struct LDPoint
  12. {
  13. double x, y;
  14. // Constructors
  15. LDPoint(){}
  16. LDPoint(double _x, double _y): x(_x), y(_y) {}
  17. LDPoint(const CPoint& p): x(p.x), y(p.y) {}
  18. LDPoint(const LDPoint& p): x(p.x), y(p.y) {}
  19. LDPoint(const POINT& p): x(p.x), y(p.y) {}
  20. // equality operators
  21. bool operator ==(const LDPoint& other) const;
  22. bool operator !=(const LDPoint& other) const;
  23. // Functions/binary-operators that return points or sizes
  24. LDPoint OffsetBy(double dx, double dy) const;
  25. LDPoint OffsetBy(const LDPoint sz) const; // should be LDSize
  26. LDPoint operator +(const LDPoint& other) const; // should be LDSize
  27. LDPoint operator -(const LDPoint& other) const; // should be LDSize * 2
  28. LDPoint operator -() const;
  29. double DistFrom2(const LDPoint&) const;
  30. double DistFrom(const LDPoint&) const;
  31. LDPoint ScaledBy(double f) const;
  32. LDPoint operator*(double f) const;
  33. CPoint GetCPoint() const;
  34. // Functions/assignement-operators that modify this point
  35. LDPoint& Offset(double dx, double dy);
  36. LDPoint& Offset(const LDPoint& sz); // should be LDSize
  37. LDPoint& operator +=(const LDPoint& other); // should be LDSize
  38. LDPoint& operator -=(const LDPoint& other); // should be LDSize
  39. LDPoint& Scale(double f);
  40. LDPoint& operator*=(double f);
  41. };
  42. struct LBezier
  43. {
  44. LDPoint p[4];
  45. void GetCPoint(CPoint* out);
  46. void TSplit(LBezier& Output, double t);
  47. void Split(LBezier& Left, LBezier& Right) const;
  48. double TAtLength(unsigned int& len) const;
  49. double TAtLength(double& len, double error = 0.5) const;
  50. double Length(double error = 0.5) const;
  51. };
  52. // equality operators
  53. inline bool LDPoint::operator ==(const LDPoint& other) const
  54. { return other.x == x && other.y == y;}
  55. inline bool LDPoint::operator !=(const LDPoint& other) const
  56. { return other.x != x || other.y != y;}
  57. // Functions/binary-operators that return points or sizes
  58. inline LDPoint LDPoint::OffsetBy(double dx, double dy) const
  59. { return LDPoint(dx+x, dy+y);}
  60. inline LDPoint LDPoint::OffsetBy(const LDPoint sz) const
  61. { return LDPoint(sz.x + x, sz.y + y);}
  62. inline LDPoint LDPoint::operator +(const LDPoint& sz) const
  63. { return LDPoint(sz.x + x, sz.y + y);}
  64. inline LDPoint LDPoint::operator -(const LDPoint& other) const
  65. { return LDPoint(x-other.x, y-other.y);}
  66. inline LDPoint LDPoint::operator -() const
  67. { return LDPoint(-x, -y);}
  68. inline double LDPoint::DistFrom2(const LDPoint& o) const
  69. { return ((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y));}
  70. inline double LDPoint::DistFrom(const LDPoint& o) const
  71. { return(sqrt(DistFrom2(o)));}
  72. inline LDPoint LDPoint::ScaledBy(double f) const
  73. {return LDPoint(x * f, y * f);}
  74. inline LDPoint LDPoint::operator*(double f) const
  75. {return LDPoint(x * f, y * f);}
  76. inline CPoint LDPoint::GetCPoint() const
  77. { return CPoint(static_cast<int>(x + 0.5), static_cast<int>(y + 0.5));}
  78. // Functions/assignement-operators that modify this point
  79. inline LDPoint& LDPoint::Offset(double dx, double dy)
  80. {x += dx; y += dy; return *this;}
  81. inline LDPoint& LDPoint::Offset(const LDPoint& sz)
  82. {x += sz.x; y += sz.y; return *this;}
  83. inline LDPoint& LDPoint::operator +=(const LDPoint& sz)
  84. {x += sz.x; y += sz.y; return *this;}
  85. inline LDPoint& LDPoint::operator -=(const LDPoint& sz)
  86. {x -= sz.x; y -= sz.y; return *this;}
  87. inline LDPoint& LDPoint::Scale(double f)
  88. {x *= f; y *= f; return *this;}
  89. inline LDPoint& LDPoint::operator*=(double f)
  90. {x *= f; y *= f; return *this;}