Skip to content

Commit

Permalink
reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
tbhaxor committed Oct 13, 2019
1 parent 100429e commit bbf5251
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": false
}
43 changes: 22 additions & 21 deletions INCLUDES/firefly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,28 @@
#include <vector>
using namespace std;

class Vectors {
public:
vector<float> vec;
Vectors(vector<float> vector);
Vectors();
void print();
Vectors add(Vectors v);
Vectors subtract(Vectors v);
Vectors scalorMultiply(float number);
float magnitude();
Vectors normalize();
float dotProductWith(Vectors v);
float angleWith(Vectors v, bool degree);
bool isParallelWith(Vectors v);
bool isOrthogonalWith(Vectors v);
Vectors componentParallelTo(Vectors b);
Vectors componentOrthogonalTo(Vectors b);
Vectors crossProductWith(Vectors v);
float areaOfTriangleWith(Vectors v);
float areaOfParallelogramWith(Vectors v);
bool isEqualTo(Vectors v);
class Vectors
{
public:
vector<float> vec;
Vectors(vector<float> vector);
Vectors();
void print();
Vectors add(Vectors v);
Vectors subtract(Vectors v);
Vectors scalorMultiply(float number);
float magnitude();
Vectors normalize();
float dotProductWith(Vectors v);
float angleWith(Vectors v, bool degree);
bool isParallelWith(Vectors v);
bool isOrthogonalWith(Vectors v);
Vectors componentParallelTo(Vectors b);
Vectors componentOrthogonalTo(Vectors b);
Vectors crossProductWith(Vectors v);
float areaOfTriangleWith(Vectors v);
float areaOfParallelogramWith(Vectors v);
bool isEqualTo(Vectors v);
};

#endif
112 changes: 72 additions & 40 deletions src/firefly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,73 @@
#include <cstdlib>
#include <cmath>
#include "./firefly.hpp"
#include <type_traits>
using namespace std;

Vectors::Vectors() {}

Vectors::Vectors(vector<float> vec) {
Vectors::Vectors(vector<float> vec)
{
this->vec = vec;
}

void Vectors::print() {
cout<<"[ ";
for(int i = 0 ;i < vec.size(); ++i) {
cout<<vec[i]<<", ";
void Vectors::print()
{
cout << "[ ";
for (int i = 0; i < vec.size(); ++i)
{
cout << vec[i] << ", ";
}
cout<<"]";
cout << "]";
}

Vectors Vectors::add(Vectors v) {
if (this->vec.size() != v.vec.size()) {
Vectors Vectors::add(Vectors v)
{
if (this->vec.size() != v.vec.size())
{
cout << "Exception: Vectors size don't match";
exit(1);
}
vector<float> tmp;
for(int i = 0; i < this->vec.size(); ++i) {
for (int i = 0; i < this->vec.size(); ++i)
{
tmp.push_back(this->vec[i] + v.vec[i]);
}
return Vectors(tmp);
}

Vectors Vectors::subtract(Vectors v) {
if (this->vec.size() != v.vec.size()) {
Vectors Vectors::subtract(Vectors v)
{
if (this->vec.size() != v.vec.size())
{
cout << "Exception: Vectors size don't match";
exit(1);
}
vector<float> tmp;
for (int i = 0; i < this->vec.size(); ++i) {
for (int i = 0; i < this->vec.size(); ++i)
{
tmp.push_back(this->vec[i] - v.vec[i]);
}
return Vectors(tmp);
}

Vectors Vectors::scalorMultiply(float number) {
if (number == 0.0f) {
Vectors Vectors::scalorMultiply(float number)
{
if (number == 0.0f)
{
cout << "Exception: Zero Divide Error";
exit(1);
}
vector<float> tmp;
for (int i = 0; i < this->vec.size(); ++i) {
for (int i = 0; i < this->vec.size(); ++i)
{
tmp.push_back(this->vec[i] * number);
}
return Vectors(tmp);
}

float Vectors::magnitude() {
float Vectors::magnitude()
{
// this the method 1
float dot = this->dotProductWith(Vectors(this->vec));
return sqrt(dot);
Expand All @@ -67,39 +81,47 @@ float Vectors::magnitude() {
// return sqrt(sum);
}

Vectors Vectors::normalize() {
Vectors Vectors::normalize()
{
float mag = this->magnitude();
if (mag == 0.0f) {
cout<<"Exception: Zero Divide Error";
if (mag == 0.0f)
{
cout << "Exception: Zero Divide Error";
exit(1);
}
vector<float> n;
for(int i = 0; i < this->vec.size(); ++i) {
for (int i = 0; i < this->vec.size(); ++i)
{
n.push_back(this->vec[i] / mag);
}

return Vectors(n);
}

float Vectors::dotProductWith(Vectors v) {
float Vectors::dotProductWith(Vectors v)
{
float sum = 0.0f;
if(this->vec.size() != v.vec.size()) {
cout<<"Exception: Vectors size don't match";
if (this->vec.size() != v.vec.size())
{
cout << "Exception: Vectors size don't match";
exit(1);
}
for(int i = 0; i < this->vec.size(); ++i) {
for (int i = 0; i < this->vec.size(); ++i)
{
sum += this->vec[i] * v.vec[i];
}
return sum;
}

float Vectors::angleWith(Vectors v, bool degree = false) {
float Vectors::angleWith(Vectors v, bool degree = false)
{
// this is the first method
float dot = this->dotProductWith(v);
float mag1 = this->magnitude();
float mag2 = v.magnitude();
float radians = acos(dot / (mag1 * mag2));
if(degree) {
if (degree)
{
return radians * (180.0 / M_PI);
}
return radians;
Expand All @@ -115,59 +137,69 @@ float Vectors::angleWith(Vectors v, bool degree = false) {
// return radians;
}

bool Vectors::isParallelWith(Vectors v) {
if(this->magnitude() == 0 or v.magnitude() == 0) {
bool Vectors::isParallelWith(Vectors v)
{
if (this->magnitude() == 0 or v.magnitude() == 0)
{
return true;
}
return this->angleWith(v) == 180.0 or this->angleWith(v) == 0.0;
}

bool Vectors::isOrthogonalWith(Vectors v) {
if (this->magnitude() == 0 or v.magnitude() == 0) {
bool Vectors::isOrthogonalWith(Vectors v)
{
if (this->magnitude() == 0 or v.magnitude() == 0)
{
return true;
}
return this->angleWith(v, true) == 90;
}

Vectors Vectors::componentParallelTo(Vectors b) {
Vectors Vectors::componentParallelTo(Vectors b)
{
Vectors u = b.normalize();
float dot = this->dotProductWith(u);
return Vectors(u.scalorMultiply(dot));
}

Vectors Vectors::componentOrthogonalTo(Vectors v) {
Vectors Vectors::componentOrthogonalTo(Vectors v)
{
Vectors p = this->componentParallelTo(v);
return Vectors(this->subtract(p));
}

Vectors Vectors::crossProductWith(Vectors v) {
Vectors Vectors::crossProductWith(Vectors v)
{
float x1, x2, y1, y2, z1 = 0, z2 = 0;
x1 = this->vec[0];
x2 = v.vec[0];
y1 = this->vec[1];
y2 = v.vec[1];
if(this->vec.size() == 3 and v.vec.size() == 3) {
if (this->vec.size() == 3 and v.vec.size() == 3)
{
z1 = this->vec[2];
z2 = v.vec[2];
}

vector<float> new_vector = {
y1 * z2 - y2 * z1,
-(x1 * z2 - x2 * z1),
x1 * y2 - x2 * y1
};

x1 * y2 - x2 * y1};

return Vectors(new_vector);
}

float Vectors::areaOfParallelogramWith(Vectors v) {
float Vectors::areaOfParallelogramWith(Vectors v)
{
return this->crossProductWith(v).magnitude();
}

float Vectors::areaOfTriangleWith(Vectors v) {
float Vectors::areaOfTriangleWith(Vectors v)
{
return this->areaOfParallelogramWith(v) / 2;
}

bool Vectors::isEqualTo(Vectors v) {
bool Vectors::isEqualTo(Vectors v)
{
return this->vec == v.vec;
}

0 comments on commit bbf5251

Please sign in to comment.