// 画点封装
function createdot(x, y, c) {
    c.beginPath()
    c.arc(x, y, 2, 0, 360)
    c.closePath()
    c.fillStyle = "red"
    c.fill()
}
// 连线
function creatdTriangle(trgle, c) {
    c.strokeStyle = '#33FF00' //设置线条颜色
    c.beginPath()
    if (trgle.length == 3) {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线
    } else if (trgle.length == 4) {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线
    } else if (trgle.length == 5) {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线
    } else if (trgle.length == 6) {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线
    } else if (trgle.length == 7) {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线
    } else {
        c.moveTo(trgle[0].x, trgle[0].y) //起点
        for (var q = 1; q < trgle.length; q++) {
            c.lineTo(trgle[q].x, trgle[q].y)
        }
        c.closePath()
        c.stroke() //开始绘制直线

    }
}
//多边形主方法
function createdPitch(trgle, ids) {
    newPointList = trgle
    var c = document.getElementById(ids);
    var ctx = c.getContext("2d");
    c.width = c.width
    for (var i = 0; i < trgle.length; i++) {
        createdot(trgle[i].x, trgle[i].y, ctx)
    }
    creatdTriangle(trgle, ctx)
    var tx, ty, stx, sty, oldx, oldy
    c.onmousedown = function(event) {
        var rect = c.getBoundingClientRect();
        tx = event.clientX - rect.left
        ty = event.clientY - rect.top
        oldx = tx
        oldy = ty
        var qe = chooseDot(tx, ty, trgle)
        if (qe || qe == 0) {
            if (tx - trgle[qe].x <= 2 && tx - trgle[qe].x >= -2) {
                stx = true
            } else {
                stx = !stx
            }
            if (ty - trgle[qe].y <= 2 && ty - trgle[qe].y >= -2) {
                sty = true
            } else {
                sty = !stx
            }
            if (stx == true && sty == true) {
                c.onmousemove = function(event) {
                    var rect = c.getBoundingClientRect();
                    c.width = c.width
                    trgle[qe].x = event.clientX - rect.left
                    trgle[qe].y = event.clientY - rect.top
                    for (var l = 0; l < trgle.length; l++) {
                        createdot(trgle[l].x, trgle[l].y, ctx)
                    }
                    creatdTriangle(trgle, ctx)

                }
                c.onmouseup = function(event) {
                    var b = chooseArr(trgle, qe)
                    var pp = allLineche(b)
                    var statusd = chooseINline(trgle[qe].x, trgle[qe].y, pp) //是否在线上
                    var km = null
                    if (trgle.length > 3) {
                        if (qe == 0) {
                            var kt = false
                            for (var i = 1; i < trgle.length - 1; i++) {
                                if (qe == i || qe + 1 == i) {
                                    kt = LineSegmentsIntersect(trgle[qe], trgle[trgle.length - 1], trgle[i], trgle[i + 1])
                                    continue
                                } else {
                                    if (i == trgle.length - 1) {
                                        var st = LineSegmentsIntersect(trgle[qe], trgle[qe + 1], trgle[trgle.length - 1], trgle[0])
                                    } else {
                                        var st = LineSegmentsIntersect(trgle[qe], trgle[qe + 1], trgle[i], trgle[i + 1])
                                    }
                                }
                                if (st || kt) {
                                    km = true
                                    break
                                }
                            }
                        } else if (qe == trgle.length - 1) {
                            var st = false
                            for (var t = 0; t < trgle.length - 2; t++) {
                                if (qe == t || qe - 2 == t) {
                                    kt = LineSegmentsIntersect(trgle[qe], trgle[0], trgle[t], trgle[t + 1])
                                    if (st || kt) {
                                        km = true
                                    }
                                    continue
                                } else {
                                    st = LineSegmentsIntersect(trgle[qe], trgle[qe - 1], trgle[t], trgle[t + 1])
                                }
                                if (st || kt) {
                                    km = true
                                    break
                                }
                            }

                        } else {
                            for (var t = qe; t < trgle.length + qe - 1; t++) {
                                if (qe + 1 == t + 1) {
                                    if (t + 2 >= trgle.length) {
                                        kt = LineSegmentsIntersect(trgle[qe], trgle[qe - 1], trgle[t + 1], trgle[t + 2 - trgle.length])
                                    } else {
                                        kt = LineSegmentsIntersect(trgle[qe], trgle[qe - 1], trgle[t + 1], trgle[t + 2])
                                    }
                                    if (st || kt) {
                                        km = true
                                        break
                                    } else {
                                        continue

                                    }
                                } else {
                                    if (t + 2 == trgle.length) {
                                        var st = LineSegmentsIntersect(trgle[qe], trgle[qe + 1], trgle[t + 1], trgle[t + 2 - trgle.length])
                                    } else if (t + 1 == trgle.length) {
                                        continue
                                    } else if (t + 2 > trgle.length) {
                                        var st = LineSegmentsIntersect(trgle[qe], trgle[qe + 1], trgle[t - trgle.length], trgle[t + 1 - trgle.length])
                                    } else {
                                        var st = LineSegmentsIntersect(trgle[qe], trgle[qe + 1], trgle[t + 1], trgle[t + 2])
                                    }
                                }
                                if (st || kt) {
                                    km = true
                                    break
                                }
                            }

                        }

                    }
                    if (km || statusd) {
                        c.onmousemove = null;
                        c.onmouseup = null;
                        c.width = c.width
                        trgle[qe].x = oldx
                        trgle[qe].y = oldy
                        for (var z = 0; z < trgle.length; z++) {
                            createdot(trgle[z].x, trgle[z].y, ctx)
                        }
                        newPointList = trgle
                        creatdTriangle(trgle, ctx)

                    } else {
                        newPointList = trgle
                        c.onmousemove = null;
                        c.onmouseup = null;
                    }
                };

            }
        }
    }
}
// 判断是哪个点
function chooseDot(a, b, trgle) {
    for (var i = 0; i < trgle.length; i++) {
        var o = trgle[i].x - a
        var p = trgle[i].y - b
        if (o >= -2 && o <= 2 && p >= -2 && p <= 2) {
            return i
        }
    }
}
// 判断点是否在线段上
function chooseINline(a, b, trgle) {
    for (var i = 0; i < trgle.length; i++) {
        if ((a - trgle[i].a.x) * (trgle[i].b.y - trgle[i].a.y) == (trgle[i].b.x - trgle[i].a.x) * (b - trgle[i].a.y)) {
            return true
        }
    }
}
// 重组数组
function chooseArr(d, qe) {
    var newArr = []
    for (var i = 0; i < d.length; i++) {
        if (i == qe) {
            continue
        } else {
            newArr.push(d[i])
        }
    }
    return newArr
}
// 组合线段数组
function allLineche(e) {
    var pArr = []
    for (var i = 0; i < e.length; i++) {
        for (var j = i + 1; j < e.length; j++) {
            pArr.push({
                a: e[i],
                b: e[j]
            })
        }
    }
    return pArr
}

