AndroidMathUtility
Supportive package for AndroidMath
Description
This is the second part of AndroidMath example.
On this page we are showing the sources of AndroidMathUtility package. The code from this package is called from AndroidMath.
The package contains simply math utility. The class Vector represents math vector. There is also Power function that calculates power.
Vector.h
#ifndef _AndroidMathUtility_Vector_h_
#define _AndroidMathUtility_Vector_h_
#include <string>
namespace AndroidMathUtility {
class Vector {
public:
Vector();
Vector(int size);
Vector(const Vector& vec);
virtual ~Vector();
float Get(int id) const;
int GetSize() const;
void Set(int id, float value);
void MultipleByScalar(float scalar);
std::string ToString() const;
private:
float* data;
int size;
};
}
#endif
AndroidMathUtility.h
#ifndef _AndroidMathUtility_AndroidMathUtiliy_h
#define _AndroidMathUtility_AndroidMathUtiliy_h
#include "Vector.h"
namespace AndroidMathUtility {
int Power(int number, int n);
}
#endif
Vector.cpp
#include <sstream>
#include "AndroidMathUtility.h"
namespace AndroidMathUtility {
Vector::Vector()
{
this->size = 0;
this->data = NULL;
}
Vector::Vector(int size)
{
data = new float[size];
for(int i = 0; i < size; i++) {
data[i] = 0.0f;
}
this->size = size;
}
Vector::Vector(const Vector& vec)
{
if(vec.GetSize() > 0) {
this->size = vec.GetSize();
this->data = new float[size];
for(int i = 0; i < size; i++) {
this->data[i] = vec.data[i];
}
}
else {
this->size = 0;
this->data = NULL;
}
}
Vector::~Vector()
{
delete[] data;
}
float Vector::Get(int id) const
{
return this->data[id];
}
int Vector::GetSize() const
{
return this->size;
}
void Vector::Set(int id, float value)
{
this->data[id] = value;
}
void Vector::MultipleByScalar(float scalar)
{
for(int i = 0; i < size; i++) {
this->data[i] *= scalar;
}
}
std::string Vector::ToString() const
{
std::stringstream ss;
ss << "[";
for(int i = 0; i < size; i++) {
ss << data[i];
if(i + 1 < size)
ss << ", ";
}
ss << "]";
return ss.str();
}
}
AndroidMathUtility.cpp
#include "AndroidMathUtility.h"
namespace AndroidMathUtility {
int Power(int number, int n) {
int result = number;
for(int i = 0; i < n - 1; i++) {
result *= number;
}
return result;
}
}
|