-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c9baca
commit ec75d16
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "Sphere.h" | ||
|
||
|
||
using namespace Supernova; | ||
|
||
|
||
Sphere::Sphere() : center(Vector3(0, 0, 0)), radius(1.0f){ | ||
|
||
} | ||
|
||
Sphere::Sphere(Vector3 c, float r) : center(c), radius(r){ | ||
|
||
} | ||
|
||
Sphere::Sphere(const Sphere& s){ | ||
this->center = s.center; | ||
this->radius = s.radius; | ||
} | ||
|
||
Sphere& Sphere::operator = (const Sphere& s){ | ||
this->center = s.center; | ||
this->radius = s.radius; | ||
|
||
return *this; | ||
} | ||
|
||
bool Sphere::operator == (const Sphere& s){ | ||
return ((this->center == s.center) && (this->radius == s.radius)); | ||
} | ||
|
||
bool Sphere::operator != (const Sphere& s){ | ||
return ((this->center != s.center) || (this->radius != s.radius)); | ||
} | ||
|
||
std::string Sphere::toString() const { | ||
return std::string("Sphere: center (") + center.toString() + "), radius " + std::to_string(radius); | ||
} | ||
|
||
bool Sphere::contains(const Vector3& point) const { | ||
return (point - center).squaredLength() <= (radius * radius); | ||
} | ||
|
||
bool Sphere::intersects(const Sphere& other) const { | ||
float distanceSquared = (center - other.center).squaredLength(); | ||
float radiusSum = radius + other.radius; | ||
return distanceSquared <= (radiusSum * radiusSum); | ||
} | ||
|
||
float Sphere::surfaceArea() const { | ||
return 4 * M_PI * radius * radius; | ||
} | ||
|
||
float Sphere::volume() const { | ||
return (4.0f / 3.0f) * M_PI * radius * radius * radius; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef sphere_h | ||
#define sphere_h | ||
|
||
#include <cmath> | ||
#include "Vector3.h" // Ensure you have this header for the Vector3 class | ||
|
||
namespace Supernova{ | ||
class Sphere { | ||
public: | ||
Vector3 center; | ||
float radius; | ||
|
||
Sphere(); | ||
Sphere(Vector3 c, float r); | ||
Sphere(const Sphere& s); | ||
|
||
Sphere& operator = (const Sphere& s); | ||
bool operator == (const Sphere& s); | ||
bool operator != (const Sphere& s); | ||
|
||
std::string toString() const; | ||
|
||
bool contains(const Vector3& point) const; | ||
bool intersects(const Sphere& other) const; | ||
|
||
float surfaceArea() const; | ||
float volume() const; | ||
}; | ||
} | ||
|
||
#endif |