flashMark
flashMark

Reputation: 767

How to know the points in a line given the starting and end points to traverse the line: any formula?

I'm using AS3. But it doesn't matter of what language am I using. I'm just finding logic on how to solve this problem.

I've done creating a line, drawing with graphics class given the start and end points of a line. Now I draw again a circle. I want that circle to traverse the line.

I can do that by knowing of what are the points between the start and end point of the line in the coordinate plane.

For example.. (0,0) and (20,20)

I can traverse the line by assigning values of an object by this...

object.x = 5;
object.y = 5;
object.x = 10;
object.y = 10;
and so on and so forth..

I want my object circle goes back and forth from start to end point then go back again to starting point while traversing the line.

Any formula you can help me with this? I have tried deducting and adding the slope to traverse the line, but it only works when a slope is positive.

Note: In flash, the coordinate system is different from the usual cartesian plane. The fourth Quadrant is all positive.

Here's my current logic:

var speed:int = 10;

function makeItMove(packetNumber:int):void{
        var xcenter:int = xCenterPt[packetNumber];
        var ycenter:int = yCenterPt[packetNumber];
        var xfirst:int = xFirstPt[packetNumber];
        var yfirst:int = yFirstPt[packetNumber];
        var xsecond:int = xSecondPt[packetNumber];
        var ysecond:int = ySecondPt[packetNumber];
        var switchPath:Boolean = switchPathArr[packetNumber];
        var slope:int = 0;

        if((xsecond-xfirst) != 0){
            slope = ((ysecond - yfirst)/(xsecond - xfirst)) * speed;
        }

        if(slope<0){
            //slope = ((ysecond - yfirst)/(xsecond - xfirst)) * speed;
            slope = slope * -1;
        }
        //else if(slope)

        //trace("PacketNumber: "+packetNumber+", xcenter: "+xcenter+", ycenter: "+ycenter+", xfirst: "+xfirst+", yfirst: "+yfirst+", xsecond: "+xsecond+", ysecond: "+ysecond)
        if(xcenter <= xfirst && ycenter <= yfirst){
            switchPath = true;
            switchPathArr[packetNumber] = switchPath;
        }

        if(xcenter >= xsecond && ycenter >= ysecond){
            switchPath = false;
            switchPathArr[packetNumber] = switchPath;
        }

        if(switchPath){
            xcenter = xcenter+slope;
            ycenter = ycenter+slope;
            xCenterPt[packetNumber] = xcenter;
            yCenterPt[packetNumber] = ycenter;
            //trace("UP: xcenter = " + xcenter + ", ycenter = " + ycenter);
            this[mc_Packets[packetNumber]].x = xcenter;
            this[mc_Packets[packetNumber]].y = ycenter;
        }
        else{
            xcenter = xcenter-slope;
            ycenter = ycenter-slope;
            xCenterPt[packetNumber] = xcenter;
            yCenterPt[packetNumber] = ycenter;
            //trace("DOWN: xcenter = " + xcenter + ", ycenter = " + ycenter);
            this[mc_Packets[packetNumber]].x = xcenter;
            this[mc_Packets[packetNumber]].y = ycenter;
        }
    }

Upvotes: 1

Views: 673

Answers (2)

Larusso
Larusso

Reputation: 1151

I would use an vector. With this you can create a direction vector out of two position vectors. Means subtract two vectors. VectorA startpoint and VectorB end point.

Then you change the length value and the vetor x and y coordinates will change accordingly.

check this class as an example:

http://code.google.com/p/as3ufw/source/browse/trunk/src/core/as3ufw/geom/Vector2D.as?r=93

this is also helpfull: http://emweb.unl.edu/math/mathweb/vectors/vectors.html

read this first before you try the class :)

Upvotes: 1

flashMark
flashMark

Reputation: 767

It is better to use tweenLike API than to solve it mathematically.

Here's a link:

http://www.greensock.com/tweenlite/

Upvotes: 0

Related Questions