Advertisement
codisinmyvines

forAynur(raytrace)

Oct 20th, 2021
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <linalg.h>
  3. #include <iostream>
  4. using namespace linalg::aliases;
  5.  
  6. bool intercectionSphere(const float3& p, const float3& u, const float3& c, const float& r, float3*& p1, float3*& p2)
  7. {
  8.     bool k;
  9.     float alpha = dot(-(p - c), u);
  10.     float3 q = p + alpha * u;
  11.     float bsq = length(q - c) * length(q - c);
  12.     if (bsq > r * r)
  13.         return false;
  14.     float a = sqrt(r * r - bsq);
  15.     if (alpha >= a)
  16.     {
  17.         p1 = new float3(p + (alpha - a) * u);
  18.         k = true;
  19.     }
  20.     if (alpha + a > 0)
  21.     {
  22.         p2 = new float3(p + (alpha + a) * u);
  23.         k = true;
  24.     }
  25.     return k;
  26. }
  27.  
  28. int main()
  29. {
  30.     float3 p{ 2,0,0 };
  31.     float3 u = normalize(p);
  32.     float3 centreSphere{ 9,-1,0 };
  33.     float radiusSphere = 2.0f;
  34.     float3 *p1, *p2;
  35.     p1 = p2 = nullptr;
  36.     if (intercectionSphere(p, u, centreSphere, radiusSphere, p1, p2));
  37.     {
  38.         if (p1 != nullptr)
  39.             std::cout << p1->x << " " << p1->y << " " << p1->z << "\n";
  40.         if (p2 != nullptr)
  41.             std::cout << p2->x << " " << p2->y << " " << p2->z << "\n";
  42.     }
  43.    
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement