geom.js 970 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function(fg){
  2. var p;
  3. var Rectangle = function(x,y,w,h){
  4. this.x = x;
  5. this.y = y;
  6. this.width = w;
  7. this.height = h;
  8. this.top = this.y;
  9. this.bottom = this.top + this.height;
  10. this.left = this.x;
  11. this.right = this.left + this.width;
  12. this.ratio = this.width/this.height;
  13. };
  14. Rectangle.prototype = p = {};
  15. p.resizeTo = function(w,h) {
  16. var ratio = w/h;
  17. if(ratio >= this.ratio) {
  18. this.width = w;
  19. this.height = this.width / this.ratio;
  20. } else {
  21. this.height = h;
  22. this.width = this.height * this.ratio;
  23. }
  24. this.x = -(this.width - w)/2;
  25. this.y = -(this.height -h)/2;
  26. };
  27. var Point = function(x,y) {
  28. this.x = x;
  29. this.y = y;
  30. };
  31. Point.prototype = p = {};
  32. p.clone = function(){
  33. return new Point(this.x, this.y);
  34. };
  35. Point.distance = function(pt1, pt2){
  36. return Math.sqrt(Math.pow(pt1.x - pt2.x, 2) + Math.pow(pt1.y - pt2.y, 2));
  37. };
  38. fg.geom = fg.geom || {};
  39. fg.geom.Rectangle = Rectangle;
  40. fg.geom.Point = Point;
  41. })(FandlrGame);