Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: RETRO_BallsAndPaddle RETRO_BallAndHoles
Vector.cpp
00001 #include "Vector.h" 00002 00003 Vector::Vector() 00004 { // constructor 00005 this->set((float)0,(float)0); 00006 } 00007 00008 Vector::Vector(float fX, float fY) 00009 { 00010 this->set(fX, fY); 00011 } 00012 00013 void Vector::set(float fX, float fY) 00014 { 00015 x=fX; 00016 y=fY; 00017 } 00018 void Vector::set(int nX, int nY) 00019 { 00020 this->set((float)nX, (float)nY); 00021 } 00022 00023 float Vector::getSize() 00024 { // get the size of the vector 00025 return(sqrt(x*x+y*y)); 00026 } 00027 00028 bool Vector::isLeft() { return(x<0); } 00029 bool Vector::isRight() { return(x>0); } 00030 bool Vector::isUp() { return(y<0); } 00031 bool Vector::isDown() { return(y>0); } 00032 00033 00034 void Vector::add(float fAdd) 00035 { // add the size of the vector by adding to its size 00036 float fSize=getSize(); 00037 float fSizeNew=fSize+fAdd; 00038 if(fSize!=0) 00039 { 00040 x=x * (fSizeNew/fSize); 00041 y=y * (fSizeNew/fSize); 00042 } 00043 else 00044 { // in case of zero lenght, default to addition in first quadrant. 00045 x=sqrt(0.5 * fAdd * fAdd); 00046 y=x; 00047 } 00048 } 00049 00050 void Vector::add(Vector vAdd) 00051 { 00052 x+=vAdd.x; 00053 y+=vAdd.y; 00054 } 00055 00056 void Vector::multiply(Vector vMult) 00057 { // multiply the vector by means of the other vector 00058 x*=vMult.x; 00059 y*=vMult.y; 00060 } 00061 00062 Vector Vector::getNormalized() 00063 { // get the Unit vector (= vector normalized to length 1) 00064 // see http://en.wikipedia.org/wiki/Unit_vector 00065 return(Vector(x/getSize(), y/getSize())); 00066 } 00067 00068 Vector Vector::getNormal() 00069 { // get the normal Vector 00070 // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector 00071 // vec.leftNormal --> vx = vec.vy; vy = -vec.vx; 00072 // vec.rightNormal --> vx = -vec.vy; vy = vec.vx; 00073 if(isLeft()) 00074 return(Vector(y, -1 * x)); 00075 else 00076 return(Vector(-1 * y, x)); 00077 } 00078 00079 void Vector::bounce(Vector vBounce) 00080 { // bounce a vector against another vector 00081 // see http://stackoverflow.com/questions/14885693/how-do-you-reflect-a-vector-over-another-vector 00082 Vector vBounceNormalized=vBounce.getNormalized(); 00083 Vector vBounceNormal=vBounce.getNormal(); 00084 Vector vBounceNormalNormalized=vBounceNormal.getNormalized(); 00085 00086 // 1. Find the dot product of vec1 and vec2 00087 // Note: dx and dy are vx and vy divided over the length of the vector (magnitude) 00088 float dpA = x * vBounceNormalized.x + y * vBounceNormalized.y; 00089 00090 // 2. Project vec1 over vec2 00091 float prA_vx = dpA * vBounceNormalized.x; 00092 float prA_vy = dpA * vBounceNormalized.y; 00093 00094 // 3. Find the dot product of vec1 and vec2's normal 00095 float dpB = x * vBounceNormalNormalized.x + y * vBounceNormalNormalized.y; 00096 00097 // 4. Project vec1 over vec2's left normal 00098 float prB_vx = dpB * vBounceNormalNormalized.x; 00099 float prB_vy = dpB * vBounceNormalNormalized.y; 00100 00101 // 5. Add the first projection prA to the reverse of the second -prB 00102 float new_vx = prA_vx - prB_vx; 00103 float new_vy = prA_vy - prB_vy; 00104 set(new_vx, new_vy); 00105 }
Generated on Thu Jul 14 2022 10:55:45 by
1.7.2