bool intersect(pt a, pt b, pt c, pt d) { return(intersect_1(a.x, b.x, c.x, d.x) && intersect_1(a.y, b.y, c.y, d.y) && area(a, b, c) * area(a, b, d) < 0 && area(c, d, a) * area(c, d, b) < 0); }
bool intersect(pt a, pt b, pt c, pt d) { return intersect_1(a.x, b.x, c.x, d.x) && intersect_1(a.y, b.y, c.y, d.y) && area(a, b, c) * area(a, b, d) < 0 && area(c, d, a) * area(c, d, b) < 0; }
int area(pt a, pt b, pt c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); }
private void триангуляцияДелонеToolStripMenuItem_Click(object sender, EventArgs e) { Tans = new int[maxn][]; for (int i = 0; i < maxn; ++i) Tans[i] = new int[3]; TMans = 0; nn = n; /////////////// int[][] D = new int[maxn * maxn][]; for (int i = 0; i < maxn * maxn; ++i) D[i] = new int[3]; int cntD = 0; for (int i = 0; i < nn; ++i) { for (int j = i + 1; j < nn; ++j) { for (int k = j + 1; k < nn; ++k) { int xi = points_arr[i].X; int xj = points_arr[j].X; int yi = points_arr[i].Y; int yj = points_arr[j].Y; int xk = points_arr[k].X; int yk = points_arr[k].Y; int UA = yi - yj;///y[i] - y[j]; int UB = xj - xi;/////x[j] - x[i]; int UC =-(UA* xi + UB * yi);//// -(UA * x[i] + UB * y[i]); if (UA * xk + UB * yk + UC == 0)////(UA * x[k] + UB * y[k] + UC == 0) continue; double A = Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); double B = Math.Sqrt((x[i] - x[k]) * (x[i] - x[k]) + (y[i] - y[k]) * (y[i] - y[k])); double C = Math.Sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j]) * (y[k] - y[j])); double p = (A + B + C) / 2.0; double S = Math.Sqrt(p * (p - A) * (p - B) * (p - C)); double[] RAB = new double[2]; RAB[0] = x[i] - x[j]; RAB[1] = y[i] - y[j]; double[] RAC = new double[2]; RAC[0] = x[i] - x[k]; RAC[1] = y[i] - y[k]; double[] RCB = new double[2]; RCB[0] = x[k] - x[j]; RCB[1] = y[k] - y[j]; double AA = (C * C * (RAB[0] * RAC[0] + RAB[1] * RAC[1])) / (8.0 * S * S); double AB = (B * B * (RAB[0] * RCB[0] + RAB[1] * RCB[1])) / (8.0 * S * S); double AC = (A * A * (-RAC[0] * RCB[0] - RAC[1] * RCB[1])) / (8.0 * S * S); double X = AA * x[i] + AB * x[j] + AC * x[k]; double Y = AA * y[i] + AB * y[j] + AC * y[k]; double R2 = (x[i] - X) * (x[i] - X) + (y[i] - Y) * (y[i] - Y); bool fnd = true; for (int t = 0; t < nn && fnd; ++t) { if (R2 > (x[t] - X) * (x[t] - X) + (y[t] - Y) * (y[t] - Y) + eps) { fnd = false; } } if (fnd) { for (int t = 0; t < cntD; ++t) { if (D[t][0] == i && D[t][1] == j && D[t][2] == k) { fnd = false; break; } } } if (fnd) { for (int t = 0; t < nn; ++t) { if (Math.Abs(R2 - (x[t] - X) * (x[t] - X) - (y[t] - Y) * (y[t] - Y)) < eps && t != i && t != j && t != k) { //нужно проверить на пересечения треугольники pt Pt1 = new pt(x[i], y[i]); pt Pt2 = new pt(x[j], y[j]); pt Pt3 = new pt(x[k], y[k]); pt Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = i; D[cntD][1] = k; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; D[cntD][0] = k; D[cntD][1] = j; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; } Pt1 = new pt(x[i], y[i]); Pt2 = new pt(x[k], y[k]); Pt3 = new pt(x[j], y[j]); Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = j; D[cntD][1] = k; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; D[cntD][0] = i; D[cntD][1] = j; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; } Pt1 = new pt(x[j], y[j]); Pt2 = new pt(x[k], y[k]); Pt3 = new pt(x[i], y[i]); Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = i; D[cntD][1] = k; D[cntD][2] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; D[cntD][0] = j; D[cntD][1] = i; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) for (int u = w + 1; u < 3; ++u) if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } ++cntD; } } } Tans[TMans][0] = i; Tans[TMans][1] = j; Tans[TMans][2] = k; ++TMans; } } } drawTriangle(); } }
private void триангуляцияДелонеToolStripMenuItem_Click(object sender, EventArgs e) { Tans = new int[maxn][]; for (int i = 0; i < maxn; ++i) { Tans[i] = new int[3]; } TMans = 0; nn = n; /////////////// int[][] D = new int[maxn * maxn][]; for (int i = 0; i < maxn * maxn; ++i) { D[i] = new int[3]; } int cntD = 0; for (int i = 0; i < nn; ++i) { for (int j = i + 1; j < nn; ++j) { for (int k = j + 1; k < nn; ++k) { int xi = points_arr[i].X; int xj = points_arr[j].X; int yi = points_arr[i].Y; int yj = points_arr[j].Y; int xk = points_arr[k].X; int yk = points_arr[k].Y; int UA = yi - yj; ///y[i] - y[j]; int UB = xj - xi; /////x[j] - x[i]; int UC = -(UA * xi + UB * yi); //// -(UA * x[i] + UB * y[i]); if (UA * xk + UB * yk + UC == 0) ////(UA * x[k] + UB * y[k] + UC == 0) { continue; } double A = Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); double B = Math.Sqrt((x[i] - x[k]) * (x[i] - x[k]) + (y[i] - y[k]) * (y[i] - y[k])); double C = Math.Sqrt((x[k] - x[j]) * (x[k] - x[j]) + (y[k] - y[j]) * (y[k] - y[j])); double p = (A + B + C) / 2.0; double S = Math.Sqrt(p * (p - A) * (p - B) * (p - C)); double[] RAB = new double[2]; RAB[0] = x[i] - x[j]; RAB[1] = y[i] - y[j]; double[] RAC = new double[2]; RAC[0] = x[i] - x[k]; RAC[1] = y[i] - y[k]; double[] RCB = new double[2]; RCB[0] = x[k] - x[j]; RCB[1] = y[k] - y[j]; double AA = (C * C * (RAB[0] * RAC[0] + RAB[1] * RAC[1])) / (8.0 * S * S); double AB = (B * B * (RAB[0] * RCB[0] + RAB[1] * RCB[1])) / (8.0 * S * S); double AC = (A * A * (-RAC[0] * RCB[0] - RAC[1] * RCB[1])) / (8.0 * S * S); double X = AA * x[i] + AB * x[j] + AC * x[k]; double Y = AA * y[i] + AB * y[j] + AC * y[k]; double R2 = (x[i] - X) * (x[i] - X) + (y[i] - Y) * (y[i] - Y); bool fnd = true; for (int t = 0; t < nn && fnd; ++t) { if (R2 > (x[t] - X) * (x[t] - X) + (y[t] - Y) * (y[t] - Y) + eps) { fnd = false; } } if (fnd) { for (int t = 0; t < cntD; ++t) { if (D[t][0] == i && D[t][1] == j && D[t][2] == k) { fnd = false; break; } } } if (fnd) { for (int t = 0; t < nn; ++t) { if (Math.Abs(R2 - (x[t] - X) * (x[t] - X) - (y[t] - Y) * (y[t] - Y)) < eps && t != i && t != j && t != k) { //нужно проверить на пересечения треугольники pt Pt1 = new pt(x[i], y[i]); pt Pt2 = new pt(x[j], y[j]); pt Pt3 = new pt(x[k], y[k]); pt Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = i; D[cntD][1] = k; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; D[cntD][0] = k; D[cntD][1] = j; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; } Pt1 = new pt(x[i], y[i]); Pt2 = new pt(x[k], y[k]); Pt3 = new pt(x[j], y[j]); Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = j; D[cntD][1] = k; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; D[cntD][0] = i; D[cntD][1] = j; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; } Pt1 = new pt(x[j], y[j]); Pt2 = new pt(x[k], y[k]); Pt3 = new pt(x[i], y[i]); Pt4 = new pt(x[t], y[t]); if (intersect(Pt1, Pt2, Pt3, Pt4)) { D[cntD][0] = i; D[cntD][1] = k; D[cntD][2] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; D[cntD][0] = j; D[cntD][1] = i; D[cntD][2] = t; //D[cntD][3] = t; for (int w = 0; w < 3; ++w) { for (int u = w + 1; u < 3; ++u) { if (D[cntD][w] > D[cntD][u]) { int tmp = D[cntD][w]; D[cntD][w] = D[cntD][u]; D[cntD][u] = tmp; } } } ++cntD; } } } Tans[TMans][0] = i; Tans[TMans][1] = j; Tans[TMans][2] = k; ++TMans; } } } drawTriangle(); } }
int area(pt a, pt b, pt c) { return((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)); }