Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include "GeoA.h"
  2. #include <string> // string
  3. #include <cstring> // strcpy
  4. #include <cmath> // mathematical functions
  5. #include <iostream> // std
  6. #include <cstdlib> // srand, rand
  7. #include <ctime> // time
  8.  
  9. #define __NAME(x) #x
  10.  
  11. double GeoA::random() {
  12. srand (static_cast <unsigned> (time(NULL)));
  13. return static_cast <double> (rand()) / static_cast <double> (RAND_MAX);
  14. }
  15. double GeoA::random(double X) {
  16. srand (static_cast <unsigned> (time(NULL)));
  17. return static_cast <double> (rand()) / (static_cast <double> (RAND_MAX/X));
  18. }
  19. double GeoA::random(double LO, double HI) {
  20. srand (static_cast <unsigned> (time(NULL)));
  21. return LO + static_cast <double> (rand()) /( static_cast <double> (RAND_MAX/(HI-LO)));
  22. }
  23.  
  24. char* GeoA::Vetor::toString() const {
  25. std::string str = "Objeto GeoA::Vetor : [";
  26. str += std::to_string(this->x);
  27. str += ",";
  28. str += std::to_string(this->y);
  29. str += ",";
  30. str += std::to_string(this->z);
  31. str += "]";
  32.  
  33. char* temp = new char[str.size()+1];
  34. strcpy(temp, str.c_str());
  35.  
  36. return temp;
  37. }
  38. GeoA::Vetor* GeoA::Vetor::set(double px, double py, double pz) {
  39. this->x = px;
  40. this->y = py;
  41. this->z = pz;
  42. return this;
  43. }
  44. GeoA::Vetor* GeoA::Vetor::set(GeoA::Vetor* pv) {
  45. this->x = pv->x;
  46. this->y = pv->y;
  47. this->z = pv->z;
  48. return this;
  49. }
  50. GeoA::Vetor* GeoA::Vetor::copy() const {
  51. return new GeoA::Vetor(this->x, this->y, this->z);
  52. }
  53.  
  54. GeoA::Vetor* GeoA::Vetor::add(double px, double py, double pz) {
  55. this->x += px;
  56. this->y += py;
  57. this->z += pz;
  58. return this;
  59. }
  60. GeoA::Vetor* GeoA::Vetor::add(GeoA::Vetor* pv) {
  61. this->x += pv->x;
  62. this->y += pv->y;
  63. this->z += pv->z;
  64. return this;
  65. }
  66. GeoA::Vetor* GeoA::Vetor::add(GeoA::Vetor* pv1, GeoA::Vetor* pv2) {
  67. return new GeoA::Vetor(pv1->x + pv2->y, pv1->y + pv2->y, pv1->z + pv2->z);
  68. }
  69. GeoA::Vetor* GeoA::Vetor::sub(double px, double py, double pz) {
  70. this->x -= px;
  71. this->y -= py;
  72. this->z -= pz;
  73. return this;
  74. }
  75. GeoA::Vetor* GeoA::Vetor::sub(const GeoA::Vetor* pv) {
  76. this->x -= pv->x;
  77. this->y -= pv->y;
  78. this->z -= pv->z;
  79. return this;
  80. }
  81. GeoA::Vetor* GeoA::Vetor::mult(double scl) {
  82. this->x *= scl;
  83. this->y *= scl;
  84. this->z *= scl;
  85. return this;
  86. }
  87. GeoA::Vetor* GeoA::Vetor::div(double scl) {
  88. this->x /= scl;
  89. this->y /= scl;
  90. this->z /= scl;
  91. }
  92. double GeoA::Vetor::mag() {
  93. return sqrt(this->magSq());
  94. }
  95. double GeoA::Vetor::magSq() {
  96. return (this->x * this->x + this->y * this->y + this->z * this->z);
  97. }
  98.  
  99. double GeoA::Vetor::dot(GeoA::Vetor* pv1, GeoA::Vetor* pv2) {
  100. return (pv1->x * pv2->x + pv1->y * pv2->y + pv1->z * pv2->z);
  101. }
  102. GeoA::Vetor* GeoA::Vetor::cross(GeoA::Vetor* pv) const {
  103. double x = this->y * pv->z - this->z * pv->y;
  104. double y = this->z * pv->x - this->x * pv->z;
  105. double z = this->x * pv->y - this->y * pv->x;
  106. return new GeoA::Vetor(x, y, z);
  107. }
  108. double GeoA::Vetor::dist(const GeoA::Vetor* pv) const {
  109. GeoA::Vetor* temp = pv->copy()->sub(this);
  110. return temp->mag();
  111. }
  112. GeoA::Vetor* GeoA::Vetor::normalize() {
  113. return this->mag() == 0 || this->mag() == 1 ? this : this->div(this->mag());
  114. }
  115. GeoA::Vetor* GeoA::Vetor::limit(double max) {
  116. double aSq = this->magSq();
  117. if (aSq > max * max) {
  118. this->div(sqrt(aSq)); // normalize it
  119. this->mult(max);
  120. }
  121. return this;
  122. }
  123. GeoA::Vetor* GeoA::Vetor::setMag(double mag) {
  124. return this->normalize()->mult(mag);
  125. }
  126. double GeoA::Vetor::heading() const {
  127. return atan2(this->y, this->x);
  128. }
  129. GeoA::Vetor* GeoA::Vetor::rotate(double a) {
  130. double newHeading = this->heading() + a;
  131. double mag = this->mag();
  132. this->x = cos(newHeading) * mag;
  133. this->y = sin(newHeading) * mag;
  134. return this;
  135. }
  136. double GeoA::Vetor::angleBetween(GeoA::Vetor* pv1, GeoA::Vetor* pv2) {
  137. return acos(GeoA::Vetor::dot(pv1, pv2) / (pv1->mag() * pv2->mag()));
  138. }
  139.  
  140. GeoA::Vetor* GeoA::Vetor::lerp(GeoA::Vetor* pv1, GeoA::Vetor* pv2, double amt) {
  141. Vetor* target = pv1->copy();
  142. target->x += (pv2->x - pv1->x) * amt;
  143. target->y += (pv2->y - pv1->y) * amt;
  144. target->z += (pv2->z - pv1->z) * amt;
  145. return target;
  146. }
  147. double* GeoA::Vetor::array() const {
  148. double* data = new double[3];
  149. data[0] = this->x;
  150. data[1] = this->y;
  151. data[2] = this->z;
  152. return data;
  153. }
  154. bool GeoA::Vetor::equals(GeoA::Vetor* pv1, GeoA::Vetor* pv2) {
  155. return (pv1->x == pv2->x && pv1->y == pv2->y && pv1->z == pv2->z);
  156. }
  157. GeoA::Vetor* GeoA::Vetor::fromAngle(double angle) {
  158. return new GeoA::Vetor(cos(angle), sin(angle), 0);
  159. }
  160. GeoA::Vetor* GeoA::Vetor::random2D() {
  161. return GeoA::Vetor::fromAngle(GeoA::random() * GeoA::PI * 2);;
  162. }
  163. GeoA::Vetor* GeoA::Vetor::random3D() {
  164. double angle = GeoA::random() * GeoA::PI * 2;
  165. double vz = GeoA::random() * 2 - 1;
  166. double vx = sqrt(1 - vz * vz) * cos(angle);
  167. double vy = sqrt(1 - vz * vz) * sin(angle);
  168. return new GeoA::Vetor(vx, vy, vz);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement