Skip to main content
markerline
Inspiring
March 13, 2015
Answered

Normals to a drawn line at runtime (incorrect math)?

  • March 13, 2015
  • 1 reply
  • 2035 views

Hello.

I have an equation which seemingly gives back Normals (line segments) to a line drawn with ActionScript and the mouse.

If I go from lesser mouseX position to greater mouseX position it seems to work fine.

line1Arr.rotation=45*Math.atan2(mouseYArr-mouseYArr[i-1], mouseXArr-mouseXArr[i-1]);

line1Arr is the instance of the line segment Normal that is being attached to the line and mouseXArr and mouseXArr[i-1] is the most recent point and the one before it.

The problem is that after having done a web search I think my math is not correct to calculate the normal.  I have found a thread somewhere which mentions to use derivative of x and derivative of y to calculate the normals.  Another member in the thread posted that one can also use sine and cosine to calculate this.

This is the problematic equation that I tried when going mouseX in the opposite direction

line1Arr.rotation=(-45)*Math.atan2(mouseXArr[i-1]-mouseXArr[i-0], mouseYArr[i-1]-mouseYArr[i-0]);

Any help with this math problem is greatly appreciated.

This topic has been closed for replies.
Correct answer kglad

because you're using a series of lines, there's no need to use trig: basic cartesian geometry is sufficient.

for example, if you have an array of [x,y] points used in your drawing, calling normalsF will add perpendiculars to the midpoint of those segments.  (for an example, http://www.kglad.com/Files/forums/normals.fla)

function normalsF():void{

    for(var i=0;i<curveA.length-1;i++){

        normalF(curveA,curveA[i+1]);

    }

}

function normalF(p1:Array,p2:Array):void{

    m = -(p1[0]-p2[0])/(p1[1]-p2[1]);

    b = (p1[1]+p2[1])/2 - m*(p1[0]+p2[0])/2;

    drawnormalF(m,b,[(p1[0]+p2[0])/2, (p1[1]+p2[1])/2]);

}

function drawnormalF(m:Number,b:Number,pt:Array):void{

    len = Math.sqrt(400/(4+4*m*m));

    with(curveParent.graphics){

        moveTo(pt[0]-len,m*(pt[0]-len)+b);

        lineTo(pt[0]+len,m*(pt[0]+len)+b);

    }

}

1 reply

kglad
Community Expert
Community Expert
March 13, 2015

are you trying to find a normal to a line or a curve?

do you have the equation for the line or curve?  if yes, is that a standardized (eg, y=..) or parametric or something else equation?

markerline
Inspiring
March 13, 2015

Hi and thanks for looking at my question.  I am trying to find the normal to a line segment which is created by an array of points, which themselves are the mouseX and mouseY positions of someone drawing a curve on the canvas or stage.  I don't have an equation I just have an array of points.  The most recent point and the one just before it (i-1) are being analyzed with the arctan2 method.  I think I am not using the correct method though.  I have decided to use atan2 because I know in another application this will return a rotational information based on provided x and y cartesian coordinates.