tools.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function heriter(destination, source) {
  2. function initClassIfNecessary(obj) {
  3. if( typeof obj["_super"] == "undefined" ) {
  4. obj["_super"] = function() {
  5. var methodName = arguments[0];
  6. var parameters = arguments[1];
  7. return this["__parent_methods"][methodName].apply(this, parameters);
  8. }
  9. }
  10. if( typeof obj["__parent_methods"] == "undefined" ) {
  11. obj["__parent_methods"] = {};
  12. }
  13. }
  14. for (var element in source) {
  15. if( typeof destination[element] != "undefined" ) {
  16. initClassIfNecessary(destination);
  17. destination["__parent_methods"][element] = source[element];
  18. } else {
  19. destination[element] = source[element];
  20. }
  21. }
  22. }
  23. function distance(point1, point2) {
  24. return Math.sqrt(Math.pow(point2.x - point1.x, 2) + Math.pow(point2.y - point1.y, 2));
  25. }
  26. function normalize(vector) {
  27. var norm = Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  28. if (norm == 0)
  29. return {
  30. x : 0,
  31. y : 0
  32. };
  33. var x = vector.x / norm;
  34. var y = vector.y / norm;
  35. return {
  36. x : x,
  37. y : y
  38. }
  39. }
  40. function angle(vector1, vector2) {
  41. if ((vector1.x == 0 && vector1.y == 0)
  42. && (vector1.x == 0 && vector1.y == 0)) {
  43. return 0;
  44. }
  45. if (vector1.x == 0 && vector1.y == 0) {
  46. return Math.acos(vector2.x
  47. / Math.sqrt(vector2.x * vector2.x + vector2.y * vector2.y));
  48. }
  49. if (vector2.x == 0 && vector2.y == 0) {
  50. return Math.acos(vector1.x
  51. / Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y));
  52. }
  53. var product = vector1.x * vector2.x + vector1.y * vector2.y,
  54. alpha = Math.acos(product
  55. / (Math.sqrt(vector1.x * vector1.x + vector1.y * vector1.y) * Math
  56. .sqrt(vector2.x * vector2.x + vector2.y * vector2.y))),
  57. sign = normalize(vector1).y > normalize(vector2).y ? -1 : 1;
  58. return sign * alpha;
  59. }