15827// TRIANGLE  

zim.Triangle = function(a, b, c, color, borderColor, borderWidth, corner, center, adjust, dashed, strokeObj, style, group, inherit) {
    var sig = "a, b, c, color, borderColor, borderWidth, corner, center, adjust, dashed, strokeObj, style, group, inherit";
    var duo; if (duo = zob(zim.Triangle, arguments, sig, this)) return duo;
    this.zimCustomShape_constructor(null,null,null,null,false);
    this.type = "Triangle";
    this.group = group;
    var DS = style===false?{}:zim.getStyle(this.type, this.group, inherit);

    if (zot(a)) a = DS.a!=null?DS.a:100;
    if (zot(b)) b = DS.b!=null?DS.b:a;
    if (zot(c)) c = DS.c!=null?DS.c:a;
    if (b==-1) b = Math.sqrt(Math.pow(a,2)+Math.pow(c,2));
    if (c==-1) c = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
    if (zot(corner)) corner = DS.corner!=null?DS.corner:0;
    if (zot(center)) center = DS.center!=null?DS.center:true;
    if (zot(adjust)) adjust = DS.adjust!=null?DS.adjust:0;
    if (zot(borderColor)) borderColor = DS.borderColor!=null?DS.borderColor:null;
    if (zot(borderWidth)) borderWidth = DS.borderWidth!=null?DS.borderWidth:null;
    if (borderColor < 0 || borderWidth < 0) borderColor = borderWidth = null;
    else if (borderColor!=null && borderWidth==null) borderWidth = 1;
    if (zot(color)) color = DS.color!=null?DS.color:(borderWidth>0?"rgba(0,0,0,0)":zim.black);
    if (zot(adjust)) adjust = DS.adjust!=null?DS.adjust:0;
    if (zot(strokeObj)) strokeObj = DS.strokeObj!=null?DS.strokeObj:{};

    // PICK
    var oa = remember(a, b, c, color, borderColor, borderWidth);
    this.veeObj = {a:oa[0], b:oa[1], c:oa[2], color:oa[3], borderColor:oa[4], borderWidth:oa[5]};
    function remember() {return arguments;} // for cloning PICK
    a = zim.Pick.choose(a);
    b = zim.Pick.choose(b);
    c = zim.Pick.choose(c);
    this.a = a;
    this.b = b;
    this.c = c;
    color = zim.Pick.choose(color);
    borderColor = zim.Pick.choose(borderColor);
    borderWidth = zim.Pick.choose(borderWidth);

    var that = this;
    that._color = color;
    that._borderColor = borderColor;
    that._borderWidth = borderWidth;
    that._dashed = dashed;
    if (that._dashed && !Array.isArray(that._dashed)) that._dashed = [10, 10];
    that._corner = corner;


    var lines = [a,b,c];
    lines.sort(function(a, b){return b-a;});
    var aa = lines[0];
    var bb = lines[1];
    var cc = lines[2];
    var order = [lines.indexOf(a), lines.indexOf(b), lines.indexOf(c)];

    if (aa > bb+cc) {
        zogy("zim display - Triangle(): invalid triangle lengths");
        return;
    }

    var tri = this.shape = new createjs.Shape();
    this.adjusted = adjust;
    this.addChild(tri);

    var g = tri.graphics;
    that.drawShape = function() {
        var corners = [];		
        if (Array.isArray(that._corner)) corners = that._corner;
        else corners.push(that._corner, that._corner, that._corner);
        for(var i=0; i<3; i++) {
            if (corners[i] < 0 || typeof corners[i] != "number") corners[i] = 0;
        }
	
        g.c();
        that.colorCommand = g.f(that._color).command;
        // if (that._color && that._color.type) that.specialColor(that.colorCommand, that._color, that);
        // border of 0 or a string value still draws a border in CreateJS
        if (zot(that._borderWidth) || that._borderWidth > 0) { // no border specified or a border > 0
            if (!zot(that._borderColor) || !zot(that._borderWidth)) { // either a border color or thickness
                if (zot(that._borderColor)) that._borderColor = zim.black;
                that.borderColorCommand = g.s(that._borderColor).command;
                if (that._borderColor && that._borderColor.type) that.specialColor(that.borderColorCommand, that._borderColor, that);
                that.borderWidthCommand = g.ss(that._borderWidth, strokeObj.caps, strokeObj.joints, strokeObj.miterLimit, strokeObj.ignoreScale).command;
                if (that._dashed) that.borderDashedCommand = g.sd(Array.isArray(that._dashed)?that._dashed:[10, 10], that._dashedOffset).command;
            }
        }
	       
        that.one={x:0,y:0};        
        that.two={x:a,y:0};

        // find biggest angle with cosine rule
        var angle1 = Math.acos( (Math.pow(bb,2) + Math.pow(cc,2) - Math.pow(aa,2)) / (2 * bb * cc) ) * 180 / Math.PI;

        // use the sine rule for next biggest angle
        var angle2 = Math.asin( bb * Math.sin(angle1 * Math.PI / 180) / aa ) * 180 / Math.PI;

        // find last angle
        var angle3 = 180 - angle1 - angle2;

        // get position of angles by mapping to opposite side sizes
        // as in smallest angle is across from smallest side
        // largest angle is across from largest size, etc.
        var temp = [angle1, angle2, angle3]; // largets to smallest
        that.angles = [temp[order[1]], temp[order[2]], temp[order[0]]];

        var nextAngle = that.angles[1];
        var backX = Math.cos(nextAngle * Math.PI / 180) * b;
        var upY = Math.sin(nextAngle * Math.PI / 180) * b;

        var width = Math.max(a, a-backX);
        var height = upY;
        that.setBounds(0,adjust,width,height);
        tri.y = height;
		
        that.three={x:a-backX,y:0-upY};
			
        var sX = a*corners[0]/((corners[0]+corners[1])||1);
        g
            .mt(sX,0)
            .at(a,0, a-backX,0-upY, corners[1])
            .at(a-backX,0-upY, 0,0, corners[2])
            .at(0,0, a,0, corners[0])
            .lt(sX,0);
		
	g.cp();

        if (center) {
            that.regX = width/2;
            that.regY = height/2;
        }
        if (adjust) {
            that.shape.y+=adjust;
        }
	if (that._color.type) that.color = that._color;
    };
that.drawShape();


    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // NOTE: extends ZIM CustomShape for more properties and a few functions.
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (style!==false) zim.styleTransforms(this, DS);
    this.clone = function(exact, useStyle) {
	var cloneStyle = zot(useStyle)?style:useStyle;
	if (exact) cloneStyle = false; 
        var newShape = that.cloneProps(new zim.Triangle((exact||!zim.isPick(oa[0]))?a:oa[0], (exact||!zim.isPick(oa[1]))?b:oa[1], (exact||!zim.isPick(oa[2]))?c:oa[2], (exact||!zim.isPick(oa[3]))?that.color:oa[3], (exact||!zim.isPick(oa[4]))?that.borderColor:oa[4], (exact||!zim.isPick(oa[5]))?that.borderWidth:oa[5], corner, center, adjust, dashed, strokeObj, cloneStyle, this.group, inherit));
        if (that.linearGradientParams) newShape.linearGradient.apply(newShape, that.linearGradientParams);
        if (that.radialGradientParams) newShape.radialGradient.apply(newShape, that.radialGradientParams);
        return newShape;
    };
};
zim.extend(zim.Triangle, zim.CustomShape, "clone", "zimCustomShape");