Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. fscommand("fullscreen", "false");
  2. fscommand("allowscale", "false");
  3. fscommand("showmenu", "false");
  4. //create game object
  5. //dragging is true when the point is being dragged
  6. game = {dragging:false};
  7. //create first vector
  8. //point p0 is its starting point in the coordinates x/y
  9. //point p1 is its end point in the coordinates x/y
  10. game.v1 = {p0:{x:150, y:50}, p1:{x:150, y:100}};
  11. //create second vector
  12. //point p0 is its starting point in the coordinates x/y
  13. //point p1 is its end point in the coordinates x/y
  14. game.v2 = {p0:{x:100, y:150}, p1:{x:200, y:150}};
  15. //function to draw the points, lines and show text
  16. //this is only needed for the example to illustrate
  17. function drawAll(v)
  18. {
  19. _root.p0._x = v.p0.x;
  20. _root.p0._y = v.p0.y;
  21. _root.p1._x = v.p1.x;
  22. _root.p1._y = v.p1.y;
  23. _root.p2._x = game.v2.p0.x;
  24. _root.p2._y = game.v2.p0.y;
  25. _root.p3._x = game.v2.p1.x;
  26. _root.p3._y = game.v2.p1.y;
  27. _root.p4._x = game.ip.x;
  28. _root.p4._y = game.ip.y;
  29. _root.dragger0._x = v.p0.x;
  30. _root.dragger0._y = v.p0.y;
  31. _root.dragger1._x = v.p1.x;
  32. _root.dragger1._y = v.p1.y;
  33. _root.dragger2._x = game.v2.p0.x;
  34. _root.dragger2._y = game.v2.p0.y;
  35. _root.dragger3._x = game.v2.p1.x;
  36. _root.dragger3._y = game.v2.p1.y;
  37. _root.v1._x = v.p0.x;
  38. _root.v1._y = v.p0.y;
  39. _root.v2._x = game.v2.p0.x;
  40. _root.v2._y = game.v2.p0.y;
  41. _root.v3._x = v.p0.x;
  42. _root.v3._y = v.p0.y;
  43. _root.v1._xscale = v.vx;
  44. _root.v1._yscale = v.vy;
  45. _root.v2._xscale = game.v2.vx;
  46. _root.v2._yscale = game.v2.vy;
  47. _root.v3._xscale = game.v3.vx;
  48. _root.v3._yscale = game.v3.vy;
  49. _root.arrow1._x = v.p1.x;
  50. _root.arrow1._y = v.p1.y;
  51. _root.arrow1._rotation = 180/Math.PI*Math.atan2(v.vy, v.vx);
  52. _root.arrow2._x = game.v2.p1.x;
  53. _root.arrow2._y = game.v2.p1.y;
  54. _root.arrow2._rotation = 180/Math.PI*Math.atan2(game.v2.vy, game.v2.vx);
  55. _root.arrow3._x = game.v3.p1.x;
  56. _root.arrow3._y = game.v3.p1.y;
  57. _root.arrow3._rotation = 180/Math.PI*Math.atan2(game.v3.vy, game.v3.vx);
  58. }
  59. //main function
  60. function runMe()
  61. {
  62. //check if point is dragged
  63. if (game.dragging)
  64. {
  65. var v = game.v1;
  66. var x = Math.round(_root.dragger0._x);
  67. var y = Math.round(_root.dragger0._y);
  68. v.p0.x = x;
  69. v.p0.y = y;
  70. //get the coordinates from dargged mcs
  71. var x = Math.round(_root.dragger1._x);
  72. var y = Math.round(_root.dragger1._y);
  73. v.p1.x = x;
  74. v.p1.y = y;
  75. updateVector(v);
  76. var v = game.v2;
  77. var x = Math.round(_root.dragger2._x);
  78. var y = Math.round(_root.dragger2._y);
  79. v.p0.x = x;
  80. v.p0.y = y;
  81. var x = Math.round(_root.dragger3._x);
  82. var y = Math.round(_root.dragger3._y);
  83. v.p1.x = x;
  84. v.p1.y = y;
  85. updateVector(v);
  86. game.v3 = {};
  87. var v = game.v3;
  88. v.p0 = game.v1.p0;
  89. v.p1 = game.v2.p0;
  90. updateVector(v);
  91. findIntersection(game.v1, game.v2);
  92. drawAll(game.v1);
  93. }
  94. }
  95. //function to find all parameters for the vector
  96. function updateVector(v)
  97. {
  98. //x and y components
  99. //end point coordinate - start point coordinate
  100. v.vx = v.p1.x-v.p0.x;
  101. v.vy = v.p1.y-v.p0.y;
  102. //length of vector
  103. v.len = Math.sqrt(v.vx*v.vx+v.vy*v.vy);
  104. //normalized unti-sized components
  105. v.dx = v.vx/v.len;
  106. v.dy = v.vy/v.len;
  107. //right hand normal
  108. //v.rx = -v.vy;
  109. //v.ry = v.vx;
  110. //left hand normal
  111. //v.lx = v.vy;
  112. //v.ly = -v.vx;
  113. }
  114. //find intersection point of 2 vectors
  115. function findIntersection(v1, v2)
  116. {
  117. //vector between starting points
  118. var v3 = {vx:v2.p0.x-v1.p0.x, vy:v2.p0.y-v1.p0.y};
  119. //if they are parallel vectors, return big number
  120. if ((v1.dx == v2.dx and v1.dy == v2.dy) or (v1.dx == -v2.dx and v1.dy == -v2.dy))
  121. {
  122. var t = 1000000;
  123. }
  124. else
  125. {
  126. var t = perP(v3, v2)/perP(v1, v2);
  127. }
  128. //intersection point
  129. game.ip = {};
  130. game.ip.x = v1.p0.x+v1.vx*t;
  131. game.ip.y = v1.p0.y+v1.vy*t;
  132. }
  133. //calculate perp product of 2 vectors
  134. function perP(va, vb)
  135. {
  136. var pp = va.vx*vb.vy-va.vy*vb.vx;
  137. return pp;
  138. }
  139. //calculate all parameters for the vectors
  140. updateVector(game.v1);
  141. updateVector(game.v2);
  142. game.v3 = {};
  143. game.v3.p0 = game.v1.p0;
  144. game.v3.p1 = game.v2.p0;
  145. updateVector(game.v3);
  146. findIntersection(game.v1, game.v2);
  147. drawAll(game.v1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement