Model2D.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. #include "Model2D.h"
  2. #include "Bild.h"
  3. #include "FrameworkMath.h"
  4. #include "Mat3.h"
  5. #include "MausEreignis.h"
  6. #include "Textur2D.h"
  7. using namespace Framework;
  8. // Inhalt der Model2DData Klasse aus Model2D.h
  9. // Konstruktor
  10. Model2DData::Model2DData()
  11. : ReferenceCounter(),
  12. polygons(0),
  13. vListen(0),
  14. minP(0, 0),
  15. maxP(0, 0)
  16. {}
  17. // Destruktor
  18. Model2DData::~Model2DData()
  19. {
  20. if (polygons)
  21. {
  22. int anz = polygons->getEintragAnzahl();
  23. for (int i = 0; i < anz; i++)
  24. {
  25. if (polygons->get(i).name) polygons->get(i).name->release();
  26. if (polygons->get(i).tKordinaten)
  27. polygons->get(i).tKordinaten->release();
  28. if (polygons->get(i).vertex) polygons->get(i).vertex->release();
  29. if (polygons->get(i).schwerpunkt)
  30. delete polygons->get(i).schwerpunkt;
  31. }
  32. polygons = (Array<Polygon2D>*)polygons->release();
  33. }
  34. if (vListen) vListen->release();
  35. }
  36. // privat
  37. bool Model2DData::istPunktInnen(Vertex p, int polygonId) const
  38. {
  39. if (p < minP || p > maxP || !polygons) return 0;
  40. int num = 0;
  41. auto outListP = outList.begin();
  42. for (auto polygon = polygons->begin(); polygon;
  43. polygon++, num++, outListP++)
  44. {
  45. if (polygonId >= 0 && num != polygonId) continue;
  46. int anz = polygon._.vertex->getEintragAnzahl();
  47. bool c = 0;
  48. int j = anz - 1;
  49. for (auto outListPP = outListP->begin(); outListPP; outListPP++)
  50. {
  51. Punkt out = outListPP;
  52. if (out.x < out.y && j > out.x && j < out.y) j = out.x;
  53. if (out.x > out.y && (j > out.x || j < out.y)) j = out.x;
  54. }
  55. auto point = polygon._.vertex->begin();
  56. for (int i = 0; i < anz; i++, point++)
  57. {
  58. bool cont = 0;
  59. for (auto outListPP = outListP->begin(); outListPP; outListPP++)
  60. {
  61. Punkt out = outListPP;
  62. if (out.x < out.y && i > out.x && i < out.y) cont = 1;
  63. if (out.x > out.y && (i > out.x || i < out.y)) cont = 1;
  64. }
  65. if (cont) continue;
  66. Vertex a = point;
  67. Vertex b = polygon._.vertex->get(j);
  68. if (((a.y >= p.y) != (b.y >= p.y))
  69. && (p.x
  70. <= (b.x - a.x) * (p.y - a.y) / (float)(b.y - a.y) + a.x))
  71. c = !c;
  72. j = i;
  73. }
  74. if (c) return 1;
  75. }
  76. return 0;
  77. }
  78. bool Model2DData::istLinieInnen(Vertex a, Vertex b, int polygonId) const
  79. {
  80. if (!polygons) return 0;
  81. int pAnz = polygons->getEintragAnzahl();
  82. for (int p = 0; p < pAnz; p++)
  83. {
  84. if (polygonId >= 0 && p != polygonId) continue;
  85. int ola = outList.z(p)->getEintragAnzahl();
  86. int anz = polygons->get(p).vertex->getEintragAnzahl();
  87. int j = anz - 1;
  88. for (int k = 0; k < ola; k++)
  89. {
  90. Punkt out = outList.z(p)->get(k);
  91. if (out.x < out.y && j > out.x && j < out.y) j = out.x;
  92. if (out.x > out.y && (j > out.x || j < out.y)) j = out.x;
  93. }
  94. for (int i = 0; i < anz; i++)
  95. {
  96. bool cont = 0;
  97. for (int k = 0; k < ola; k++)
  98. {
  99. Punkt out = outList.z(p)->get(k);
  100. if (out.x < out.y && i > out.x && i < out.y) cont = 1;
  101. if (out.x > out.y && (i > out.x || i < out.y)) cont = 1;
  102. }
  103. if (cont) continue;
  104. Punkt va = polygons->get(p).vertex->get(i);
  105. Punkt vb = polygons->get(p).vertex->get(j);
  106. if ((Punkt)a == va && (Punkt)b == vb) return 1;
  107. if ((Punkt)a == vb && (Punkt)b == va) return 1;
  108. j = i;
  109. }
  110. Vertex len = b - a;
  111. Vertex speed(len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f);
  112. int mLen = 0;
  113. if (fabs(len.x) > fabs(len.y))
  114. {
  115. mLen = (int)fabs(len.x);
  116. speed.y = len.y / (float)fabs(len.x);
  117. }
  118. else
  119. {
  120. mLen = (int)fabs(len.y);
  121. speed.x = len.x / (float)fabs(len.y);
  122. }
  123. int i = 1;
  124. bool inside = 1;
  125. for (Vertex vp = speed + a;
  126. (Punkt)vp != (Punkt)(b - speed) && inside && i < mLen - 1;
  127. vp += speed, i++)
  128. inside &= istPunktInnen(vp, p);
  129. if (inside) return 1;
  130. }
  131. return 0;
  132. }
  133. // nicht constant
  134. bool Model2DData::erstelleModell(Array<Polygon2D>* polygons)
  135. {
  136. removeModell();
  137. if (!polygons || !polygons->getEintragAnzahl())
  138. {
  139. this->polygons = polygons;
  140. vListen = new RCArray<RCArray<DreieckListe<Vertex>>>();
  141. return 1;
  142. }
  143. this->polygons = polygons;
  144. int pAnz = polygons->getEintragAnzahl();
  145. vListen = new RCArray<RCArray<DreieckListe<Vertex>>>();
  146. for (int p = 0; p < pAnz; p++)
  147. {
  148. Polygon2D pg = polygons->get(p);
  149. if (!pg.vertex || pg.vertex->getEintragAnzahl() < 3) continue;
  150. vListen->add(new RCArray<DreieckListe<Vertex>>());
  151. outList.add(new Array<Punkt>);
  152. int vAnz = pg.vertex->getEintragAnzahl();
  153. bool textur = pg.tKordinaten != 0;
  154. for (int i = 0; i < vAnz && textur; i++)
  155. textur &= pg.tKordinaten->hat(i);
  156. for (int i = 0; i < vAnz; i++)
  157. {
  158. if ((float)maxP.x < fabs(pg.vertex->get(i).x))
  159. {
  160. maxP.x = abs((int)pg.vertex->get(i).x) + 1;
  161. maxP.y = abs((int)pg.vertex->get(i).x) + 1;
  162. }
  163. if ((float)maxP.y < fabs(pg.vertex->get(i).y))
  164. {
  165. maxP.x = abs((int)pg.vertex->get(i).y) + 1;
  166. maxP.y = abs((int)pg.vertex->get(i).y) + 1;
  167. }
  168. }
  169. minP = -maxP;
  170. if (!textur)
  171. {
  172. if (pg.tKordinaten) pg.tKordinaten->leeren();
  173. }
  174. RCArray<RCArray<DreieckListe<Vertex>>> lists;
  175. int lauf = 0;
  176. while (1)
  177. {
  178. lists.add(new RCArray<DreieckListe<Vertex>>());
  179. outList.z(p)->add(Punkt(0, 0));
  180. bool fertig = 0;
  181. Vertex a;
  182. Vertex b;
  183. Array<Punkt> tmpOutList;
  184. for (int i = 0; i < vAnz; i++)
  185. {
  186. bool cont = 0;
  187. int vorher = i - 1;
  188. int nachher = i + 1;
  189. if (nachher >= vAnz) nachher = 0;
  190. if (vorher < 0) vorher = vAnz - 1;
  191. int ola = outList.z(p)->getEintragAnzahl();
  192. for (int j = 0; j < ola; j++)
  193. {
  194. Punkt out = outList.z(p)->get(j);
  195. if (out.x < out.y)
  196. {
  197. if (nachher > out.x && nachher < out.y) nachher = out.y;
  198. if (vorher > out.x && vorher < out.y) vorher = out.x;
  199. }
  200. if (out.x > out.y)
  201. {
  202. if (nachher > out.x || nachher < out.y) nachher = out.y;
  203. if (vorher > out.x || vorher < out.y) vorher = out.x;
  204. }
  205. if (out.x < out.y && i > out.x && i < out.y) cont = 1;
  206. if (out.x > out.y && (i > out.x || i < out.y)) cont = 1;
  207. }
  208. if (cont) continue;
  209. if (vorher < 0)
  210. a = pg.vertex->get(vAnz + vorher);
  211. else
  212. a = pg.vertex->get(vorher);
  213. if (nachher > vAnz - 1)
  214. b = pg.vertex->get(nachher - vAnz + 1);
  215. else
  216. b = pg.vertex->get(nachher);
  217. if (istLinieInnen(a, b, p))
  218. {
  219. DreieckListe<Vertex>* lowL = new DreieckListe<Vertex>();
  220. DreieckListe<Vertex>* heightL = new DreieckListe<Vertex>();
  221. lowL->addPunkt(new Vertex(pg.vertex->get(i)),
  222. textur ? new Vertex(pg.tKordinaten->get(i)) : 0);
  223. heightL->addPunkt(new Vertex(pg.vertex->get(i)),
  224. textur ? new Vertex(pg.tKordinaten->get(i)) : 0);
  225. int height = i + 1;
  226. int low = i - 1;
  227. Punkt outL(0, 0);
  228. Punkt outH(0, 0);
  229. for (int k = 0; k < 2; k++)
  230. {
  231. bool lowp = !k;
  232. while (1)
  233. {
  234. if (height >= vAnz) height = 0;
  235. if (low < 0) low = vAnz - 1;
  236. for (int j = 0; j <= lauf; j++)
  237. {
  238. Punkt out = outList.z(p)->get(j);
  239. if (out.x < out.y)
  240. {
  241. if (height > out.x && height < out.y)
  242. height = out.y;
  243. if (low > out.x && low < out.y) low = out.x;
  244. }
  245. if (out.x > out.y)
  246. {
  247. if (height > out.x || height < out.y)
  248. height = out.y;
  249. if (low > out.x || low < out.y) low = out.x;
  250. }
  251. }
  252. Vertex a = pg.vertex->get(height);
  253. Vertex b = pg.vertex->get(low);
  254. if (low == height)
  255. {
  256. fertig = 1;
  257. outList.z(p)->set(Punkt(0, 0), lauf);
  258. if (!k)
  259. lowL->addPunkt(new Vertex(b),
  260. textur ? new Vertex(
  261. pg.tKordinaten->get(low))
  262. : 0);
  263. else
  264. heightL->addPunkt(new Vertex(b),
  265. textur ? new Vertex(
  266. pg.tKordinaten->get(low))
  267. : 0);
  268. break;
  269. }
  270. bool inside = istLinieInnen(a, b, p);
  271. if (inside)
  272. {
  273. if (!k)
  274. outL = Punkt(low, height);
  275. else
  276. outH = Punkt(low, height);
  277. outList.z(p)->set(Punkt(low, height), lauf);
  278. }
  279. if (lowp)
  280. {
  281. if (!k)
  282. lowL->addPunkt(new Vertex(b),
  283. textur ? new Vertex(
  284. pg.tKordinaten->get(low))
  285. : 0);
  286. else
  287. heightL->addPunkt(new Vertex(b),
  288. textur ? new Vertex(
  289. pg.tKordinaten->get(low))
  290. : 0);
  291. low--;
  292. }
  293. else
  294. {
  295. if (!k)
  296. lowL->addPunkt(new Vertex(a),
  297. textur ? new Vertex(
  298. pg.tKordinaten->get(height))
  299. : 0);
  300. else
  301. heightL->addPunkt(new Vertex(a),
  302. textur ? new Vertex(
  303. pg.tKordinaten->get(height))
  304. : 0);
  305. height++;
  306. }
  307. lowp = !lowp;
  308. if (!inside)
  309. {
  310. height = i + 1;
  311. low = i - 1;
  312. outList.z(p)->set(Punkt(0, 0), lauf);
  313. break;
  314. }
  315. }
  316. if (fertig) break;
  317. }
  318. if (lowL->getDreieckAnzahl() > heightL->getDreieckAnzahl())
  319. {
  320. lists.z(lauf)->add(lowL);
  321. tmpOutList.add(outL);
  322. heightL->release();
  323. }
  324. else
  325. {
  326. lists.z(lauf)->add(heightL);
  327. tmpOutList.add(outH);
  328. lowL->release();
  329. }
  330. }
  331. else
  332. lists.z(lauf)->add(new DreieckListe<Vertex>());
  333. if (fertig) break;
  334. }
  335. int maxP = -1;
  336. int max = 0;
  337. for (int i = 0; i < vAnz; i++)
  338. {
  339. if (lists.z(lauf)->hat(i)
  340. && lists.z(lauf)->z(i)->getDreieckAnzahl() > max)
  341. {
  342. max = lists.z(lauf)->z(i)->getDreieckAnzahl();
  343. maxP = i;
  344. }
  345. }
  346. if (!max || maxP < 0) break;
  347. vListen->z(p)->add(lists.z(lauf)->get(maxP));
  348. outList.z(p)->set(tmpOutList.get(maxP), lauf);
  349. if (fertig) break;
  350. lauf++;
  351. }
  352. outList.z(p)->leeren();
  353. }
  354. return 1;
  355. }
  356. void Model2DData::removeModell() // setzt die Vertex daten zurück
  357. {
  358. if (polygons)
  359. {
  360. int anz = polygons->getEintragAnzahl();
  361. for (int i = 0; i < anz; i++)
  362. {
  363. if (polygons->get(i).name) polygons->get(i).name->release();
  364. if (polygons->get(i).tKordinaten)
  365. polygons->get(i).tKordinaten->release();
  366. if (polygons->get(i).vertex) polygons->get(i).vertex->release();
  367. if (polygons->get(i).schwerpunkt)
  368. delete polygons->get(i).schwerpunkt;
  369. }
  370. polygons = (Array<Polygon2D>*)polygons->release();
  371. }
  372. if (vListen)
  373. vListen = (RCArray<RCArray<DreieckListe<Vertex>>>*)vListen->release();
  374. outList.leeren();
  375. minP = Punkt(0, 0);
  376. maxP = Punkt(0, 0);
  377. }
  378. bool Model2DData::calcHitPoint(Vertex pos,
  379. Vertex dir,
  380. const char* polygonName,
  381. Vertex& hitpoint,
  382. Vertex& moveSpeed,
  383. float& rotSpeed) const
  384. {
  385. if (dir.x == 0 && dir.y == 0) return 0;
  386. bool ret = 0;
  387. for (Polygon2D polygon : *polygons)
  388. {
  389. if (polygon.name->istGleich(polygonName))
  390. {
  391. int anz = polygon.vertex->getEintragAnzahl();
  392. for (int i = 0; i < anz; i++)
  393. {
  394. Vertex a = polygon.vertex->get(i);
  395. Vertex b = polygon.vertex->get((i + 1) % anz);
  396. b -= a;
  397. float offset = 0;
  398. if (dir.y != 0 && dir.x != 0)
  399. offset = ((a.y - pos.y) / dir.y - (a.x - pos.x) / dir.x)
  400. / (b.x / dir.x
  401. - b.y / dir.y); // solve hitpoint equasion
  402. else if (dir.y == 0)
  403. {
  404. if (b.y == 0) continue;
  405. offset = (pos.y - a.y) / b.y;
  406. }
  407. else if (dir.x == 0)
  408. {
  409. if (b.x == 0) continue;
  410. offset = (pos.x - a.x) / b.x;
  411. }
  412. Vertex point = a + (b * offset);
  413. if (offset >= 0 && offset <= 1)
  414. {
  415. float f = (point.x - pos.x) / dir.x;
  416. if (!dir.x) f = (point.y - pos.y) / dir.y;
  417. if ((!ret
  418. || (hitpoint - pos).getLengthSq()
  419. > (point - pos).getLengthSq())
  420. && f > 0)
  421. {
  422. Vertex normal = b.CW90().normalize();
  423. Vertex kNorm = Vertex(dir).normalize();
  424. moveSpeed = normal * (normal * kNorm) * dir.getLength();
  425. normal
  426. = (point - *polygon.schwerpunkt).CW90().normalize();
  427. Vertex rotKraft
  428. = normal * (normal * kNorm) * dir.getLength();
  429. rotSpeed
  430. = ((float)sqrt(
  431. rotKraft.getLength()
  432. * (point - *polygon.schwerpunkt).getLength())
  433. / 180.f)
  434. * 3.14f * (normal * kNorm);
  435. hitpoint = point;
  436. if (isnan(moveSpeed.x) || isnan(moveSpeed.y)
  437. || isnan(rotSpeed))
  438. return 0;
  439. ret = 1;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. return ret;
  446. }
  447. bool Model2DData::split(Vertex pos,
  448. Vertex dir,
  449. char* polygonName,
  450. Polygon2D& partA,
  451. Polygon2D& partB,
  452. Punkt& posA,
  453. Punkt& posB,
  454. std::function<double()> random) const
  455. {
  456. Vertex originalDir = dir;
  457. bool ret = 0;
  458. int num = 0;
  459. for (Polygon2D polygon : *polygons)
  460. {
  461. if (polygon.name->istGleich(polygonName))
  462. {
  463. while (istPunktInnen(pos, num))
  464. {
  465. pos -= dir;
  466. }
  467. int anz = polygon.vertex->getEintragAnzahl();
  468. Vertex startPoint;
  469. Vertex texturSP;
  470. int leftI = 0;
  471. int rightI = 0;
  472. Vertex txtChpPix(0, 0);
  473. for (int i = 0; i < anz; i++)
  474. {
  475. Vertex a = polygon.vertex->get(i);
  476. Vertex b = polygon.vertex->get((i + 1) % anz);
  477. b -= a;
  478. if ((txtChpPix.x == 0 || txtChpPix.y == 0) && b.x != 0
  479. && b.y != 0)
  480. {
  481. Vertex ta = polygon.tKordinaten->get(i);
  482. Vertex tb = polygon.tKordinaten->get((i + 1) % anz);
  483. tb -= ta;
  484. txtChpPix = Vertex(tb.x / b.x, tb.y / b.y);
  485. }
  486. float offset = 0;
  487. if (dir.y != 0 && dir.x != 0)
  488. offset = ((a.y - pos.y) / dir.y - (a.x - pos.x) / dir.x)
  489. / (b.x / dir.x
  490. - b.y / dir.y); // solve hitpoint equasion
  491. else if (dir.y == 0)
  492. offset = (pos.y - a.y) / b.y;
  493. else if (dir.x == 0)
  494. offset = (pos.x - a.x) / b.x;
  495. Vertex point = a + (b * offset);
  496. if (offset >= 0 && offset <= 1)
  497. {
  498. if (!ret
  499. || (startPoint - pos).getLengthSq()
  500. > (point - pos).getLengthSq())
  501. {
  502. leftI = i;
  503. rightI = (i + 1) % anz;
  504. startPoint = point;
  505. texturSP = polygon.tKordinaten->get(i)
  506. + (polygon.tKordinaten->get((i + 1) % anz)
  507. - polygon.tKordinaten->get(i))
  508. * offset;
  509. }
  510. ret = 1;
  511. }
  512. }
  513. if (ret)
  514. {
  515. partA.transparent = polygon.transparent;
  516. partA.schwerpunkt = new Vertex(0, 0);
  517. partA.tKordinaten = new Array<Vertex>();
  518. partA.name = new Text(polygon.name->getText());
  519. partA.vertex = new Array<Vertex>();
  520. partB.transparent = polygon.transparent;
  521. partB.schwerpunkt = new Vertex(0, 0);
  522. partB.tKordinaten = new Array<Vertex>();
  523. partB.name = new Text(polygon.name->getText());
  524. partB.vertex = new Array<Vertex>();
  525. *partA.schwerpunkt += startPoint;
  526. *partB.schwerpunkt += startPoint;
  527. partA.vertex->add(startPoint);
  528. partB.vertex->add(startPoint);
  529. partA.tKordinaten->add(texturSP);
  530. partB.tKordinaten->add(texturSP);
  531. int leftIE = 0;
  532. int rightIE = 0;
  533. while (1)
  534. {
  535. pos = startPoint;
  536. Vertex next = startPoint + dir;
  537. Vertex nextT
  538. = texturSP
  539. + Vertex(dir.x * txtChpPix.x, dir.y * txtChpPix.y);
  540. ret = 0;
  541. bool needOne = !istPunktInnen(next);
  542. int bestI = -1;
  543. float bo1 = 1000;
  544. float bo2 = 1000;
  545. for (int i = 0; i < anz; i++)
  546. {
  547. if (i == leftI) continue;
  548. Vertex a = polygon.vertex->get(i);
  549. Vertex b = polygon.vertex->get((i + 1) % anz);
  550. b -= a;
  551. float offset1 = 0;
  552. if (dir.y != 0 && dir.x != 0)
  553. offset1
  554. = ((a.y - pos.y) / dir.y
  555. - (a.x - pos.x) / dir.x)
  556. / (b.x / dir.x
  557. - b.y / dir.y); // solve hitpoint equasion
  558. else if (dir.y == 0)
  559. offset1 = (pos.y - a.y) / b.y;
  560. else if (dir.x == 0)
  561. offset1 = (pos.x - a.x) / b.x;
  562. Vertex point = a + (b * offset1);
  563. float offset2 = 0;
  564. if (dir.x != 0)
  565. offset2 = (point.x - pos.x) / dir.x;
  566. else
  567. offset2 = (point.y - pos.y) / dir.y;
  568. if (needOne
  569. && MIN(abs(bo1), abs(bo1 - 1))
  570. + MIN(abs(bo2), bo2 - 1)
  571. > MIN(abs(offset1), abs(offset1 - 1))
  572. + MIN(abs(offset2), abs(offset2 - 1)))
  573. {
  574. bo1 = offset1;
  575. bo2 = offset2;
  576. bestI = i;
  577. }
  578. if (offset1 >= 0 && offset1 <= 1 && offset2 >= 0
  579. && offset2 <= 1)
  580. {
  581. if (!ret
  582. || (startPoint - pos).getLengthSq()
  583. > (point - pos).getLengthSq())
  584. {
  585. leftIE = i;
  586. rightIE = (i + 1) % anz;
  587. startPoint = point;
  588. texturSP
  589. = polygon.tKordinaten->get(i)
  590. + (polygon.tKordinaten->get((i + 1) % anz)
  591. - polygon.tKordinaten->get(i))
  592. * offset1;
  593. }
  594. ret = 1;
  595. }
  596. }
  597. if (needOne && !ret)
  598. {
  599. Vertex a = polygon.vertex->get(bestI);
  600. Vertex b = polygon.vertex->get((bestI + 1) % anz);
  601. b -= a;
  602. leftIE = bestI;
  603. rightIE = (bestI + 1) % anz;
  604. startPoint = a + (b * bo1);
  605. texturSP = polygon.tKordinaten->get(bestI)
  606. + (polygon.tKordinaten->get((bestI + 1) % anz)
  607. - polygon.tKordinaten->get(bestI))
  608. * bo1;
  609. ret = 1;
  610. }
  611. if (ret) break;
  612. *partA.schwerpunkt += next;
  613. *partB.schwerpunkt += next;
  614. partA.vertex->add(next);
  615. partB.vertex->add(next);
  616. partA.tKordinaten->add(nextT);
  617. partB.tKordinaten->add(nextT);
  618. startPoint = next;
  619. texturSP = nextT;
  620. dir = originalDir.rotation((float)(random() - 0.5));
  621. }
  622. *partA.schwerpunkt += startPoint;
  623. *partB.schwerpunkt += startPoint;
  624. partA.vertex->add(startPoint);
  625. partB.vertex->add(startPoint);
  626. partA.tKordinaten->add(texturSP);
  627. partB.tKordinaten->add(texturSP);
  628. for (int i = rightIE; i != leftI; i++)
  629. {
  630. i = i % anz;
  631. if (i == leftI) break;
  632. *partA.schwerpunkt += polygon.vertex->get(i);
  633. partA.vertex->add(polygon.vertex->get(i));
  634. partA.tKordinaten->add(polygon.tKordinaten->get(i));
  635. }
  636. *partA.schwerpunkt += polygon.vertex->get(leftI);
  637. partA.vertex->add(polygon.vertex->get(leftI));
  638. partA.tKordinaten->add(polygon.tKordinaten->get(leftI));
  639. for (int i = leftIE; i != rightI; i--)
  640. {
  641. if (i < 0) i += anz;
  642. if (i == rightI) break;
  643. *partB.schwerpunkt += polygon.vertex->get(i);
  644. partB.vertex->add(polygon.vertex->get(i));
  645. partB.tKordinaten->add(polygon.tKordinaten->get(i));
  646. }
  647. *partB.schwerpunkt += polygon.vertex->get(rightI);
  648. partB.vertex->add(polygon.vertex->get(rightI));
  649. partB.tKordinaten->add(polygon.tKordinaten->get(rightI));
  650. *partA.schwerpunkt /= (float)partA.vertex->getEintragAnzahl();
  651. *partB.schwerpunkt /= (float)partB.vertex->getEintragAnzahl();
  652. posA = (Punkt)*partA.schwerpunkt;
  653. posB = (Punkt)*partB.schwerpunkt;
  654. for (int i = 0; i < partA.vertex->getEintragAnzahl(); i++)
  655. partA.vertex->set(
  656. partA.vertex->get(i) - *partA.schwerpunkt, i);
  657. for (int i = 0; i < partB.vertex->getEintragAnzahl(); i++)
  658. partB.vertex->set(
  659. partB.vertex->get(i) - *partB.schwerpunkt, i);
  660. *partA.schwerpunkt = Vertex(0, 0);
  661. *partB.schwerpunkt = Vertex(0, 0);
  662. }
  663. }
  664. num++;
  665. }
  666. return ret;
  667. }
  668. float Model2DData::getMasse() const
  669. {
  670. float m = 0;
  671. for (Polygon2D p : *polygons)
  672. {
  673. if (p.transparent) continue;
  674. int anz = p.vertex->getEintragAnzahl();
  675. if (anz < 3) continue;
  676. Vertex p1 = p.vertex->get(anz - 1);
  677. Vertex p2 = p.vertex->get(0);
  678. m += (p1.y + p2.y) * (p1.x - p2.x);
  679. for (int i = 1; i < anz; i++)
  680. {
  681. p1 = p.vertex->get(i - 1);
  682. p2 = p.vertex->get(i);
  683. m += (p1.y + p2.y) * (p1.x - p2.x);
  684. }
  685. }
  686. m *= 0.5f;
  687. return m;
  688. }
  689. // Inhalt der Model2D Klasse aus Model2D.h
  690. // Konstruktor
  691. Model2DObject::Model2DObject()
  692. : Object2D()
  693. {
  694. rData = 0;
  695. textur = new RCArray<Textur2D>();
  696. }
  697. // Destruktor
  698. Model2DObject::~Model2DObject()
  699. {
  700. if (rData) rData->release();
  701. textur->release();
  702. }
  703. // nicht constant
  704. void Model2DObject::setModel(Model2DData* mdl)
  705. {
  706. if (rData) rData->release();
  707. rData = mdl;
  708. }
  709. void Model2DObject::setTextur(Textur2D* t)
  710. {
  711. if (rData)
  712. {
  713. textur->leeren();
  714. for (int i = 0; i < rData->polygons->getEintragAnzahl(); i++)
  715. textur->add(dynamic_cast<Textur2D*>(t->getThis()));
  716. }
  717. t->release();
  718. }
  719. void Model2DObject::impuls(Vertex start, Vertex speed, float strength)
  720. {
  721. start = getObjectPos(start);
  722. speed = getObjectDir(speed);
  723. if (rData)
  724. {
  725. Vertex resSpeed;
  726. float resRotSpeed = 0;
  727. Vertex hp;
  728. Vertex mSpeed;
  729. float rSpeed;
  730. float dist = INFINITY;
  731. for (Polygon2D p : *rData->polygons)
  732. {
  733. if (!p.transparent
  734. && rData->calcHitPoint(
  735. start, speed, p.name->getText(), hp, mSpeed, rSpeed))
  736. {
  737. float f = (hp.x - start.x) / speed.x;
  738. if (!speed.x) f = (hp.y - start.y) / speed.y;
  739. if ((hp - start).getLengthSq() < dist && f > 0)
  740. {
  741. resSpeed = mSpeed.rotation(rotation);
  742. resRotSpeed = rSpeed;
  743. dist = (hp - start).getLengthSq();
  744. }
  745. }
  746. }
  747. // TODO schleife über alle polygone und translation von start und speed
  748. // in Object koordinaten
  749. if (dist < INFINITY)
  750. {
  751. this->speed += resSpeed * strength;
  752. this->rSpeed += resRotSpeed * strength;
  753. }
  754. }
  755. }
  756. void Model2DObject::setTextur(Textur2D* t, const char* polygonName)
  757. {
  758. int index = 0;
  759. for (Polygon2D i : *rData->polygons)
  760. {
  761. if (i.name->istGleich(polygonName))
  762. textur->set(dynamic_cast<Textur2D*>(t->getThis()), index);
  763. index++;
  764. }
  765. t->release();
  766. }
  767. void Model2DObject::render(
  768. Mat3<float>& kamMat, Bild& zRObj, const char* kamName)
  769. {
  770. if (!rData || !rData->polygons || !textur) return;
  771. int num = 0;
  772. for (auto p : *rData->vListen)
  773. {
  774. Mat3<float> mat = kamMat * getObjectMatrix();
  775. if (textur->hat(num))
  776. {
  777. Bild* txt = textur->z(num)->zTextur();
  778. for (auto i = p->begin(); i && txt; i++)
  779. {
  780. for (auto j = i->zListe()->begin();
  781. j.hasNext() && j.next().hasNext();
  782. j++)
  783. {
  784. Vertex a = mat * *j->punkt;
  785. Vertex b = mat * *j.next()->punkt;
  786. Vertex c = mat * *j.next().next()->punkt;
  787. Punkt ta = (Punkt)Vertex(
  788. j->textur->x * (float)(txt->getBreite() - 1),
  789. j->textur->y * (float)(txt->getHeight() - 1));
  790. Punkt tb = (Punkt)Vertex(
  791. j.next()->textur->x * (float)(txt->getBreite() - 1),
  792. j.next()->textur->y * (float)(txt->getHeight() - 1));
  793. Punkt tc
  794. = (Punkt)Vertex(j.next().next()->textur->x
  795. * (float)(txt->getBreite() - 1),
  796. j.next().next()->textur->y
  797. * (float)(txt->getHeight() - 1));
  798. zRObj.drawDreieckTexturAlpha(a, b, c, ta, tb, tc, *txt);
  799. }
  800. }
  801. }
  802. num++;
  803. }
  804. /* Draws 2D Mesh
  805. for( auto *p = &rData->vListen->getIterator(); p && p->set; p = p->next,
  806. num++ )
  807. {
  808. Mat3< float > mat = kamMat * getObjectMatrix();
  809. for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
  810. {
  811. for( auto *j = &i->var->zListe()->getArray(); j->next->next &&
  812. j->next->next->set; j = j->next )
  813. {
  814. Vertex a = mat * *j->var->punkt;
  815. Vertex b = mat * *j->next->var->punkt;
  816. Vertex c = mat * *j->next->next->var->punkt;
  817. zRObj.drawLinie( a, b, 0xFFFFFFFF );
  818. zRObj.drawLinie( a, c, 0xFFFFFFFF );
  819. zRObj.drawLinie( b, c, 0xFFFFFFFF );
  820. }
  821. }
  822. }
  823. */
  824. }
  825. // constant
  826. bool Model2DObject::istPunktInnen(Vertex p, bool ignoreTransparent) const
  827. {
  828. if (!rData) return 0;
  829. p -= position;
  830. if (p < Mat3<float>::scaling(size) * rData->minP
  831. || p > Mat3<float>::scaling(size) * rData->maxP || !rData->polygons)
  832. return 0;
  833. Mat3<float> mat
  834. = Mat3<float>::rotation(-rotation) * Mat3<float>::scaling(1 / size);
  835. p = mat * p;
  836. for (Polygon2D polygon : *rData->polygons)
  837. {
  838. if (polygon.transparent && !ignoreTransparent) continue;
  839. bool c = 0;
  840. for (auto point = polygon.vertex->begin(); point; point++)
  841. {
  842. Vertex a;
  843. if (point.next())
  844. a = point.next();
  845. else
  846. a = polygon.vertex->get(0);
  847. Vertex b = point;
  848. if (((a.y >= p.y) != (b.y >= p.y))
  849. && (p.x
  850. <= (b.x - a.x) * (p.y - a.y) / (float)(b.y - a.y) + a.x))
  851. c = !c;
  852. }
  853. if (c) return 1;
  854. }
  855. return 0;
  856. }
  857. bool Model2DObject::istLinieInnen(
  858. Vertex a, Vertex b, bool ignoreTransparent) const
  859. {
  860. if (!rData || !rData->polygons) return 0;
  861. int pAnz = rData->polygons->getEintragAnzahl();
  862. for (int p = 0; p < pAnz; p++)
  863. {
  864. if (rData->polygons->get(p).transparent && !ignoreTransparent) continue;
  865. Mat3<float> mat
  866. = Mat3<float>::rotation(rotation) * Mat3<float>::scaling(size);
  867. int anz = rData->polygons->get(p).vertex->getEintragAnzahl();
  868. int j = anz - 1;
  869. for (int i = 0; i < anz; i++)
  870. {
  871. Punkt va = mat * rData->polygons->get(p).vertex->get(i);
  872. Punkt vb = mat * rData->polygons->get(p).vertex->get(j);
  873. if ((Punkt)a == position + va && (Punkt)b == position + vb)
  874. return 1;
  875. if ((Punkt)a == position + vb && (Punkt)b == position + va)
  876. return 1;
  877. j = i;
  878. }
  879. Vertex len = b - a;
  880. Vertex speed(len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f);
  881. int mLen = 0;
  882. if (fabs(len.x) > fabs(len.y))
  883. {
  884. mLen = (int)fabs(len.x);
  885. speed.y = len.y / (float)fabs(len.x);
  886. }
  887. else
  888. {
  889. mLen = (int)fabs(len.y);
  890. speed.x = len.x / (float)fabs(len.y);
  891. }
  892. int i = 1;
  893. bool inside = 1;
  894. for (Vertex vp = speed + a;
  895. (Punkt)vp != (Punkt)(b - speed) && inside && i < mLen - 1;
  896. vp += speed, i++)
  897. inside &= istPunktInnen(vp, ignoreTransparent);
  898. if (inside) return 1;
  899. }
  900. return 0;
  901. }
  902. bool Model2DObject::istModelInnen(
  903. const Object2D* zObj, Vertex* sp, bool end, bool ignoreTransparent) const
  904. {
  905. if (!end)
  906. {
  907. if (!getBoundingBox().collidesWith(zObj->getBoundingBox())) return 0;
  908. }
  909. Mat3<float> mat = getObjectMatrix();
  910. for (Polygon2D polygon : *rData->polygons)
  911. {
  912. if (polygon.transparent && !ignoreTransparent) continue;
  913. int anz = polygon.vertex->getEintragAnzahl();
  914. for (int i = 0; i < anz; i++)
  915. {
  916. if (zObj->istPunktInnen(
  917. mat * polygon.vertex->get(i), ignoreTransparent))
  918. {
  919. if (sp) *sp = mat * polygon.vertex->get(i);
  920. return 1;
  921. }
  922. }
  923. }
  924. if (end) return 0;
  925. return zObj->istModelInnen(this, sp, 1, ignoreTransparent);
  926. }
  927. Rect2<float> Model2DObject::getBoundingBox() const
  928. {
  929. if (rData)
  930. return Rect2<float>{(Vertex)rData->minP * size + position,
  931. (Vertex)rData->maxP * size + position};
  932. return Rect2<float>();
  933. }
  934. bool Model2DObject::calcHitPoint(Vertex pos, Vertex dir, Vertex& hitpoint) const
  935. {
  936. pos = getObjectPos(pos);
  937. dir = getObjectDir(dir);
  938. Vertex ms;
  939. Vertex hp;
  940. float rs;
  941. float dist = INFINITY;
  942. if (rData)
  943. {
  944. for (Polygon2D p : *rData->polygons)
  945. {
  946. if (!p.transparent
  947. && rData->calcHitPoint(pos, dir, p.name->getText(), hp, ms, rs))
  948. {
  949. float f = (hp.x - pos.x) / dir.x;
  950. if (!speed.x) f = (hp.y - pos.y) / dir.y;
  951. if ((hp - pos).getLengthSq() < dist && f > 0)
  952. {
  953. hitpoint = getObjectMatrix() * hp;
  954. dist = (hp - pos).getLengthSq();
  955. }
  956. }
  957. }
  958. if (dist < INFINITY) return 1;
  959. }
  960. return 0;
  961. }
  962. float Model2DObject::getLuftWiederstand() const
  963. {
  964. if (!rData) return 0;
  965. float angle = speed.angle(Vertex(1, 0));
  966. float faktor = -1;
  967. if (getDrehung() > PI) faktor = -faktor;
  968. if (getDrehung() < -PI) faktor = -faktor;
  969. Mat3<float> m = Mat3<float>::rotation(rotation + faktor * angle)
  970. * Mat3<float>::scaling(size);
  971. float yMin = INFINITY;
  972. float yMax = -INFINITY;
  973. for (Polygon2D p : *rData->polygons)
  974. {
  975. if (p.transparent) continue;
  976. for (Vertex point : *p.vertex)
  977. {
  978. Vertex v = m * point;
  979. if (v.y > yMax) yMax = v.y;
  980. if (v.y < yMin) yMin = v.y;
  981. }
  982. }
  983. if (yMin != INFINITY)
  984. {
  985. return yMax - yMin;
  986. }
  987. return 0;
  988. }
  989. float Model2DObject::getMasse() const
  990. {
  991. if (!rData) return 0;
  992. return abs(rData->getMasse() * size * size);
  993. }
  994. // Gibt die Textur des ersten Polygons zurück
  995. Textur2D* Model2DObject::getTextur() const
  996. {
  997. return textur->get(0);
  998. }
  999. // Gibt die Textur eines Polygons zurück
  1000. // polygonName: Der Name des Polygons
  1001. Textur2D* Model2DObject::getTextur(const char* polygonName) const
  1002. {
  1003. int index = 0;
  1004. for (Polygon2D p : *rData->polygons)
  1005. {
  1006. if (p.name->istGleich(polygonName)) return textur->get(index);
  1007. index++;
  1008. }
  1009. return 0;
  1010. }
  1011. // Gibt die Textur des ersten Polygons ohne erhöhten Reference Counter zurück
  1012. Textur2D* Model2DObject::zTextur() const
  1013. {
  1014. return textur->z(0);
  1015. }
  1016. // Gibt die Textur eines Polygons ohne erhöhten Reference Counter zurück
  1017. // polygonName: Der Name des Polygons
  1018. Textur2D* Model2DObject::zTextur(const char* polygonName) const
  1019. {
  1020. int index = 0;
  1021. for (Polygon2D p : *rData->polygons)
  1022. {
  1023. if (p.name->istGleich(polygonName)) return textur->z(index);
  1024. index++;
  1025. }
  1026. return 0;
  1027. }
  1028. Model2DData* Model2DObject::getModel() const
  1029. {
  1030. return rData ? dynamic_cast<Model2DData*>(rData->getThis()) : 0;
  1031. }
  1032. Model2DData* Model2DObject::zModel() const
  1033. {
  1034. return rData;
  1035. }
  1036. // Inhalt der Model2D Klasse aus Model2D.h
  1037. // Konstruktor
  1038. Model2D::Model2D()
  1039. : Zeichnung()
  1040. {
  1041. farbe = 0;
  1042. style = 0;
  1043. rData = 0;
  1044. drehung = 0;
  1045. size = 1;
  1046. textur = new RCArray<Textur2D>;
  1047. }
  1048. // Destruktor
  1049. Model2D::~Model2D()
  1050. {
  1051. if (rData) rData->release();
  1052. textur->release();
  1053. }
  1054. // nicht constant
  1055. void Model2D::setModel(Model2DData* mdl)
  1056. {
  1057. if (rData) rData->release();
  1058. rData = mdl;
  1059. }
  1060. void Model2D::setDrehung(float drehung)
  1061. {
  1062. this->drehung = drehung;
  1063. while (this->drehung > PI * 2)
  1064. this->drehung -= (float)PI * 2;
  1065. while (this->drehung < 0)
  1066. this->drehung += (float)PI * 2;
  1067. rend = 1;
  1068. }
  1069. void Model2D::addDrehung(float drehung)
  1070. {
  1071. this->drehung += drehung;
  1072. while (this->drehung > PI * 2)
  1073. this->drehung -= (float)PI * 2;
  1074. while (this->drehung < 0)
  1075. this->drehung += (float)PI * 2;
  1076. rend = 1;
  1077. }
  1078. void Model2D::setSize(float size)
  1079. {
  1080. this->size = size;
  1081. rend = 1;
  1082. }
  1083. void Model2D::addSize(float size)
  1084. {
  1085. this->size += size;
  1086. rend = 1;
  1087. }
  1088. void Model2D::setTextur(Textur2D* t)
  1089. {
  1090. if (rData)
  1091. {
  1092. for (int i = 0; i < rData->polygons->getEintragAnzahl(); i++)
  1093. textur->set(dynamic_cast<Textur2D*>(t->getThis()), i);
  1094. }
  1095. t->release();
  1096. }
  1097. void Model2D::setTextur(Textur2D* t, const char* polygonName)
  1098. {
  1099. int index = 0;
  1100. for (Polygon2D p : *rData->polygons)
  1101. {
  1102. if (p.name->istGleich(polygonName))
  1103. textur->set(dynamic_cast<Textur2D*>(t->getThis()), index);
  1104. index++;
  1105. }
  1106. t->release();
  1107. }
  1108. void Model2D::setFarbe(int f)
  1109. {
  1110. farbe = f;
  1111. rend = 1;
  1112. }
  1113. bool Model2D::tick(double tickVal)
  1114. {
  1115. return Zeichnung::tick(tickVal);
  1116. }
  1117. void Model2D::render(Bild& zRObj)
  1118. {
  1119. if (!rData || hatStyleNicht(Model2D::Style::Sichtbar) || !rData->polygons)
  1120. return;
  1121. Zeichnung::render(zRObj);
  1122. int num = 0;
  1123. for (auto p : *rData->vListen)
  1124. {
  1125. Mat3<float> mat = Mat3<float>::translation(pos)
  1126. * Mat3<float>::rotation(drehung)
  1127. * Mat3<float>::scaling(size);
  1128. if (hatStyle(Model2D::Style::Textur))
  1129. {
  1130. if (!textur || !textur->z(num) || !textur->z(num)->zTextur()
  1131. || !rData->polygons->get(num).tKordinaten)
  1132. {
  1133. for (auto i : *p)
  1134. {
  1135. for (auto j = i->zListe()->begin();
  1136. j.hasNext() && j.next().hasNext();
  1137. j++)
  1138. {
  1139. Vertex a = mat * *j->punkt;
  1140. Vertex b = mat * *j.next()->punkt;
  1141. Vertex c = mat * *j.next().next()->punkt;
  1142. if (hatStyle(Model2D::Style::Alpha))
  1143. zRObj.drawDreieckAlpha(a, b, c, farbe);
  1144. else
  1145. zRObj.drawDreieck(a, b, c, farbe);
  1146. }
  1147. }
  1148. }
  1149. else
  1150. {
  1151. Bild* txt = textur->z(num)->zTextur();
  1152. for (auto i : *p)
  1153. {
  1154. for (auto j = i->zListe()->begin();
  1155. j.hasNext() && j.next().hasNext();
  1156. j++)
  1157. {
  1158. Vertex a = mat * *j->punkt;
  1159. Vertex b = mat * *j.next()->punkt;
  1160. Vertex c = mat * *j.next().next()->punkt;
  1161. Punkt ta = (Punkt)Vertex(
  1162. j->textur->x * (float)(txt->getBreite() - 1),
  1163. j->textur->y * (float)(txt->getHeight() - 1));
  1164. Punkt tb = (Punkt)Vertex(
  1165. j.next()->textur->x * (float)(txt->getBreite() - 1),
  1166. j.next()->textur->y
  1167. * (float)(txt->getHeight() - 1));
  1168. Punkt tc
  1169. = (Punkt)Vertex(j.next().next()->textur->x
  1170. * (float)(txt->getBreite() - 1),
  1171. j.next().next()->textur->y
  1172. * (float)(txt->getHeight() - 1));
  1173. if (hatStyle(Model2D::Style::Alpha))
  1174. zRObj.drawDreieckTexturAlpha(
  1175. a, b, c, ta, tb, tc, *txt);
  1176. else
  1177. zRObj.drawDreieckTextur(a, b, c, ta, tb, tc, *txt);
  1178. }
  1179. }
  1180. }
  1181. }
  1182. if (hatStyle(Model2D::Style::Mesh))
  1183. {
  1184. for (auto i : *p)
  1185. {
  1186. for (auto j = i->zListe()->begin();
  1187. j.hasNext() && j.next().hasNext();
  1188. j++)
  1189. {
  1190. Vertex a = mat * *j->punkt;
  1191. Vertex b = mat * *j.next()->punkt;
  1192. Vertex c = mat * *j.next().next()->punkt;
  1193. if (hatStyle(Model2D::Style::Alpha))
  1194. {
  1195. zRObj.drawLinieAlpha(a, b, farbe);
  1196. zRObj.drawLinieAlpha(b, c, farbe);
  1197. zRObj.drawLinieAlpha(c, a, farbe);
  1198. }
  1199. else
  1200. {
  1201. zRObj.drawLinie(a, b, farbe);
  1202. zRObj.drawLinie(b, c, farbe);
  1203. zRObj.drawLinie(c, a, farbe);
  1204. }
  1205. }
  1206. }
  1207. }
  1208. if (hatStyle(Model2D::Style::Rahmen))
  1209. {
  1210. auto beg = rData->polygons->get(num).vertex->begin();
  1211. if (beg)
  1212. {
  1213. Vertex letzter;
  1214. for (auto e = beg; e && e.hasNext(); e++)
  1215. {
  1216. if (hatStyle(Model2D::Style::Alpha))
  1217. zRObj.drawLinieAlpha(
  1218. mat * e._, mat * e.next()._, farbe);
  1219. else
  1220. zRObj.drawLinie(mat * e._, mat * e.next()._, farbe);
  1221. letzter = e.next();
  1222. }
  1223. if (beg.hasNext())
  1224. {
  1225. if (hatStyle(Model2D::Style::Alpha))
  1226. zRObj.drawLinieAlpha(mat * letzter, mat * beg._, farbe);
  1227. else
  1228. zRObj.drawLinie(mat * letzter, mat * beg._, farbe);
  1229. }
  1230. }
  1231. }
  1232. num++;
  1233. }
  1234. }
  1235. // constant
  1236. float Model2D::getDrehung() const
  1237. {
  1238. return drehung;
  1239. }
  1240. float Model2D::getSize() const
  1241. {
  1242. return size;
  1243. }
  1244. // Gibt zurück, ob ein Punkt in dem Model enthalten ist
  1245. // p: Der Punkt
  1246. bool Model2D::istPunktInnen(int x, int y) const
  1247. {
  1248. return istPunktInnen(Vertex((float)x, (float)y));
  1249. }
  1250. bool Model2D::istPunktInnen(Vertex p) const
  1251. {
  1252. if (!rData) return 0;
  1253. p -= pos;
  1254. if (p < Mat3<float>::scaling(size) * rData->minP
  1255. || p > Mat3<float>::scaling(size) * rData->maxP || !rData->polygons)
  1256. return 0;
  1257. for (Polygon2D polygon : *rData->polygons)
  1258. {
  1259. if (polygon.transparent) continue;
  1260. Mat3<float> mat
  1261. = Mat3<float>::rotation(drehung) * Mat3<float>::scaling(size);
  1262. int anz = polygon.vertex->getEintragAnzahl();
  1263. bool c = 0;
  1264. int j = anz - 1;
  1265. for (int i = 0; i < anz; i++)
  1266. {
  1267. Vertex a = mat * polygon.vertex->get(i);
  1268. Vertex b = mat * polygon.vertex->get(j);
  1269. if (((a.y >= p.y) != (b.y >= p.y))
  1270. && (p.x
  1271. <= (b.x - a.x) * (p.y - a.y) / (float)(b.y - a.y) + a.x))
  1272. c = !c;
  1273. j = i;
  1274. }
  1275. if (c) return 1;
  1276. }
  1277. return 0;
  1278. }
  1279. bool Model2D::istLinieInnen(Vertex a, Vertex b) const
  1280. {
  1281. if (!rData || !rData->polygons) return 0;
  1282. int pAnz = rData->polygons->getEintragAnzahl();
  1283. for (int p = 0; p < pAnz; p++)
  1284. {
  1285. if (rData->polygons->get(p).transparent) continue;
  1286. Mat3<float> mat
  1287. = Mat3<float>::rotation(drehung) * Mat3<float>::scaling(size);
  1288. int anz = rData->polygons->get(p).vertex->getEintragAnzahl();
  1289. int j = anz - 1;
  1290. for (int i = 0; i < anz; i++)
  1291. {
  1292. Punkt va = mat * rData->polygons->get(p).vertex->get(i);
  1293. Punkt vb = mat * rData->polygons->get(p).vertex->get(j);
  1294. if ((Punkt)a == pos + va && (Punkt)b == pos + vb) return 1;
  1295. if ((Punkt)a == pos + vb && (Punkt)b == pos + va) return 1;
  1296. j = i;
  1297. }
  1298. Vertex len = b - a;
  1299. Vertex speed(len.x > 0 ? 1 : -1.f, len.y > 0 ? 1 : -1.f);
  1300. int mLen = 0;
  1301. if (fabs(len.x) > fabs(len.y))
  1302. {
  1303. mLen = (int)fabs(len.x);
  1304. speed.y = len.y / (float)fabs(len.x);
  1305. }
  1306. else
  1307. {
  1308. mLen = (int)fabs(len.y);
  1309. speed.x = len.x / (float)fabs(len.y);
  1310. }
  1311. int i = 1;
  1312. bool inside = 1;
  1313. for (Vertex vp = speed + a;
  1314. (Punkt)vp != (Punkt)(b - speed) && inside && i < mLen - 1;
  1315. vp += speed, i++)
  1316. inside &= istPunktInnen(vp);
  1317. if (inside) return 1;
  1318. }
  1319. return 0;
  1320. }
  1321. bool Model2D::istModelInnen(const Model2D* zMdl, bool end) const
  1322. {
  1323. if (!end)
  1324. {
  1325. Vertex min = (Vertex)rData->minP * size + pos;
  1326. Vertex max = (Vertex)rData->maxP * size + pos;
  1327. Vertex min2 = (Vertex)zMdl->zModel()->minP * zMdl->getSize()
  1328. + zMdl->getPosition();
  1329. Vertex max2 = (Vertex)zMdl->zModel()->maxP * zMdl->getSize()
  1330. + zMdl->getPosition();
  1331. if (max.x < min2.x || min.x > max2.x || max.y < min2.y
  1332. || min.y > max2.y)
  1333. return 0;
  1334. }
  1335. Mat3<float> mat = Mat3<float>::translation(pos)
  1336. * Mat3<float>::rotation(drehung)
  1337. * Mat3<float>::scaling(size);
  1338. for (Polygon2D polygon : *rData->polygons)
  1339. {
  1340. if (polygon.transparent) continue;
  1341. int anz = polygon.vertex->getEintragAnzahl();
  1342. for (int i = 0; i < anz; i++)
  1343. {
  1344. if (zMdl->istPunktInnen(mat * polygon.vertex->get(i))) return 1;
  1345. }
  1346. }
  1347. if (end) return 0;
  1348. return zMdl->istModelInnen(this, 1);
  1349. }
  1350. Model2DData* Model2D::getModel() const
  1351. {
  1352. return rData ? dynamic_cast<Model2DData*>(rData->getThis()) : 0;
  1353. }
  1354. Model2DData* Model2D::zModel() const
  1355. {
  1356. return rData;
  1357. }