# Drawing api

i’m suppose to create a drawing api which consist of a line tool and a closed polygon tool. users are able to use this line tools to draw straight line between any 2 point. and the polygon tool is to create a shape irregardless of how many points as long as the first dot and the last dot meets, it will form a shape. is there a way to find the angles inside the polygon? is there also a way to find the point where the lines intersect and its angles?

1. yes, you can find the angle using Math.atan2(y,x) where y is the vertical distance between the two points in the line (side) and x is the horizontal. This gives you the angle from the horizontal so you may need to do some figuring to get the angles between 2 lines

2. line intersections is a little more complicated and takes a formula to calculate. You can find this formula online but I think I have a version somewhere around here…

``````// Actionscript:
lineIntersection = function (x1, y1, x2, y2, x3, y3, x4, y4, segment) {
var p={}, d1 = x2-x1, d2 = x4-x3;
if (!d1){
if (!d2) return false;
p._x = x1; p._y = y3+(y4-y3)*(x1-x3)/d2;
}else if (!d2){
p._x = x3; p._y = y1+(y2-y1)*(x3-x1)/d1;
}else{
var m1 = (y2-y1)/d1, m2 = (y4-y3)/d2;
if (m1 == m2) return false;
var c1 = y1-m1*x1; c2 = y3-m2*x3;
var d = m2-m1;
p._x = (c1-c2)/d; p._y = (m2*c1-m1*c2)/d;
}
if (segment){
if (d1>0){
if (p._x<x1 || p._x>x2) return false;
}else if (d1<0){
if (p._x>x1 || p._x<x2) return false;
}else if ((p._y<y1 && p._y<y2) || (p._y>y1 && p._y>y2)) return false;
if (d2>0){
if (p._x<x3 || p._x>x4) return false;
}else if (d2<0){
if (p._x>x3 || p._x<x4) return false;
}else if ((p._y<y3 && p._y<y4) || (p._y>y3 && p._y>y4)) return false;
}
return p;
}
``````

the x and y values represent your lines’ endpoints (first line is (x1,y1),(x2,y2) and second is (x3,y3),(x4,y4)) and the segment parameter is a boolean (true or false) which decides whether or not to use the line segments or the lines as infinitely expanding (true uses segments). If the lines do not intersect, false is returned, otherwise an object with two properties, _x and _y is returned that represents the point of instersection of the two lines.

thanks for ur fast reply. how do u dynamically get the x and y of the line when each line is a mc within another mc? (eg, _root.tool.line1) it work if i use trace(_root._xmouse) and trace(_root.ymouse) to get the x and y for each line.