Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fscommand("fullscreen", "false");
- fscommand("allowscale", "false");
- fscommand("showmenu", "false");
- //create game object
- //dragging is true when the point is being dragged
- game = {dragging:false};
- //create first vector
- //point p0 is its starting point in the coordinates x/y
- //point p1 is its end point in the coordinates x/y
- game.v1 = {p0:{x:150, y:50}, p1:{x:150, y:100}};
- //create second vector
- //point p0 is its starting point in the coordinates x/y
- //point p1 is its end point in the coordinates x/y
- game.v2 = {p0:{x:100, y:150}, p1:{x:200, y:150}};
- //function to draw the points, lines and show text
- //this is only needed for the example to illustrate
- function drawAll(v)
- {
- _root.p0._x = v.p0.x;
- _root.p0._y = v.p0.y;
- _root.p1._x = v.p1.x;
- _root.p1._y = v.p1.y;
- _root.p2._x = game.v2.p0.x;
- _root.p2._y = game.v2.p0.y;
- _root.p3._x = game.v2.p1.x;
- _root.p3._y = game.v2.p1.y;
- _root.p4._x = game.ip.x;
- _root.p4._y = game.ip.y;
- _root.dragger0._x = v.p0.x;
- _root.dragger0._y = v.p0.y;
- _root.dragger1._x = v.p1.x;
- _root.dragger1._y = v.p1.y;
- _root.dragger2._x = game.v2.p0.x;
- _root.dragger2._y = game.v2.p0.y;
- _root.dragger3._x = game.v2.p1.x;
- _root.dragger3._y = game.v2.p1.y;
- _root.v1._x = v.p0.x;
- _root.v1._y = v.p0.y;
- _root.v2._x = game.v2.p0.x;
- _root.v2._y = game.v2.p0.y;
- _root.v3._x = v.p0.x;
- _root.v3._y = v.p0.y;
- _root.v1._xscale = v.vx;
- _root.v1._yscale = v.vy;
- _root.v2._xscale = game.v2.vx;
- _root.v2._yscale = game.v2.vy;
- _root.v3._xscale = game.v3.vx;
- _root.v3._yscale = game.v3.vy;
- _root.arrow1._x = v.p1.x;
- _root.arrow1._y = v.p1.y;
- _root.arrow1._rotation = 180/Math.PI*Math.atan2(v.vy, v.vx);
- _root.arrow2._x = game.v2.p1.x;
- _root.arrow2._y = game.v2.p1.y;
- _root.arrow2._rotation = 180/Math.PI*Math.atan2(game.v2.vy, game.v2.vx);
- _root.arrow3._x = game.v3.p1.x;
- _root.arrow3._y = game.v3.p1.y;
- _root.arrow3._rotation = 180/Math.PI*Math.atan2(game.v3.vy, game.v3.vx);
- }
- //main function
- function runMe()
- {
- //check if point is dragged
- if (game.dragging)
- {
- var v = game.v1;
- var x = Math.round(_root.dragger0._x);
- var y = Math.round(_root.dragger0._y);
- v.p0.x = x;
- v.p0.y = y;
- //get the coordinates from dargged mcs
- var x = Math.round(_root.dragger1._x);
- var y = Math.round(_root.dragger1._y);
- v.p1.x = x;
- v.p1.y = y;
- updateVector(v);
- var v = game.v2;
- var x = Math.round(_root.dragger2._x);
- var y = Math.round(_root.dragger2._y);
- v.p0.x = x;
- v.p0.y = y;
- var x = Math.round(_root.dragger3._x);
- var y = Math.round(_root.dragger3._y);
- v.p1.x = x;
- v.p1.y = y;
- updateVector(v);
- game.v3 = {};
- var v = game.v3;
- v.p0 = game.v1.p0;
- v.p1 = game.v2.p0;
- updateVector(v);
- findIntersection(game.v1, game.v2);
- drawAll(game.v1);
- }
- }
- //function to find all parameters for the vector
- function updateVector(v)
- {
- //x and y components
- //end point coordinate - start point coordinate
- v.vx = v.p1.x-v.p0.x;
- v.vy = v.p1.y-v.p0.y;
- //length of vector
- v.len = Math.sqrt(v.vx*v.vx+v.vy*v.vy);
- //normalized unti-sized components
- v.dx = v.vx/v.len;
- v.dy = v.vy/v.len;
- //right hand normal
- //v.rx = -v.vy;
- //v.ry = v.vx;
- //left hand normal
- //v.lx = v.vy;
- //v.ly = -v.vx;
- }
- //find intersection point of 2 vectors
- function findIntersection(v1, v2)
- {
- //vector between starting points
- var v3 = {vx:v2.p0.x-v1.p0.x, vy:v2.p0.y-v1.p0.y};
- //if they are parallel vectors, return big number
- if ((v1.dx == v2.dx and v1.dy == v2.dy) or (v1.dx == -v2.dx and v1.dy == -v2.dy))
- {
- var t = 1000000;
- }
- else
- {
- var t = perP(v3, v2)/perP(v1, v2);
- }
- //intersection point
- game.ip = {};
- game.ip.x = v1.p0.x+v1.vx*t;
- game.ip.y = v1.p0.y+v1.vy*t;
- }
- //calculate perp product of 2 vectors
- function perP(va, vb)
- {
- var pp = va.vx*vb.vy-va.vy*vb.vx;
- return pp;
- }
- //calculate all parameters for the vectors
- updateVector(game.v1);
- updateVector(game.v2);
- game.v3 = {};
- game.v3.p0 = game.v1.p0;
- game.v3.p1 = game.v2.p0;
- updateVector(game.v3);
- findIntersection(game.v1, game.v2);
- drawAll(game.v1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement