Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool triangleExists (double ko[][3][2], int index);
- void findCollisions (int tr1i, int tr1m, int tr2i, int tr2m, double ko[][3][2], double r[6][2], int &c, bool &inf);
- const int TR = 2;
- int main()
- {
- double ko[TR][3][2]; // Masīvs, kas saturēs dotās trijstūru koordinātas
- int ok; // Beigt vai turpināt programmu
- double r[6][2];
- int c = 0;
- bool inf = false;
- // Veicam darbību, kamēr lietotājs vēlas to turpināt
- do {
- c = 0;
- inf = false;
- for (int i = 0; i < TR; i++) {
- do {
- for (int j = 0; j < 3; j++) {
- cout << "Lūdzu ievadiet " << i + 1 << ". trijstūra " << j + 1 << ". virsotnes x koordinātu: ";
- cin >> ko[i][j][0];
- cout << "Lūdzu ievadiet " << i + 1 << ". trijstūra " << j + 1 << ". virsotnes y koordinātu: ";
- cin >> ko[i][j][1];
- }
- if (!triangleExists(ko, i))
- cout << "Šāds trijstūris neeksistē! Lūdzu ievadiet jaunas koordinātas!" << endl;
- } while (!triangleExists(ko, i));
- }
- for (int i = 0; i < TR; i++) {
- for (int j = i + 1; j < TR; j++) {
- for (int k = 0; k < 3; k++) {
- for (int l = 0; l < 3; l++) {
- findCollisions(i, k, j, l, ko, r, c, inf);
- }
- }
- }
- }
- if (inf == true) {
- cout << "Trijstūru kontūrām ir neierobežoti daudz kopīgi punkti (kāda mala pārklājas)!" << endl;
- } else {
- if (c > 0) {
- for (int i = 0; i < c; i++) {
- cout << "Trijstūru kontūras krustojas punktā (" << r[i][0] << ", " << r[i][1] << ")" << endl;
- }
- } else {
- cout << "Trijstūriem nav kopīgu punktu!" << endl;
- }
- }
- cout << "Turpināt (1) vai beigt (0) programmas izpildi?" << endl; // Piedāvajam lietotājam turpināt programmu
- cin >> ok;
- } while (ok == 1);
- return 0;
- }
- double max (double c1, double c2) {
- if (c1 > c2)
- return c1;
- else
- return c2;
- }
- double min (double c1, double c2) {
- if (c1 > c2)
- return c2;
- else
- return c1;
- }
- int nextLine(int curLine)
- {
- if (curLine == 0)
- return 1;
- else if (curLine == 1)
- return 2;
- else if (curLine == 2)
- return 0;
- else
- return -1;
- }
- bool triangleExists(double ko[][3][2], int index)
- {
- if (ko[index][0][0] == ko[index][1][0] && ko[index][1][0] == ko[index][2][0] || ko[index][0][1] == ko[index][1][1] && ko[index][1][1] == ko[index][2][1])
- return false;
- else
- return true;
- }
- void findCollisions (int tr1i, int tr1m, int tr2i, int tr2m, double ko[][3][2], double r[6][2], int &c, bool &inf)
- {
- int tr1m2 = nextLine(tr1m), tr2m2 = nextLine(tr2m);
- if (tr1m2 == -1 || tr2m2 == -1) {
- cout << "Norādīta neeksistējoša trijstūra mala!";
- }
- double x1 = ko[tr1i][tr1m][0], x2 = ko[tr1i][tr1m2][0], x3 = ko[tr2i][tr2m][0], x4 = ko[tr2i][tr2m2][0];
- double y1 = ko[tr1i][tr1m][1], y2 = ko[tr1i][tr1m2][1], y3 = ko[tr2i][tr2m][1], y4 = ko[tr2i][tr2m2][1];
- double slope1 = (y2 - y1) / (x2 - x1);
- double slope2 = (y4 - y3) / (x4 - x3);
- if (slope1 != slope2) {
- double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4))/((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
- double y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4))/((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
- if (min(x1, x2) <= x && x <= max(x1, x2) &&
- min(y1, y2) <= y && y <= max(y1, y2) &&
- min(x3, x4) <= x && x <= max(x3, x4) &&
- min(y3, y4) <= y && y <= max(y3, y4)){
- if (c > 0) {
- bool f = false;
- for (int i = 0; i < c; i++) {
- if (r[i][0] == x && r[i][1] == y) {
- f = true;
- break;
- }
- }
- if (f == false) {
- r[c][0] = x;
- r[c][1] = y;
- c++;
- }
- } else {
- r[c][0] = x;
- r[c][1] = y;
- c++;
- }
- }
- } else {
- if ((y2-y1)/(x2-x1) == (y4-y3)/(x4-x3) && y1 - ((y2-y1)/(x2-x1) * x1) == y3 - ((y4-y3)/(x4-x3) * x3)) {
- inf = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement