Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. var Vec = function(x, y) {
  2. this.x = x || 0;
  3. this.y = y || 0;
  4. };
  5.  
  6. Vec.prototype = {
  7. set: function(v) {
  8. this.v.x = v.x;
  9. this.v.y = v.y;
  10.  
  11. return this;
  12. },
  13.  
  14. add: function(add) {
  15. if (add instanceof Vec) {
  16. return new Vec(this.v.x + add.x, this.v.y + add.y);
  17. } else {
  18. return new Vec(this.v.x + add, this.v.y + add);
  19. }
  20. }
  21. };
  22.  
  23. var MyVector = new Vector(32, 46);
  24.  
  25. MyVector.set(MyVector.add(new Vec(12, 7))); // MyVector = MyVector.add(new Vec(12, 7)); Same thing
  26.  
  27. MyVector.add(new Vec(12, 7)).set();
  28.  
  29. var Vec = function(a,b) {
  30. return {
  31. x: a,
  32. y: b,
  33.  
  34. set: function(vector) {
  35. this.x = vector.x;
  36. this.y = vector.y;
  37. },
  38.  
  39. get: function() {
  40. return {x: this.x, y:this.y};
  41. },
  42.  
  43. add: function(v) {
  44. if(s.constructor === new Vec().constructor) {
  45. values = v.get();
  46. this.x += values.x;
  47. this.y += values.y;
  48. }
  49. else {
  50. this.x += v;
  51. this.y += v;
  52. }
  53.  
  54. }
  55. }
  56. }
  57.  
  58. var v = new Vec(12,13);
  59. v.add(new Vect(14,15));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement