Circle.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /******************************************************************************
  2. Use 'Circle' to handle circle shapes, Flt type
  3. Use 'CircleD' to handle circle shapes, Dbl type
  4. /******************************************************************************/
  5. struct Circle // Circle Shape
  6. {
  7. Flt r ; // radius
  8. Vec2 pos; // center position
  9. // set
  10. Circle& set(Flt r, C Vec2 &pos=Vec2(0,0)) {T.r=r; T.pos=pos ; return T;}
  11. Circle& set(Flt r, Flt x, Flt y ) {T.r=r; T.pos.set(x, y); return T;}
  12. // get
  13. Flt area ()C {return PI *r*r;} // get surface area
  14. Flt perimeter()C {return PI2*r ;} // get perimeter
  15. Str asText()C {return S+"Radius: "+r+", Pos: "+pos;} // get text description
  16. // transform
  17. Circle& operator+=(C Vec2 &v) { pos+=v; return T;}
  18. Circle& operator-=(C Vec2 &v) { pos-=v; return T;}
  19. Circle& operator*=( Flt f) {r*=f; pos*=f; return T;}
  20. Circle& operator/=( Flt f) {r/=f; pos/=f; return T;}
  21. friend Circle operator+ (C Circle &circle, C Vec2 &v) {return Circle(circle)+=v;}
  22. friend Circle operator- (C Circle &circle, C Vec2 &v) {return Circle(circle)-=v;}
  23. friend Circle operator* (C Circle &circle, Flt f) {return Circle(circle)*=f;}
  24. friend Circle operator/ (C Circle &circle, Flt f) {return Circle(circle)/=f;}
  25. // operations
  26. Circle& extend(Flt e) {r+=e; return T;} // extend
  27. // draw
  28. void draw(C Color &color, Bool fill=true, Int resolution=-1)C;
  29. void drawPie(C Color &color, Flt r_start, Flt angle_start=0, Flt angle_range=PI2, Bool fill=true, Int resolution=-1)C; // draw pie, 'r_start'=radius at which start drawing (0..'r'), 'angle_start'=angle at which start drawing (0..PI2), 'angle_range'=angle range (0..PI2)
  30. Circle() {}
  31. Circle(Flt r, C Vec2 &pos=Vec2(0,0)) {set(r, pos );}
  32. Circle(Flt r, Flt x, Flt y ) {set(r, x, y);}
  33. };
  34. /******************************************************************************/
  35. struct CircleD // Circle Shape (double precision)
  36. {
  37. Dbl r ; // radius
  38. VecD2 pos; // center
  39. // set
  40. CircleD& set(Dbl r, C VecD2 &pos=VecD2(0,0)) {T.r=r; T.pos=pos ; return T;}
  41. CircleD& set(Dbl r, Dbl x, Dbl y ) {T.r=r; T.pos.set(x, y); return T;}
  42. // get
  43. Dbl area ()C {return PID *r*r;} // get surface area
  44. Dbl perimeter()C {return PID2*r ;} // get perimeter
  45. CircleD() {}
  46. CircleD(Dbl r, C VecD2 &pos=VecD2(0,0)) {set(r, pos );}
  47. CircleD(Dbl r, Dbl x, Dbl y ) {set(r, x, y);}
  48. };
  49. /******************************************************************************/
  50. // distance
  51. Flt Dist(C Vec2 &point, C Circle &circle); // distance between point and a circle
  52. Flt Dist(C VecI2 &point, C Circle &circle); // distance between point and a circle
  53. Flt Dist(C Rect &rect , C Circle &circle); // distance between rectangle and a circle
  54. Flt Dist(C RectI &rect , C Circle &circle); // distance between rectangle and a circle
  55. Flt Dist(C Circle &a , C Circle &b ); // distance between circle and a circle
  56. // cuts
  57. Bool Cuts(C Vec2 &point, C Circle &circle); // if point cuts circle
  58. Bool Cuts(C VecI2 &point, C Circle &circle); // if point cuts circle
  59. Bool Cuts(C Rect &rect , C Circle &circle); // if rectangle cuts circle
  60. Bool Cuts(C RectI &rect , C Circle &circle); // if rectangle cuts circle
  61. Bool Cuts(C Circle &a , C Circle &b ); // if circle cuts circle
  62. // if straight line cuts circle, return number of contacts
  63. Int CutsStrCircle(C Vec2 &point, C Vec2 &normal, C Circle &circle, Vec2 *contact_a=null, Vec2 *contact_b=null, Flt *width=null);
  64. // if circle cuts circle, returns number of contacts
  65. Int CutsCircleCircle(C Circle &a, C Circle &b, Vec2 *contact_a=null, Vec2 *contact_b=null, Flt *width=null);
  66. Bool Inside(C Rect &a, C Circle &b); // if 'a' is fully inside 'b'
  67. // if moving point cuts through a static circle
  68. Bool SweepPointCircle(C Vec2 &point, C Vec2 &move, C Circle &circle, Flt *hit_frac=null, Vec2 *hit_normal=null);
  69. Bool SweepPointCircle(C VecD2 &point, C VecD2 &move, C CircleD &circle, Dbl *hit_frac=null, VecD2 *hit_normal=null);
  70. // if moving edge cuts through a static circle
  71. Bool SweepEdgeCircle(C Edge2 &edge, C Vec2 &move, C Circle &circle, Flt *hit_frac=null, Vec2 *hit_normal=null);
  72. Bool SweepEdgeCircle(C EdgeD2 &edge, C VecD2 &move, C CircleD &circle, Dbl *hit_frac=null, VecD2 *hit_normal=null);
  73. // if moving circle cuts through a static circle
  74. Bool SweepCircleCircle(C Circle &circle, C Vec2 &move, C Circle &c2, Flt *hit_frac=null, Vec2 *hit_normal=null);
  75. Bool SweepCircleCircle(C CircleD &circle, C VecD2 &move, C CircleD &c2, Dbl *hit_frac=null, VecD2 *hit_normal=null);
  76. // if moving circle cuts through a static point
  77. Bool SweepCirclePoint(C Circle &circle, C Vec2 &move, C Vec2 &point, Flt *hit_frac=null, Vec2 *hit_normal=null);
  78. Bool SweepCirclePoint(C CircleD &circle, C VecD2 &move, C VecD2 &point, Dbl *hit_frac=null, VecD2 *hit_normal=null);
  79. // if moving circle cuts through a static edge
  80. Bool SweepCircleEdge(C Circle &circle, C Vec2 &move, C Edge2 &edge, Flt *hit_frac=null, Vec2 *hit_normal=null);
  81. Bool SweepCircleEdge(C CircleD &circle, C VecD2 &move, C EdgeD2 &edge, Dbl *hit_frac=null, VecD2 *hit_normal=null);
  82. /******************************************************************************/