Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <linalg.h>
- #include <iostream>
- using namespace linalg::aliases;
- bool intercectionSphere(const float3& p, const float3& u, const float3& c, const float& r, float3*& p1, float3*& p2)
- {
- bool k;
- float alpha = dot(-(p - c), u);
- float3 q = p + alpha * u;
- float bsq = length(q - c) * length(q - c);
- if (bsq > r * r)
- return false;
- float a = sqrt(r * r - bsq);
- if (alpha >= a)
- {
- p1 = new float3(p + (alpha - a) * u);
- k = true;
- }
- if (alpha + a > 0)
- {
- p2 = new float3(p + (alpha + a) * u);
- k = true;
- }
- return k;
- }
- int main()
- {
- float3 p{ 2,0,0 };
- float3 u = normalize(p);
- float3 centreSphere{ 9,-1,0 };
- float radiusSphere = 2.0f;
- float3 *p1, *p2;
- p1 = p2 = nullptr;
- if (intercectionSphere(p, u, centreSphere, radiusSphere, p1, p2));
- {
- if (p1 != nullptr)
- std::cout << p1->x << " " << p1->y << " " << p1->z << "\n";
- if (p2 != nullptr)
- std::cout << p2->x << " " << p2->y << " " << p2->z << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement