Model2D.cpp 47 KB

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