Skip to content

Commit

Permalink
add set and get
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmags committed Apr 30, 2023
1 parent 0b42352 commit a0e52ca
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/quaternion/quaternion.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,26 @@ void setup() {
Serial.flush();

//-------- Quaternions --------

// 0. Setters and getters:
quat_t q1 = { 1, 0, 0, 0 };
quat_t q2 = { 0, 1, 0, 0 };
quat_t q3 = { 0, 0, 1, 0 };
quat_t q4 = { 0, 0, 0, 1 };

// initialize with vector as array
quat_t q5;

for( int i = 0; i < 4; i += 1 ) {
q5.set(i, 2*i + 1); // (index, value)
}

/* access component at index using: quat_t.get(index) */

// copy vector into array
float arr_q[4]; q5.copyArray(arr_q);
printOperation( "0. Copy into array", quat_t(arr_q) );

// 1. Basic operations:
// Quaternions can be manipulated in the same way as scalar values

Expand Down
16 changes: 15 additions & 1 deletion examples/vector/vector.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,30 @@ void setup() {
Serial.flush();

//-------- Vectors --------

// 0. Setters and getters:
vec3_t v1 = { 1, 0, 0 };
vec3_t v2 = { 0, 1, 0 };
vec3_t v3 = { 0, 0, 1 };

// initialize with vector as array
vec3_t v4;

for( int i = 0; i < 3; i += 1 ) {
v4.set(i, 2*i + 1); // (index, value)
}

/* access component at index using: vec3_t.get(index) */

// copy vector into array
float arr_v[3]; v4.copyArray(arr_v);
printOperation( "0. Copy into array", vec3_t(arr_v) );

// 1. Operations:
// Vectors can be manipulated in the same way as scalar values

// Addition
v4= v1 + v2;
v4 = v1 + v2;
printOperation( "1. Addition: ", v4 );

// Subtraction
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Vector datatype
version=1.2.1
version=1.3.0
author=RCmags <[email protected]>
maintainer=RCmags <[email protected]>
sentence=Library for 3d vectors and quaternions
Expand Down
23 changes: 23 additions & 0 deletions src/quaternion_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ quat_t::quat_t( float arr[] ) {
v.z = arr[3];
}

//--------------- Setters and getters -----------------

void quat_t::copyArray( float s[] ) {
s[0] = w;
v.copyArray(s + 1);
}

void quat_t::set(const int i, float input) {
if( i == 0 ) {
w = input;
} else {
v.set(i - 1, input);
}
}

float quat_t::get(const int i) {
if( i == 0 ) {
return w;
} else {
return v.get(i - 1);
}
}

//----------------- Basic Operations ------------------

//-- Addition and subtraction:
Expand Down
5 changes: 5 additions & 0 deletions src/quaternion_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ struct quat_t {
quat_t( vec3_t );
quat_t( float [] );

// 0A. Setters and getters
void copyArray( float [] );
void set(const int, float);
float get(const int);

// 1. Basic Operations:
// Addition and subtraction:
quat_t operator + ( const quat_t & );
Expand Down
36 changes: 36 additions & 0 deletions src/vector_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ vec3_t operator * ( const float s, const vec3_t &r ) {
return v;
}

//--------------- Setters and getters ----------------

void vec3_t::copyArray( float s[] ) {
s[0] = x;
s[1] = y;
s[2] = z;
}

void vec3_t::set(const int i, float input) {
switch(i) {
case 0:
x = input;
break;
case 1:
y = input;
break;
case 2:
z = input;
break;
}
}

float vec3_t::get(const int i) {
switch(i) {
case 0:
return x;
break;
case 1:
return y;
break;
case 2:
return z;
break;
}
}

//--------------- Important operations ---------------

// Dot product
Expand Down
5 changes: 5 additions & 0 deletions src/vector_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ struct vec3_t {
vec3_t cross( const vec3_t );
float mag();
vec3_t norm();

// 2. setters and getters
void copyArray( float [] );
void set(const int, float);
float get(const int);
};

// 1B. Reverse order - Scalar product
Expand Down

0 comments on commit a0e52ca

Please sign in to comment.