fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. double ComputePanelWidth2(double fovDegrees, double d, double r) {
  7. double fovRadians = fovDegrees * M_PI / 180.0;
  8. if (r == 0)
  9. {
  10. // Ensure width is non-negative
  11. return 2.0f * d * std::tan(fovRadians / 2.0f);
  12. }
  13.  
  14. float fovDistance = std::cos(fovRadians / 2.0f) * (d - r) + std::sqrt(r * r - std::pow((d - r) * std::sin(fovRadians / 2.0f), 2));
  15. return 2.0f * fovDistance * std::sin(fovRadians / 2.0f);
  16. }
  17.  
  18. double ComputePanelWidth(double fovDegrees, double d, double r) {
  19. //double fovRadians = fovDegrees * M_PI / 180.0;
  20. //return std::sin(fovRadians) * (d - r) + std::sqrt(r * r - std::pow((d - r) * std::cos(fovRadians), 2));
  21. float targetWidth;
  22. float fovRad = fovDegrees * M_PI / 180.0;
  23. float fovHalfRad = fovRad / 2.0f;
  24.  
  25. if (r == 0)
  26. {
  27. targetWidth = 2.0f * d * std::tan(fovRad / 2.0f);
  28. // Ensure width is non-negative
  29. return std::max(0.0f, targetWidth);
  30. }
  31.  
  32.  
  33. // Precompute sin and cos of half FOV
  34. float sinFovHalf = std::sin(fovHalfRad);
  35. float cosFovHalf = std::cos(fovHalfRad);
  36.  
  37. // Calculate y_c = d - r (as per formula derivation)
  38. float yc = d - r;
  39.  
  40. // Calculate term inside the square root: r² - y_c² * sin²(FOV/2)
  41. float termInsideSqrt = (r * r) - (yc * yc * sinFovHalf * sinFovHalf);
  42.  
  43. // Check condition: term inside sqrt must be non-negative
  44. if (termInsideSqrt >= 0)
  45. {
  46. // Calculate generalized t = cos(FOV/2)*y_c + sqrt( r² - y_c² * sin²(FOV/2) )
  47. float t_generalized = cosFovHalf * yc + std::sqrt(termInsideSqrt);
  48.  
  49. // Check condition: t (distance to intersection) must be positive
  50. if (t_generalized > 0)
  51. {
  52. // Calculate Panel Width = 2 * t * sin(FOV/2)
  53. targetWidth = 2.0f * t_generalized * sinFovHalf;
  54. }
  55. else
  56. {
  57. // Intersection occurs behind the viewer or at the origin, width is effectively zero
  58. targetWidth = 0.0f;
  59. }
  60. }
  61. return targetWidth;
  62. }
  63.  
  64. int main() {
  65. // your code goes here
  66. double fov = 60.0; // degrees
  67. double d = 1.8;
  68. double r = 0;
  69.  
  70. for (double nfov = 50; nfov <=68; nfov++)
  71. {
  72. std::cout << "Panel width fov: " << nfov << " " << ComputePanelWidth(nfov, d, r) << " " << ComputePanelWidth2(nfov, d, r) << std::endl;
  73. }
  74. return 0;
  75. }
Success #stdin #stdout 0s 5296KB
stdin
Standard input is empty
stdout
Panel width fov: 50 1.67871 1.67871
Panel width fov: 51 1.71711 1.71711
Panel width fov: 52 1.75584 1.75584
Panel width fov: 53 1.79489 1.79489
Panel width fov: 54 1.83429 1.83429
Panel width fov: 55 1.87404 1.87404
Panel width fov: 56 1.91415 1.91415
Panel width fov: 57 1.95464 1.95464
Panel width fov: 58 1.99551 1.99551
Panel width fov: 59 2.03678 2.03678
Panel width fov: 60 2.07846 2.07846
Panel width fov: 61 2.12056 2.12056
Panel width fov: 62 2.1631 2.1631
Panel width fov: 63 2.20608 2.20608
Panel width fov: 64 2.24953 2.24953
Panel width fov: 65 2.29345 2.29345
Panel width fov: 66 2.33787 2.33787
Panel width fov: 67 2.38279 2.38279
Panel width fov: 68 2.42823 2.42823