Advertisement
Rapido

V2_class

Dec 12th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <bits/stdc++.h> //All main STD libraries
  2.  
  3. using namespace std;
  4.  
  5. // Point or Vector 2D class (by Imanol SAGA)
  6. class V2
  7. {
  8. public:
  9.     float x,y;
  10.    
  11.     V2(){ x=0.1; y=0.1; }
  12.     V2(int _x, int _y){ x=_x; y=_y; }
  13.     V2(V2 *v){ x=v->x; y=v->y; }
  14.     V2(int range){ float rd=range*0.5; x=(-rd)+(rand()%range); y=(-rd)+(rand()%range); }
  15.  
  16.     void set(V2 &v){ x=v.x; y=v.y; }
  17.     inline void never0(){ x=x!=0?x:0.1; y=y!=0?y:0.1; }
  18.    
  19.     inline void add(V2 &v){ x+=v.x; y+=v.y; }
  20.     inline void subs(V2 &v){ x-=v.x; y-=v.y; }
  21.     inline void mult(float n){ x*=n; y*=n; }
  22.     inline void div(float n){ never0(); n=n!=0?n:0.1; x/=n; y/=n; }
  23.     inline float mag2(){ return((x*x)+(y*y)); }
  24.     inline float mag(){ return(sqrt(mag2())); }
  25.     inline void norm(){ div(mag()); }
  26.     inline void setMag(float m){ norm(); mult(m); }
  27.     inline float dist2(V2 &p){ return(pow(x-p.x,2)+pow(y-p.y,2)); }
  28.     inline float dist(V2 &p){ return(sqrt(dist2(p))); }
  29. };
  30.  
  31. int main(){ cout<< "You are welcome"<< endl; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement