fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdbool.h>
  4. #include <float.h>
  5.  
  6. //-----------------------------------------------------
  7. // { to calculate angles between 2 vectors
  8. float get_angle_clockwise_atan2(float Avec[2], float Bvec[2])
  9. {
  10. float angle_rad = atan2(Avec[1], Avec[0]) - atan2(Bvec[1], Bvec[0]);
  11. float angle_deg = angle_rad * 180.0 / acos(-1.0);
  12. if (angle_deg >= 0.0) {
  13. return angle_deg;
  14. } else {
  15. return angle_deg + 360.0;
  16. }
  17. }
  18.  
  19. float get_angle_clockwise_wrapper(float x1, float y1, float x2, float y2)
  20. {
  21. float Avec[2];
  22. float Bvec[2];
  23.  
  24. Avec[0] = x1;
  25. Avec[1] = y1;
  26. Bvec[0] = x2;
  27. Bvec[1] = y2;
  28. return get_angle_clockwise_atan2(Avec, Bvec);
  29. }
  30. // } to calculate angles between 2 vectors
  31. //-----------------------------------------------------
  32.  
  33. bool isInside(float avec[2], float minvec[2], float maxvec[2])
  34. {
  35. float aAndMin = get_angle_clockwise_atan2(minvec, avec);
  36. float minAndMax = get_angle_clockwise_atan2(minvec, maxvec);
  37.  
  38. #if 0 // debug
  39. printf("%f %f\n", aAndMin, minAndMax);
  40. #endif
  41. return (aAndMin <= minAndMax);
  42. }
  43. bool isInside_wrapper(float a_x, float a_y, float min_x, float min_y,
  44. float max_x, float max_y)
  45. {
  46. float avec[2];
  47. float minvec[2];
  48. float maxvec[2];
  49.  
  50. avec[0] = a_x;
  51. avec[1] = a_y;
  52. minvec[0] = min_x;
  53. minvec[1] = min_y;
  54. maxvec[0] = max_x;
  55. maxvec[1] = max_y;
  56. return isInside(avec, minvec, maxvec);
  57. }
  58.  
  59. int main(void) {
  60. printf("%d \n", isInside_wrapper(1,1, 0,1, 1,0)); // inside
  61. printf("%d \n", isInside_wrapper(0,1, 0,1, 1,0)); // inside
  62. printf("%d \n", isInside_wrapper(1,0, 0,1, 1,0)); // inside
  63. printf("%d \n", isInside_wrapper(-1,-1, 0,1, 1,0)); // outside
  64.  
  65. float ax, ay;
  66. float deg2rad = acos(-1.0) / 180.0;
  67. ax = 1.0 * cos(105 * deg2rad);
  68. ay = 1.0 * sin(105 * deg2rad);
  69. printf("%d \n", isInside_wrapper(ax,ay, 0,1, 1,0)); // outside
  70. ax = 1.0 * cos(359 * deg2rad);
  71. ay = 1.0 * sin(359 * deg2rad);
  72. printf("%d \n", isInside_wrapper(ax,ay, 0,1, 1,0)); // outside
  73. ax = 1.0 * cos(365 * deg2rad);
  74. ay = 1.0 * sin(365 * deg2rad);
  75. printf("%d \n", isInside_wrapper(ax,ay, 0,1, 1,0)); // inside
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
1 
1 
1 
0 
0 
0 
1