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); } };