Good day to all, it was necessary to write a program to cut a polygon straight, the job is done. The truth is, I used the stl template library, and this turns out to be unacceptable in this task, how can it be as simple as possible to transfer this case to rtl (runtime library)? Difficulties arose in one function.

RTL - means you can not use:

  • exceptions (throw, catch, try)

  • type casting RTTI (* cast)

  • Standard Template Library (STL)

I attach the code:

// Ρ€Π°Π·Ρ€Π΅Π·Π°Π½ΠΈΠ΅ Π²Ρ‹ΠΏΡƒΠΊΠ»ΠΎΠ³ΠΎ ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° ΠΏΠΎ прямой // функция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π΄Π²Π° ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° // ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ Π»Π΅ΠΆΠΈΡ‚ с ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ стороны ΠΎΡ‚ прямой // Π²Ρ‚ΠΎΡ€ΠΎΠΉ - с ΠΎΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΠΉ void cut_convex_for_line(vector < point > p, line l, vector < point > &v1, vector < point > &v2, point &p1, point &p2) { int n = p.size(); int i, j; // Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ Ρ‚ΠΎΡ‡ΠΊΠΈ пСрСсСчСниС прямой с ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠΎΠΌ ΠΈ вставляСм ΠΈΡ… Π² ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ int c = 0; // счётчик пСрСсСчСний ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° с прямой list < point > s(p.begin(), p.end()); // прСдставляСм ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ ΠΊΠ°ΠΊ список Π²Π΅Ρ€ΡˆΠΈΠ½ list < point > ::iterator it, jt; // ΠΈΡ‚Π΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ ΠΎΠ±Ρ…ΠΎΠ΄Ρ‹ list < point > ::iterator i1, i2; // ΠΈΡ‚Π΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ вставлСнных Ρ‚ΠΎΡ‡Π΅ΠΊ for (it = s.begin(); it != s.end(); ++it) { jt = it; ++jt; if (jt == s.end()) jt = s.begin(); // пСрСсСкаСм ΠΏΡ€ΡΠΌΡƒΡŽ со стороной point t; int flag = cross_segment_line(*it, *jt, l, t); // Ссли прямая ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΡ‚ ΠΏΠΎ сторонС if (flag == 2) { if (polygon_for_line(p, l) > 0) v1 = p; else v2 = p; return; } // Ссли прямая ΠΈ сторона Π½Π΅ ΠΏΠ΅Ρ€Π΅ΡΠ΅ΠΊΠ°ΡŽΡ‚ΡΡ if (flag == 0) continue; // Ссли прямая ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΡ‚ Ρ‡Π΅Ρ€Π΅Π· Π²Π΅Ρ€ΡˆΠΈΠ½Ρƒ ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° if (abs(tx - (*it).x) <= eps && abs(ty - (*it).y) <= eps) { if (c == 0) i1 = it; else i2 = it; ++c; continue; } if (abs(tx - (*jt).x) <= eps && abs(ty - (*jt).y) <= eps) continue; // ΠΈΠ½Π°Ρ‡Π΅ прямая пСрСсСкаСт сторону, вставляСм Ρ‚ΠΎΡ‡ΠΊΡƒ пСрСсСчСния Π² ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ ++it; it = s.insert(it, t); // ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ счётчик пСрСсСчСний ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ° с прямой if (c == 0) i1 = it; else i2 = it; ++c; } // Ссли прямая Π½Π΅ пСрСсСкаСт ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ if (c != 2) { if (polygon_for_line(p, l) > 0) v1 = p; else v2 = p; return; } // прСдставляСм ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ массивом Ρ‚ΠΎΡ‡Π΅ΠΊ n = s.size(); vector < point > all(s.begin(), s.end()); int j1, j2; for (it = s.begin(), i = 0; it != s.end(); ++i, ++it) { if (it == i1) j1 = i; if (it == i2) j2 = i; } // Ρ€Π΅ΠΆΠ΅ΠΌ ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊ p1 = all[j1]; p2 = all[j2]; cut_polygon_for_edge(all, j1, j2, v1, v2); // Ссли ΠΌΠ½ΠΎΠ³ΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠΈ ΠΈΠΌΠ΅ΡŽΡ‚ Π½Π΅ Ρ‚ΠΎ располоТСниС ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π½Π°ΠΌ трСбуСтся - мСняСм ΠΈΡ… мСстами if (polygon_for_line(v1, l) < 0) swap(v1, v2); } 

PS Apparently it is worth reworking all used vectors under din. arrays? If I rework, I have to use them in place of the list ?

polygon class for replacing vectors:

 class polygon { public: point *polygon_ = new point; size_t size_polygon; polygon(point polygon_[], size_t size_polygon) { this->polygon_ = polygon_; this->size_polygon = size_polygon; } polygon() { polygon P(polygon_, size_polygon); } }; 
  • 3
    What is rtl? run-time library? As for me, dynamic arrays are orthogonal to them. - KoVadim
  • @ KoVadim, right, run-time library. it is necessary to alter under its templates. Not using stl - LokenGarvel '
  • 2
    You need to clarify with the teacher in detail what he does not like vectors for and what exactly he means by rtl (maybe this is some kind of Rome Template Library). - KoVadim
  • @ KoVadim, the question is supplemented - LokenGarvel '

0