Last active
November 4, 2021 09:05
-
-
Save Saafan/ac864874d9234d7632b270b673b383fd to your computer and use it in GitHub Desktop.
Vector3 Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
struct Vector3 | |
{ | |
Vector3() = default; | |
Vector3(float x, float y, float z) : x(x), y(y), z(z) {} | |
float x, y, z; | |
Vector3 operator+(Vector3); | |
Vector3 operator-(Vector3); | |
float operator*(Vector3 v); | |
Vector3 operator*=(Vector3 v); | |
Vector3 operator*(float value); | |
Vector3 operator/(float value); | |
Vector3 operator/=(Vector3 v); | |
Vector3 operator!(); | |
Vector3 operator-(); | |
friend std::ostream& operator<<(std::ostream& os, Vector3& vec); | |
friend std::istream& operator>>(std::istream& is, Vector3& vec); | |
}; | |
Vector3 Vector3::operator-(Vector3 v) | |
{ | |
Vector3 vec(x - v.x, y - v.y, z - v.z); | |
return vec; | |
} | |
Vector3 Vector3::operator-() | |
{ | |
Vector3 vec(-x, -y, -z); | |
return vec; | |
} | |
Vector3 Vector3::operator+(Vector3 v) | |
{ | |
Vector3 vec(x + v.x, y + v.y, z + v.z); | |
return vec; | |
} | |
float Vector3::operator*(Vector3 v) | |
{ | |
return (x * v.x) + (y * v.y) + (z * v.z); | |
} | |
Vector3 Vector3::operator*(float value) | |
{ | |
Vector3 vec(x * value, y * value, z * value); | |
return vec; | |
} | |
Vector3 Vector3::operator*=(Vector3 v) | |
{ | |
Vector3 vec(x * v.x, y * v.y, z * v.z); | |
return vec; | |
} | |
Vector3 Vector3::operator/(float value) | |
{ | |
Vector3 vec(x / value, y / value, z / value); | |
return vec; | |
} | |
Vector3 Vector3::operator/=(Vector3 v) | |
{ | |
Vector3 vec(x / v.x, y / v.y, z / v.z); | |
return vec; | |
} | |
Vector3 Vector3::operator!() | |
{ | |
float magnitude = std::sqrt((x * x) + (y * y) + (z * z)); | |
Vector3 vec(x / magnitude, y / magnitude, z / magnitude); | |
return vec; | |
} | |
std::ostream& operator<<(std::ostream& os, Vector3& vec) | |
{ | |
os << "(x: " << vec.x << ", y:" << vec.y << ", z:" << vec.z << ")" << std::endl; | |
return os; | |
} | |
std::istream& operator>>(std::istream& is, Vector3& vec) | |
{ | |
std::cout << "Enter x: "; | |
is >> vec.x; | |
std::cout << "Enter y: "; | |
is >> vec.y; | |
std::cout << "Enter z: "; | |
is >> vec.z; | |
return is; | |
} | |
Vector3& GetPlaneIntersection(Vector3& p0, Vector3& p1, Vector3& n, float d) | |
{ | |
Vector3 v = p1 - p0; | |
const float a = -(p0 * n) + d; | |
const float b = v * n; | |
const float t = a / b; | |
Vector3 intersectionPoint = p0 + (v * t); | |
return intersectionPoint; | |
} | |
Vector3& GetSphereIntersection(Vector3 p0, Vector3 p1, Vector3 origin, float radius) | |
{ | |
Vector3 a = origin - p0; | |
Vector3 v = !(p1 - p0); | |
Vector3 intersectionPoint = p0 + (v * (a * v - std::sqrt(radius * radius - (a * a - ((a * v) * (a * v)))))); | |
return intersectionPoint; | |
} | |
float GetSphereIncidentAngle(Vector3& p0, Vector3& p1, Vector3& origin, float radius) | |
{ | |
Vector3 intersectionPoint = GetSphereIntersection(p0, p1, origin, radius); | |
Vector3 vDash = -!(p1 - p0); | |
Vector3 n = !(intersectionPoint - origin); | |
float angle = std::acos(vDash * n); | |
std::cout << intersectionPoint << std::endl; | |
std::cout << angle * (180 / (22/7)) << std::endl; | |
return angle; | |
} | |
int main() | |
{ | |
Vector3 p0; | |
Vector3 p1; | |
Vector3 origin; | |
float radius = 0.0f; | |
std::cout << "Enter P0" << std::endl; | |
std::cin >> p0; | |
std::cout << "Enter P1" << std::endl; | |
std::cin >> p1; | |
std::cout << "Enter o" << std::endl; | |
std::cin >> origin; | |
std::cout << "Enter Radius: " << std::endl; | |
std::cin >> radius; | |
std::cout << "Incident Angle: " << GetSphereIncidentAngle(p0, p1, origin, radius) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment