
require("topsin.genmath")
var genMath = new GenMath();
function featsToLimits(feats) {
    feats.forEach(function(item) {
        if(item.feats) {
            var polygon = genMath.profile2Polygon(item.feats.join("\n"),0)
            var res = polygon.reduce(function(a,b){
                a.xmin = b.x > a.xmin ? a.xmin : b.x;
                a.ymin = b.y > a.ymin ? a.ymin : b.y;
                a.xmax = b.x < a.xmax ? a.xmax : b.x;
                a.ymax = b.y < a.ymax ? a.ymax : b.y;
                return a
            }, {
                xmin: polygon[0].x,
                ymin: polygon[0].y,
                xmax: polygon[0].x,
                ymax: polygon[0].y
            })
            res.xsize = Math.abs(res.xmax - res.xmin) 
            res.ysize = Math.abs(res.ymax - res.ymin) 
            res.xc = res.xmin + res.xsize/2
			res.yc = res.ymin + res.ysize/2
            item.limits = res
        }
    })
}




function changeLine(line, length) {
    line.xs = Number(line.xs)
    line.ys = Number(line.ys)
    line.xe = Number(line.xe)
    line.ye = Number(line.ye)
    if (line.xe - line.xs == 0 || line.ye - line.ys == 0) {
        if (line.xe - line.xs == 0 ) {
            return {
                xs: line.xs,
                ys: line.ys + (line.ye - line.ys > 0 ? -length : length),
                xe: line.xe,
                ye: line.ye + (line.ye - line.ys > 0 ? length : -length)
            }
        } else {
            return {
                xs: line.xs - (line.xe - line.xs > 0 ? length : -length),
                ys: line.ys,
                xe: line.xe - (line.xe - line.xs > 0 ? -length : length),
                ye: line.ye
            }
        }
    }
    var width =  Math.sqrt( (line.xe - line.xs)*(line.xe - line.xs) + (line.ye - line.ys)*(line.ye - line.ys) )
    var k = length / width;
    var x = line.xe - line.xs;
    var y = line.ye - line.ys;
    return {
        xs: line.xs - k * x,
        ys: line.ys - k * y,
        xe: line.xe + k * x,
        ye: line.ye + k * y
    }
}