function LineSegmentsIntersect(lh, lt, th, tt) {
    if (!(Math.min(lh.x, lt.x) <= Math.max(th.x, tt.x) && Math.min(th.y, tt.y) <= Math.max(lh.y, lt.y) && Math.min(th.x, tt.x) <= Math.max(lh.x, lt.x) && Math.min(lh.y, lt.y) <= Math.max(th.y, tt.y))) {
        return false;
    }
    if ((((lh.x - th.x) * (tt.y - th.y) - (lh.y - th.y) * (tt.x - th.x)) *
            ((lt.x - th.x) * (tt.y - th.y) - (lt.y - th.y) * (tt.x - th.x))) > 0 ||
        (((th.x - lh.x) * (lt.y - lh.y) - (th.y - lh.y) * (lt.x - lh.x)) *
            ((tt.x - lh.x) * (lt.y - lh.y) - (tt.y - lh.y) * (lt.x - lh.x))) > 0) {
        return false;
    } else {
        return true;
    }
}
// 缩放保持100%大小方法
function ScalePitch(data, mjp) {
    var ppi = window.innerWidth;
    if (ppi < 1920) { 
        var bls = getRatio();
        if (bls > 100) {
            var newHs = (420 * 100 / bls) * 0.75;
            var newWs = (560 * 100 / bls) * 0.75;
        } else {
            var newHs = 420 * 100 / bls;
            var newWs = 560 * 100 / bls;
        }
        var divds = document.getElementById(data);
        if (mjp) {
            var myCanvas = document.getElementById(mjp);
        } else {
            var myCanvas = document.getElementById("myCanvas");
        }
        if (divds && myCanvas) {
            divds.style.height = newHs + "px";
            divds.style.width = newWs + "px";
            myCanvas.height = newHs
            myCanvas.width = newWs
        }
    }
}
// 缩放图片
function ScalePitchImg(data) {
    var ppi = window.innerWidth;
    if (ppi < 1920) { 
        var bls = getRatio();
        if (bls > 100) {
            var newHs = (36 * 100 / bls) * 0.75;
            var newWs = (72 * 100 / bls) * 0.75;
        } else {
            var newHs = 36 * 100 / bls;
            var newWs = 72 * 100 / bls;
        }
        var divds = document.getElementsByClassName(data);
        for (var i = 0; i < divds.length; i++) {
            var div = divds[i];
            if (div) {
                div.style.height = newHs + "px";
                div.style.width = newWs + "px";
            }
        }
    }
}