Advertisement
Guest User

Untitled

a guest
Nov 30th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool triangleExists (double ko[][3][2], int index);
  6.  
  7. void findCollisions (int tr1i, int tr1m, int tr2i, int tr2m, double ko[][3][2], double r[6][2], int &c, bool &inf);
  8.  
  9. const int TR = 2;
  10.  
  11. int main()
  12. {
  13.     double ko[TR][3][2]; // Masīvs, kas saturēs dotās trijstūru koordinātas
  14.     int ok; // Beigt vai turpināt programmu
  15.     double r[6][2];
  16.     int c = 0;
  17.     bool inf = false;
  18.  
  19.     // Veicam darbību, kamēr lietotājs vēlas to turpināt
  20.     do {
  21.         c = 0;
  22.         inf = false;
  23.  
  24.         for (int i = 0; i < TR; i++) {
  25.                 do {
  26.                         for (int j = 0; j < 3; j++) {
  27.                                 cout << "Lūdzu ievadiet " << i + 1 << ". trijstūra " << j + 1 << ". virsotnes x koordinātu: ";
  28.                                 cin >> ko[i][j][0];
  29.                                 cout << "Lūdzu ievadiet " << i + 1 << ". trijstūra " << j + 1 << ". virsotnes y koordinātu: ";
  30.                                 cin >> ko[i][j][1];
  31.                         }
  32.  
  33.                         if (!triangleExists(ko, i))
  34.                                 cout << "Šāds trijstūris neeksistē! Lūdzu ievadiet jaunas koordinātas!" << endl;
  35.                 } while (!triangleExists(ko, i));
  36.         }
  37.  
  38.         for (int i = 0; i < TR; i++) {
  39.                 for (int j = i + 1; j < TR; j++) {
  40.                         for (int k = 0; k < 3; k++) {
  41.                                 for (int l = 0; l < 3; l++) {
  42.                                         findCollisions(i, k, j, l, ko, r, c, inf);
  43.                                 }
  44.                         }
  45.                 }
  46.         }
  47.  
  48.         if (inf == true) {
  49.                 cout << "Trijstūru kontūrām ir neierobežoti daudz kopīgi punkti (kāda mala pārklājas)!" << endl;
  50.         } else {
  51.                 if (c > 0) {
  52.                         for (int i = 0; i < c; i++) {
  53.                                 cout << "Trijstūru kontūras krustojas punktā (" << r[i][0] << ", " << r[i][1] << ")" << endl;
  54.                         }
  55.                 } else {
  56.                         cout << "Trijstūriem nav kopīgu punktu!" << endl;
  57.                 }
  58.         }
  59.  
  60.         cout << "Turpināt (1) vai beigt (0) programmas izpildi?" << endl; // Piedāvajam lietotājam turpināt programmu
  61.         cin >> ok;
  62.     } while (ok == 1);
  63.  
  64.     return 0;
  65. }
  66.  
  67. double max (double c1, double c2) {
  68.         if (c1 > c2)
  69.                 return c1;
  70.         else
  71.                 return c2;
  72. }
  73.  
  74. double min (double c1, double c2) {
  75.         if (c1 > c2)
  76.                 return c2;
  77.         else
  78.                 return c1;
  79. }
  80.  
  81. int nextLine(int curLine)
  82. {
  83.         if (curLine == 0)
  84.                 return 1;
  85.         else if (curLine == 1)
  86.                 return 2;
  87.         else if (curLine == 2)
  88.                 return 0;
  89.         else
  90.                 return -1;
  91. }
  92.  
  93. bool triangleExists(double ko[][3][2], int index)
  94. {
  95.         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])
  96.                 return false;
  97.         else
  98.                 return true;
  99. }
  100.  
  101. void findCollisions (int tr1i, int tr1m, int tr2i, int tr2m, double ko[][3][2], double r[6][2], int &c, bool &inf)
  102. {
  103.         int tr1m2 = nextLine(tr1m), tr2m2 = nextLine(tr2m);
  104.  
  105.         if (tr1m2 == -1 || tr2m2 == -1) {
  106.                 cout << "Norādīta neeksistējoša trijstūra mala!";
  107.         }
  108.  
  109.         double x1 = ko[tr1i][tr1m][0], x2 = ko[tr1i][tr1m2][0], x3 = ko[tr2i][tr2m][0], x4 = ko[tr2i][tr2m2][0];
  110.         double y1 = ko[tr1i][tr1m][1], y2 = ko[tr1i][tr1m2][1], y3 = ko[tr2i][tr2m][1], y4 = ko[tr2i][tr2m2][1];
  111.         double slope1 = (y2 - y1) / (x2 - x1);
  112.         double slope2 = (y4 - y3) / (x4 - x3);
  113.  
  114.         if (slope1 != slope2) {
  115.                 double x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4))/((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
  116.                 double y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4))/((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
  117.  
  118.                 if (min(x1, x2) <= x && x <= max(x1, x2) &&
  119.                     min(y1, y2) <= y && y <= max(y1, y2) &&
  120.                     min(x3, x4) <= x && x <= max(x3, x4) &&
  121.                     min(y3, y4) <= y && y <= max(y3, y4)){
  122.                         if (c > 0) {
  123.                                 bool f = false;
  124.  
  125.                                 for (int i = 0; i < c; i++) {
  126.                                         if (r[i][0] == x && r[i][1] == y) {
  127.                                                 f = true;
  128.                                                 break;
  129.                                         }
  130.                                 }
  131.  
  132.                                 if (f == false) {
  133.                                         r[c][0] = x;
  134.                                         r[c][1] = y;
  135.                                         c++;
  136.                                 }
  137.                         } else {
  138.                                 r[c][0] = x;
  139.                                 r[c][1] = y;
  140.                                 c++;
  141.                         }
  142.                 }
  143.         } else {
  144.                 if ((y2-y1)/(x2-x1) == (y4-y3)/(x4-x3) && y1 - ((y2-y1)/(x2-x1) * x1) == y3 - ((y4-y3)/(x4-x3) * x3)) {
  145.                         inf = true;
  146.                 }
  147.         }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement