CMath.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. using CommonLang.Geometry;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace CommonLang
  6. {
  7. /**
  8. * util math function none float
  9. *
  10. * @author yifeizhang
  11. * @since 2006-12-1
  12. * @version 1.0
  13. */
  14. public class CMath
  15. {
  16. public static int sqrt(int i)
  17. {
  18. int l = 0;
  19. for (int k = 0x100000; k != 0; k >>= 2)
  20. {
  21. int j = l + k;
  22. l >>= 1;
  23. if (j <= i)
  24. {
  25. i -= j;
  26. l += k;
  27. }
  28. }
  29. return l;
  30. }
  31. // --------------------------------------------------------------------------------------------------------
  32. /// <summary>
  33. /// compute cyc number: (value+d) within 0~max scope
  34. /// </summary>
  35. /// <param name="value"></param>
  36. /// <param name="d"></param>
  37. /// <param name="max"></param>
  38. /// <returns></returns>
  39. static public int cycNum(int value, int d, int max)
  40. {
  41. value += d;
  42. return (value >= 0) ? (value % max) : ((max + value % max) % max);
  43. }
  44. /// <summary>
  45. /// compute cyc number: (value+d) within 0~max scope
  46. /// </summary>
  47. /// <param name="value"></param>
  48. /// <param name="max"></param>
  49. /// <returns></returns>
  50. static public int cycNum(int value, int max)
  51. {
  52. return (value >= 0) ? (value % max) : ((max + value % max) % max);
  53. }
  54. /// <summary>
  55. /// compute cyc mod: -1 mod 10 = -1
  56. /// </summary>
  57. /// <param name="value"></param>
  58. /// <param name="div"></param>
  59. /// <returns></returns>
  60. static public int cycMod(int value, int div)
  61. {
  62. return (value / div) + (value < 0 ? -1 : 0);
  63. }
  64. /// <summary>
  65. /// 获得符号
  66. /// </summary>
  67. /// <param name="value"></param>
  68. /// <returns>1 or 0 or -1</returns>
  69. static public int getDirect(int value)
  70. {
  71. return value == 0 ? 0 : (value > 0 ? 1 : -1);
  72. }
  73. /// <summary>
  74. /// 获得符号
  75. /// </summary>
  76. /// <param name="value"></param>
  77. /// <returns></returns>
  78. static public float getDirect(float value)
  79. {
  80. return value == 0 ? 0 : (value > 0 ? 1 : -1);
  81. }
  82. /// <summary>
  83. /// comput round mod roundMode(33,8) = 5 => 33/8 + (33%8==0?:0:1)
  84. /// </summary>
  85. /// <param name="value"></param>
  86. /// <param name="div"></param>
  87. /// <returns></returns>
  88. static public int roundMod(int value, int div)
  89. {
  90. return (value / div) + (value % div == 0 ? 0 : (1 * getDirect(value)));
  91. }
  92. /// <summary>
  93. /// comput round mod roundMode(33,8) = 5 => 33/8 + (33%8==0?:0:1)
  94. /// </summary>
  95. /// <param name="value"></param>
  96. /// <param name="div"></param>
  97. /// <returns></returns>
  98. static public int roundMod(float value, float div)
  99. {
  100. return (int)(value / div) + (value % div == 0 ? 0 : (1 * getDirect((int)value)));
  101. }
  102. /// <summary>
  103. /// 根据速度和时间段得到距离
  104. /// </summary>
  105. /// <param name="speed">速度 (距离/秒)</param>
  106. /// <param name="interval_ms">毫秒</param>
  107. /// <returns></returns>
  108. static public float getDistanceSpeedInTime(float speed, float interval_ms)
  109. {
  110. float rate = interval_ms / 1000.0f;
  111. return speed * rate;
  112. }
  113. /// <summary>
  114. ///
  115. /// </summary>
  116. /// <param name="x1"></param>
  117. /// <param name="y1"></param>
  118. /// <param name="x2"></param>
  119. /// <param name="y2"></param>
  120. /// <returns></returns>
  121. public static float getDistance(float x1, float y1, float x2, float y2)
  122. {
  123. float r1 = x1 - x2;
  124. float r2 = y1 - y2;
  125. return (float)Math.Sqrt(r1 * r1 + r2 * r2);
  126. }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. /// <param name="x1"></param>
  131. /// <param name="y1"></param>
  132. /// <param name="x2"></param>
  133. /// <param name="y2"></param>
  134. /// <returns></returns>
  135. public static float getDistanceSquare(float x1, float y1, float x2, float y2)
  136. {
  137. float r1 = x1 - x2;
  138. float r2 = y1 - y2;
  139. return r1 * r1 + r2 * r2;
  140. }
  141. // -------------------------------------------------------------------------------------------------------------------
  142. #region _Geometry_Collision_Include_
  143. /// <summary>
  144. /// 扇形和圆相交
  145. /// </summary>
  146. /// <param name="sx">扇形圆心</param>
  147. /// <param name="sy">扇形圆心</param>
  148. /// <param name="sr">扇形半径</param>
  149. /// <param name="dx">点</param>
  150. /// <param name="dy">点</param>
  151. /// <param name="startAngle">扇形起始角度</param>
  152. /// <param name="endAngle">扇形结束角度</param>
  153. /// <returns></returns>
  154. static public bool includeFanPoint(float sx, float sy, float sr, float dx, float dy, float startAngle, float endAngle)
  155. {
  156. float ddx = dx - sx;
  157. float ddy = dy - sy;
  158. float r = sr;
  159. if (ddx * ddx + ddy * ddy <= r * r)
  160. {
  161. float direction = OpitimizeRadians((float)Math.Atan2(ddy, ddx));
  162. startAngle = OpitimizeRadians(startAngle);
  163. endAngle = OpitimizeRadians(endAngle);
  164. if (endAngle < startAngle)
  165. {
  166. if (direction < endAngle)
  167. {
  168. direction += PI_MUL_2;
  169. }
  170. endAngle += PI_MUL_2;
  171. }
  172. if (direction >= startAngle && direction <= endAngle)
  173. {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. // --------------------------------------------------------------------------------------------------
  180. /// <summary>
  181. ///
  182. /// </summary>
  183. /// <param name="sx">圆x</param>
  184. /// <param name="sy">圆y</param>
  185. /// <param name="sr">圆r</param>
  186. /// <param name="px">点</param>
  187. /// <param name="py">点</param>
  188. /// <returns></returns>
  189. static public bool includeRoundPoint(float sx, float sy, float sr, float px, float py)
  190. {
  191. float w = sx - px;
  192. float h = sy - py;
  193. return (w * w + h * h) <= (sr * sr);
  194. }
  195. /// <summary>
  196. ///
  197. /// </summary>
  198. /// <param name="sx">圆1x</param>
  199. /// <param name="sy">圆1y</param>
  200. /// <param name="sr">圆1r</param>
  201. /// <param name="dx">圆2x</param>
  202. /// <param name="dy">圆2y</param>
  203. /// <param name="dr">圆2r</param>
  204. /// <returns></returns>
  205. static public bool includeRound2(float sx, float sy, float sr, float dx, float dy, float dr)
  206. {
  207. float w = sx - dx;
  208. float h = sy - dy;
  209. float r = sr - dr;
  210. return (w * w + h * h) <= (r * r);
  211. }
  212. // --------------------------------------------------------------------------------------------------
  213. /// <summary>
  214. ///
  215. /// </summary>
  216. /// <param name="sx1">矩形x1</param>
  217. /// <param name="sy1">矩形y1</param>
  218. /// <param name="sx2">矩形x2</param>
  219. /// <param name="sy2">矩形y2</param>
  220. /// <param name="dx">点</param>
  221. /// <param name="dy">点</param>
  222. /// <returns></returns>
  223. static public bool includeRectPoint(float sx1, float sy1, float sx2, float sy2, float dx, float dy)
  224. {
  225. if (sx2 < dx) return false;
  226. if (sx1 > dx) return false;
  227. if (sy2 < dy) return false;
  228. if (sy1 > dy) return false;
  229. return true;
  230. }
  231. /// <summary>
  232. ///
  233. /// </summary>
  234. /// <param name="sx1">矩形x</param>
  235. /// <param name="sy1">矩形y</param>
  236. /// <param name="sw">矩形w</param>
  237. /// <param name="sh">矩形h</param>
  238. /// <param name="dx">点</param>
  239. /// <param name="dy">点</param>
  240. /// <returns></returns>
  241. static public bool includeRectPoint2(float sx1, float sy1, float sw, float sh, float dx, float dy)
  242. {
  243. float sx2 = sx1 + sw;
  244. if (sx2 < dx) return false;
  245. if (sx1 > dx) return false;
  246. float sy2 = sy1 + sh;
  247. if (sy2 < dy) return false;
  248. if (sy1 > dy) return false;
  249. return true;
  250. }
  251. /// <summary>
  252. /// 点在椭圆内
  253. /// </summary>
  254. /// <param name="x0">椭圆X</param>
  255. /// <param name="y0">椭圆Y</param>
  256. /// <param name="rx">椭圆宽</param>
  257. /// <param name="ry">椭圆高</param>
  258. /// <param name="dx">点</param>
  259. /// <param name="dy">点</param>
  260. /// <returns></returns>
  261. public static bool includeEllipsePoint(float x0, float y0, float rx, float ry, float dx, float dy)
  262. {
  263. return intersectRectEllipse(dx, dy, dx, dy, x0, y0, rx, ry);
  264. }
  265. /// <summary>
  266. /// 点在粗线条内
  267. /// </summary>
  268. /// <param name="lx1">线段X1</param>
  269. /// <param name="ly1">线段Y1</param>
  270. /// <param name="lx2">线段X2</param>
  271. /// <param name="ly2">线段Y2</param>
  272. /// <param name="line_r">线段半径(粗度的一半)</param>
  273. /// <param name="dx">点</param>
  274. /// <param name="dy">点</param>
  275. /// <returns></returns>
  276. public static bool includeStripWidthPoint(
  277. float lx1, float ly1,
  278. float lx2, float ly2,
  279. float line_r,
  280. float dx, float dy)
  281. {
  282. CommonLang.Geometry.Vector2[] list = toStripWidthPolygon(lx1, ly1, lx2, ly2, line_r);
  283. return includePolygonPoint(list, dx, dy);
  284. }
  285. /// <summary>
  286. /// 点在多边形内
  287. /// </summary>
  288. /// <param name="list">多边形</param>
  289. /// <param name="dx">点</param>
  290. /// <param name="dy">点</param>
  291. /// <returns></returns>
  292. public static bool includePolygonPoint(Vector2[] list, float dx, float dy)
  293. {
  294. return CommonLang.Geometry.CollisionMath.PointInPolygon(new Vector2(dx, dy), list);
  295. }
  296. #endregion
  297. // --------------------------------------------------------------------------------------------------
  298. #region _Geometry_Collision_Intersect_
  299. /// <summary>
  300. /// 两圆相交
  301. /// </summary>
  302. /// <param name="sx">圆1x</param>
  303. /// <param name="sy">圆1y</param>
  304. /// <param name="sr">圆1r</param>
  305. /// <param name="dx">圆2x</param>
  306. /// <param name="dy">圆2y</param>
  307. /// <param name="dr">圆2r</param>
  308. /// <returns></returns>
  309. static public bool intersectRound(
  310. float sx, float sy, float sr,
  311. float dx, float dy, float dr)
  312. {
  313. float w = sx - dx;
  314. float h = sy - dy;
  315. float r = sr + dr;
  316. return (w * w + h * h) <= (r * r);
  317. }
  318. /// <summary>
  319. /// 扇形和圆相交
  320. /// </summary>
  321. /// <param name="sx">扇形圆心</param>
  322. /// <param name="sy">扇形圆心</param>
  323. /// <param name="sr">扇形半径</param>
  324. /// <param name="dx">圆形中心</param>
  325. /// <param name="dy">圆形中心</param>
  326. /// <param name="dr">圆形半径</param>
  327. /// <param name="startAngle">扇形起始角(弧度)</param>
  328. /// <param name="endAngle">扇形结束角(弧度)</param>
  329. /// <returns></returns>
  330. static public bool intersectFanRound(
  331. float sx, float sy, float sr,
  332. float dx, float dy, float dr,
  333. float startAngle, float endAngle)
  334. {
  335. float ddx = dx - sx;
  336. float ddy = dy - sy;
  337. float r = sr + dr;
  338. if (ddx * ddx + ddy * ddy <= r * r)
  339. {
  340. float direction = OpitimizeRadians((float)Math.Atan2(ddy, ddx));
  341. startAngle = OpitimizeRadians(startAngle);
  342. endAngle = OpitimizeRadians(endAngle);
  343. if (endAngle < startAngle)
  344. {
  345. if (direction < endAngle)
  346. {
  347. direction += PI_MUL_2;
  348. }
  349. endAngle += PI_MUL_2;
  350. }
  351. if (direction >= startAngle && direction <= endAngle)
  352. {
  353. return true;
  354. }
  355. if (intersectLineRound(sx, sy,
  356. sx + (float)Math.Cos(startAngle) * sr,
  357. sy + (float)Math.Sin(startAngle) * sr,
  358. dx, dy, dr))
  359. {
  360. return true;
  361. }
  362. if (intersectLineRound(sx, sy,
  363. sx + (float)Math.Cos(endAngle) * sr,
  364. sy + (float)Math.Sin(endAngle) * sr,
  365. dx, dy, dr))
  366. {
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. // --------------------------------------------------------------------------------------------------
  373. /// <summary>
  374. /// 两矩形相交
  375. /// </summary>
  376. /// <param name="sx1">矩形1</param>
  377. /// <param name="sy1">矩形1</param>
  378. /// <param name="sx2">矩形1</param>
  379. /// <param name="sy2">矩形1</param>
  380. /// <param name="dx1">矩形2</param>
  381. /// <param name="dy1">矩形2</param>
  382. /// <param name="dx2">矩形2</param>
  383. /// <param name="dy2">矩形2</param>
  384. /// <returns></returns>
  385. static public bool intersectRect(
  386. float sx1, float sy1, float sx2, float sy2,
  387. float dx1, float dy1, float dx2, float dy2)
  388. {
  389. if (sx2 < dx1) return false;
  390. if (sx1 > dx2) return false;
  391. if (sy2 < dy1) return false;
  392. if (sy1 > dy2) return false;
  393. return true;
  394. }
  395. /// <summary>
  396. /// 两矩形相交(宽高)
  397. /// </summary>
  398. /// <param name="sx1">矩形1</param>
  399. /// <param name="sy1">矩形1</param>
  400. /// <param name="sw"> 矩形1</param>
  401. /// <param name="sh"> 矩形1</param>
  402. /// <param name="dx1">矩形2</param>
  403. /// <param name="dy1">矩形2</param>
  404. /// <param name="dw"> 矩形2</param>
  405. /// <param name="dh"> 矩形2</param>
  406. /// <returns></returns>
  407. static public bool intersectRect2(
  408. float sx1, float sy1, float sw, float sh,
  409. float dx1, float dy1, float dw, float dh)
  410. {
  411. float sx2 = sx1 + sw;
  412. float dx2 = dx1 + dw;
  413. if (sx2 < dx1) return false;
  414. if (sx1 > dx2) return false;
  415. float sy2 = sy1 + sh;
  416. float dy2 = dy1 + dh;
  417. if (sy2 < dy1) return false;
  418. if (sy1 > dy2) return false;
  419. return true;
  420. }
  421. // --------------------------------------------------------------------------------------------------
  422. /// <summary>
  423. /// 矩形和线段相交
  424. /// </summary>
  425. /// <param name="sx1">矩形</param>
  426. /// <param name="sy1">矩形</param>
  427. /// <param name="sx2">矩形</param>
  428. /// <param name="sy2">矩形</param>
  429. /// <param name="lx1">线段</param>
  430. /// <param name="ly1">线段</param>
  431. /// <param name="lx2">线段</param>
  432. /// <param name="ly2">线段</param>
  433. /// <returns></returns>
  434. static public bool intersectRectLine(
  435. float sx1, float sy1,
  436. float sx2, float sy2,
  437. float lx1, float ly1,
  438. float lx2, float ly2)
  439. {
  440. float dx1 = lx1;
  441. float dy1 = ly1;
  442. float dx2 = lx2;
  443. float dy2 = ly2;
  444. if (dx1 > dx2) CUtils.Swap(ref dx1, ref dx2);
  445. if (dy1 > dy2) CUtils.Swap(ref dy1, ref dy2);
  446. if (!intersectRect(sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2))
  447. {
  448. return false;
  449. }
  450. if (intersectLine(lx1, ly1, lx2, ly2, sx1, sy1, sx2, sy1))
  451. {
  452. return true;
  453. }
  454. if (intersectLine(lx1, ly1, lx2, ly2, sx2, sy1, sx2, sy2))
  455. {
  456. return true;
  457. }
  458. if (intersectLine(lx1, ly1, lx2, ly2, sx2, sy2, sx1, sy2))
  459. {
  460. return true;
  461. }
  462. if (intersectLine(lx1, ly1, lx2, ly2, sx1, sy2, sx1, sy1))
  463. {
  464. return true;
  465. }
  466. return false;
  467. }
  468. /// <summary>
  469. /// 矩形和圆相交
  470. /// </summary>
  471. /// <param name="sx">矩形</param>
  472. /// <param name="sy">矩形</param>
  473. /// <param name="dx">矩形</param>
  474. /// <param name="dy">矩形</param>
  475. /// <param name="cx">圆形</param>
  476. /// <param name="cy">圆形</param>
  477. /// <param name="r">圆形</param>
  478. /// <returns></returns>
  479. static public bool intersectRectRound(float sx, float sy, float dx, float dy, float cx, float cy, float r)
  480. {
  481. return intersectRectEllipse(sx, sy, dx, dy, cx, cy, r, r);
  482. }
  483. /// <summary>
  484. /// 计算椭圆和矩形是否相交
  485. /// (x/rx)^2 + (y/ry)^2 = 1;
  486. /// left is: (x* ry)^2 + (y* rx)^2
  487. /// right is: (rx* ry)^2
  488. /// if(left > right) out
  489. /// else in
  490. /// </summary>
  491. /// <param name="xmin">矩形左上角x</param>
  492. /// <param name="ymin">矩形左上角y</param>
  493. /// <param name="xmax">矩形右下角x</param>
  494. /// <param name="ymax">矩形右下角y</param>
  495. /// <param name="x0">椭圆圆心x </param>
  496. /// <param name="y0">椭圆圆心y </param>
  497. /// <param name="rx">椭圆x轴半径</param>
  498. /// <param name="ry">椭圆y轴半径</param>
  499. /// <returns></returns>
  500. public static bool intersectRectEllipse(float xmin, float ymin, float xmax, float ymax, float x0, float y0, float rx, float ry)
  501. {
  502. //如果圆心点就在矩形内部, 那么就直接返回true
  503. // if ((x0, y0, x0, y0, xmin, ymin, xmax, ymax))
  504. // {
  505. // return true;
  506. // }
  507. if (includeRectPoint(xmin, ymin, xmax, ymax, x0, y0))
  508. return true;
  509. //首先找到矩形距离圆心的最近点
  510. float x = x0, y = y0;
  511. if (x < xmin)
  512. {
  513. x = xmin;
  514. }
  515. else if (x > xmax)
  516. {
  517. x = xmax;
  518. }
  519. if (y < ymin)
  520. {
  521. y = ymin;
  522. }
  523. else if (y > ymax)
  524. {
  525. y = ymax;
  526. }
  527. float dx = x - x0;
  528. float dy = y - y0;
  529. dx *= dx;
  530. dy *= dy;
  531. rx *= rx;
  532. ry *= ry;
  533. dx *= ry;
  534. dy *= rx;
  535. if (dx + dy <= rx * ry)
  536. {
  537. return true;
  538. }
  539. return false;
  540. }
  541. /// <summary>
  542. /// 两个线段相交
  543. /// ((Q2-Q1)X(P1-Q1))*((P2-Q1)X(Q2-Q1)) >= 0
  544. /// ((P2-P1)X(Q1-P1))*((Q2-P1)X(P2-P1)) >= 0
  545. /// </summary>
  546. /// <param name="p1_x">line 1</param>
  547. /// <param name="p1_y">line 1</param>
  548. /// <param name="p2_x">line 1</param>
  549. /// <param name="p2_y">line 1</param>
  550. /// <param name="q1_x">line 2</param>
  551. /// <param name="q1_y">line 2</param>
  552. /// <param name="q2_x">line 2</param>
  553. /// <param name="q2_y">line 2</param>
  554. /// <returns></returns>
  555. static public bool intersectLine(
  556. float p1_x, float p1_y, float p2_x, float p2_y,
  557. float q1_x, float q1_y, float q2_x, float q2_y)
  558. {
  559. /* croe */
  560. float v1_x = q2_x - q1_x;
  561. float v1_y = q2_y - q1_y;
  562. float v2_x = p1_x - q1_x;
  563. float v2_y = p1_y - q1_y;
  564. float v3_x = p2_x - q1_x;
  565. float v3_y = p2_y - q1_y;
  566. if ((v1_x * v2_y - v1_y * v2_x) * (v3_x * v1_y - v3_y * v1_x) < 0)
  567. {
  568. return false;
  569. }
  570. float v5_x = p2_x - p1_x;
  571. float v5_y = p2_y - p1_y;
  572. float v6_x = q1_x - p1_x;
  573. float v6_y = q1_y - p1_y;
  574. float v7_x = q2_x - p1_x;
  575. float v7_y = q2_y - p1_y;
  576. if ((v5_x * v6_y - v5_y * v6_x) * (v7_x * v5_y - v7_y * v5_x) < 0)
  577. {
  578. return false;
  579. }
  580. return true;
  581. }
  582. /// <summary>
  583. /// 两个线段相交
  584. /// ((Q2-Q1)X(P1-Q1))*((P2-Q1)X(Q2-Q1)) >= 0
  585. /// ((P2-P1)X(Q1-P1))*((Q2-P1)X(P2-P1)) >= 0
  586. /// </summary>
  587. /// <param name="p1_x">line 1</param>
  588. /// <param name="p1_y">line 1</param>
  589. /// <param name="p2_x">line 1</param>
  590. /// <param name="p2_y">line 1</param>
  591. /// <param name="q1_x">line 2</param>
  592. /// <param name="q1_y">line 2</param>
  593. /// <param name="q2_x">line 2</param>
  594. /// <param name="q2_y">line 2</param>
  595. /// <param name="ignore_touch">忽略焦点在线段内</param>
  596. /// <returns></returns>
  597. static public bool intersectLine(
  598. float p1_x, float p1_y, float p2_x, float p2_y,
  599. float q1_x, float q1_y, float q2_x, float q2_y,
  600. bool ignore_touch = true)
  601. {
  602. /* croe */
  603. float v1_x = q2_x - q1_x;
  604. float v1_y = q2_y - q1_y;
  605. float v2_x = p1_x - q1_x;
  606. float v2_y = p1_y - q1_y;
  607. float v3_x = p2_x - q1_x;
  608. float v3_y = p2_y - q1_y;
  609. float f = (v1_x * v2_y - v1_y * v2_x) * (v3_x * v1_y - v3_y * v1_x);
  610. if (ignore_touch ? f <= 0 : f < 0)
  611. {
  612. return false;
  613. }
  614. float v5_x = p2_x - p1_x;
  615. float v5_y = p2_y - p1_y;
  616. float v6_x = q1_x - p1_x;
  617. float v6_y = q1_y - p1_y;
  618. float v7_x = q2_x - p1_x;
  619. float v7_y = q2_y - p1_y;
  620. float s = (v5_x * v6_y - v5_y * v6_x) * (v7_x * v5_y - v7_y * v5_x);
  621. if (ignore_touch ? s <= 0 : s < 0)
  622. {
  623. return false;
  624. }
  625. return true;
  626. }
  627. /// <summary>
  628. /// 求线段与圆碰撞
  629. /// </summary>
  630. /// <param name="lx1">线段起点</param>
  631. /// <param name="ly1">线段起点</param>
  632. /// <param name="lx2">线段终点</param>
  633. /// <param name="ly2">线段终点</param>
  634. /// <param name="cx">圆心坐标</param>
  635. /// <param name="cy">圆心坐标</param>
  636. /// <param name="Radius">半径</param>
  637. /// <returns>如果有交点返回true,否则返回false</returns>
  638. public static bool intersectLineRound(
  639. float lx1, float ly1,
  640. float lx2, float ly2,
  641. float cx, float cy,
  642. float Radius)
  643. {
  644. return CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), Radius, new Vector2(lx1, ly1), new Vector2(lx2, ly2));
  645. }
  646. /// <summary>
  647. /// 圆与胶囊线段碰撞检测
  648. /// 性能远好于 intersetctRoundStripWidth
  649. /// </summary>
  650. /// <param name="lx1">线段</param>
  651. /// <param name="ly1">线段</param>
  652. /// <param name="lx2">线段</param>
  653. /// <param name="ly2">线段</param>
  654. /// <param name="line_r">线段半径(粗度的一半)</param>
  655. /// <param name="cx">圆形</param>
  656. /// <param name="cy">圆形</param>
  657. /// <param name="r">圆形</param>
  658. /// <returns></returns>
  659. public static bool intersectRoundStripCapsule(
  660. float cx, float cy, float r,
  661. float lx1, float ly1,
  662. float lx2, float ly2,
  663. float line_r)
  664. {
  665. return CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), line_r + r, new Vector2(lx1, ly1), new Vector2(lx2, ly2));
  666. }
  667. /// <summary>
  668. /// 圆与线段碰撞检测
  669. /// 性能较差
  670. /// </summary>
  671. /// <param name="lx1">线段</param>
  672. /// <param name="ly1">线段</param>
  673. /// <param name="lx2">线段</param>
  674. /// <param name="ly2">线段</param>
  675. /// <param name="line_r">线段半径(粗度的一半)</param>
  676. /// <param name="cx">圆形</param>
  677. /// <param name="cy">圆形</param>
  678. /// <param name="r">圆形</param>
  679. /// <returns></returns>
  680. public static bool intersectRoundStripWidth(
  681. float cx, float cy, float r,
  682. float lx1, float ly1,
  683. float lx2, float ly2,
  684. float line_r)
  685. {
  686. CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
  687. CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
  688. if (CommonLang.Geometry.CollisionMath.CircleLineCollide(new Vector2(cx, cy), line_r + r, sp, dp))
  689. {
  690. float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
  691. CommonLang.Geometry.Vector2 sl = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
  692. CommonLang.Geometry.Vector2 sr = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
  693. CommonLang.Geometry.Vector2 dl = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
  694. CommonLang.Geometry.Vector2 dr = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
  695. return CMath.intersectRoundPolygon(cx, cy, r, new CommonLang.Geometry.Vector2[] { sl, sr, dr, dl, });
  696. }
  697. return false;
  698. }
  699. /// <summary>
  700. /// 矩形与粗线段相交
  701. /// </summary>
  702. /// <param name="sx1">矩形</param>
  703. /// <param name="sy1">矩形</param>
  704. /// <param name="sx2">矩形</param>
  705. /// <param name="sy2">矩形</param>
  706. /// <param name="lx1">线段</param>
  707. /// <param name="ly1">线段</param>
  708. /// <param name="lx2">线段</param>
  709. /// <param name="ly2">线段</param>
  710. /// <param name="line_r">线段半径(粗度的一半)</param>
  711. /// <returns></returns>
  712. public static bool intersectRectStripWidth(
  713. float sx1, float sy1,
  714. float sx2, float sy2,
  715. float lx1, float ly1,
  716. float lx2, float ly2,
  717. float line_r)
  718. {
  719. if (CMath.intersectRect(sx1, sy1, sx2, sy2,
  720. Math.Min(lx1, lx2),
  721. Math.Min(ly1, ly2),
  722. Math.Max(lx1, lx2),
  723. Math.Max(ly1, ly2)))
  724. {
  725. CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
  726. CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
  727. float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
  728. CommonLang.Geometry.Vector2 p0 = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
  729. CommonLang.Geometry.Vector2 p1 = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
  730. CommonLang.Geometry.Vector2 p2 = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
  731. CommonLang.Geometry.Vector2 p3 = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
  732. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p0.X, p0.Y, p1.X, p1.Y)) return true;
  733. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p1.X, p1.Y, p2.X, p2.Y)) return true;
  734. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p2.X, p2.Y, p3.X, p3.Y)) return true;
  735. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p3.X, p3.Y, p0.X, p0.Y)) return true;
  736. }
  737. return false;
  738. }
  739. /// <summary>
  740. /// 圆形与多边形碰撞
  741. /// </summary>
  742. /// <param name="cx">圆形</param>
  743. /// <param name="cy">圆形</param>
  744. /// <param name="r">圆形</param>
  745. /// <param name="list">多边形</param>
  746. /// <returns></returns>
  747. public static bool intersectRoundPolygon(float cx, float cy, float r, Vector2[] list)
  748. {
  749. if (list.Length == 1)
  750. {
  751. return CMath.includeRoundPoint(cx, cy, r, list[0].X, list[0].Y);
  752. }
  753. Vector2 center = new Vector2(cx, cy);
  754. if (CollisionMath.PointInPolygon(center, list))
  755. {
  756. return true;
  757. }
  758. for (int i = 0; i < list.Length - 1; ++i)
  759. {
  760. if (CollisionMath.CircleLineCollide(center, r, list[i], list[i + 1]))
  761. return true;
  762. }
  763. if (CollisionMath.CircleLineCollide(center, r, list[list.Length - 1], list[0]))
  764. {
  765. return true;
  766. }
  767. return false;
  768. }
  769. /// <summary>
  770. /// 矩形与多边形碰撞
  771. /// </summary>
  772. /// <param name="sx1">矩形</param>
  773. /// <param name="sy1">矩形</param>
  774. /// <param name="sx2">矩形</param>
  775. /// <param name="sy2">矩形</param>
  776. /// <param name="list">多边形</param>
  777. /// <returns></returns>
  778. public static bool intersectRectPolygon(float sx1, float sy1, float sx2, float sy2, Vector2[] list)
  779. {
  780. if (list.Length == 1)
  781. return CMath.includeRectPoint(sx1, sy1, sx2, sy2, list[0].X, list[0].Y);
  782. if (CollisionMath.PointInPolygon(new Vector2(
  783. CMath.getMiddleValue(0.5f, sx1, sx2),
  784. CMath.getMiddleValue(0.5f, sy1, sy2)),
  785. list))
  786. return true;
  787. Vector2 p = list[0];
  788. Vector2 q = list[list.Length - 1];
  789. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p.X, p.Y, q.X, q.Y))
  790. return true;
  791. for (int i = list.Length - 2; i >= 0; --i)
  792. {
  793. p = list[i];
  794. q = list[i + 1];
  795. if (CMath.intersectRectLine(sx1, sy1, sx2, sy2, p.X, p.Y, q.X, q.Y))
  796. return true;
  797. }
  798. return false;
  799. }
  800. /// <summary>
  801. /// 获得粗线段多边形表达式
  802. /// </summary>
  803. /// <param name="lx1">线段</param>
  804. /// <param name="ly1">线段</param>
  805. /// <param name="lx2">线段</param>
  806. /// <param name="ly2">线段</param>
  807. /// <param name="line_r">线段半径(粗度的一半)</param>
  808. /// <returns>多边形</returns>
  809. public static Vector2[] toStripWidthPolygon(float lx1, float ly1, float lx2, float ly2, float line_r)
  810. {
  811. Vector2[] ret = new Vector2[4];
  812. CommonLang.Geometry.Vector2 sp = new CommonLang.Geometry.Vector2(lx1, ly1);
  813. CommonLang.Geometry.Vector2 dp = new CommonLang.Geometry.Vector2(lx2, ly2);
  814. float angle = (float)Math.Atan2(ly1 - ly2, lx1 - lx2);
  815. ret[0] = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle + CMath.PI_DIV_2, line_r);
  816. ret[1] = CommonLang.Geometry.CollisionMath.MoveToByRadians(sp, angle - CMath.PI_DIV_2, line_r);
  817. ret[3] = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle + CMath.PI_DIV_2, line_r);
  818. ret[2] = CommonLang.Geometry.CollisionMath.MoveToByRadians(dp, angle - CMath.PI_DIV_2, line_r);
  819. return ret;
  820. }
  821. /// <summary>
  822. /// 获取多边形外包框
  823. /// </summary>
  824. /// <param name="list"></param>
  825. /// <param name="min"></param>
  826. /// <param name="max"></param>
  827. public static void toBoundingBox(Vector2[] list, out Vector2 min, out Vector2 max)
  828. {
  829. min = new Vector2(float.MaxValue, float.MaxValue);
  830. max = new Vector2(float.MinValue, float.MinValue);
  831. for (int i = list.Length - 1; i >= 0; --i)
  832. {
  833. if (list[i].X < min.X) { min.X = list[i].X; }
  834. if (list[i].Y < min.Y) { min.Y = list[i].Y; }
  835. if (list[i].X > max.X) { max.X = list[i].X; }
  836. if (list[i].Y > max.Y) { max.Y = list[i].Y; }
  837. }
  838. }
  839. #endregion
  840. // --------------------------------------------------------------------------------------------------
  841. /// <summary>
  842. /// max 大于等于 value 并且 min 小于等于 value
  843. /// </summary>
  844. /// <param name="value"></param>
  845. /// <param name="min"></param>
  846. /// <param name="max"></param>
  847. /// <returns></returns>
  848. public static bool isIncludeEqual(int value, int min, int max)
  849. {
  850. return max >= value && min <= value;
  851. }
  852. /// <summary>
  853. /// max 大于 value 并且 min 小于 value
  854. /// </summary>
  855. /// <param name="value"></param>
  856. /// <param name="min"></param>
  857. /// <param name="max"></param>
  858. /// <returns></returns>
  859. public static bool isInclude(int value, int min, int max)
  860. {
  861. return max > value && min < value;
  862. }
  863. /// <summary>
  864. /// value 大于等于 0 并且& value 小于 max
  865. /// </summary>
  866. /// <param name="value"></param>
  867. /// <param name="max"></param>
  868. /// <returns></returns>
  869. public static bool isInRange(int value, int max)
  870. {
  871. return value >= 0 && value < max;
  872. }
  873. static public bool IsIntersect(float sx1, float sx2, float dx1, float dx2)
  874. {
  875. if (sx2 < dx1) return false;
  876. if (sx1 > dx2) return false;
  877. return true;
  878. }
  879. static public bool IsIntersect2(float sx1, float sw, float dx1, float dw)
  880. {
  881. float sx2 = sx1 + sw;
  882. if (sx2 < dx1) return false;
  883. float dx2 = dx1 + dw;
  884. if (sx1 > dx2) return false;
  885. return true;
  886. }
  887. /// <summary>
  888. /// 将值定位于min和max之间
  889. /// </summary>
  890. /// <param name="value"></param>
  891. /// <param name="min"></param>
  892. /// <param name="max">包含</param>
  893. /// <returns></returns>
  894. public static float getInRange(
  895. float value,
  896. float min,
  897. float max)
  898. {
  899. value = Math.Min(max, value);
  900. value = Math.Max(min, value);
  901. return value;
  902. }
  903. /// <summary>
  904. /// 获得值在最大与最小之间的比率
  905. /// </summary>
  906. /// <param name="value"></param>
  907. /// <param name="min_v"></param>
  908. /// <param name="max_v"></param>
  909. /// <returns></returns>
  910. public static float getRate(
  911. float value,
  912. float min_v,
  913. float max_v)
  914. {
  915. float sw = max_v - min_v;
  916. float sx = value - min_v;
  917. return (sx / sw);
  918. }
  919. /// <summary>
  920. /// 将比率换算为实际值
  921. /// </summary>
  922. /// <param name="rate"></param>
  923. /// <param name="min"></param>
  924. /// <param name="max"></param>
  925. /// <returns></returns>
  926. public static float getMiddleValue(
  927. float rate,
  928. float min,
  929. float max)
  930. {
  931. float v = max - min;
  932. return min + v * rate;
  933. }
  934. // --------------------------------------------------------------------------------------------------
  935. public static long ccNextPOT(long x)
  936. {
  937. x = x - 1;
  938. x = x | (x >> 1);
  939. x = x | (x >> 2);
  940. x = x | (x >> 4);
  941. x = x | (x >> 8);
  942. x = x | (x >> 16);
  943. return x + 1;
  944. }
  945. public const float PI_F = (float)(Math.PI);
  946. public const float PI_FLOAT = (float)(Math.PI);
  947. /// <summary>
  948. /// Math.PI / 180
  949. /// </summary>
  950. public const float PI_DIV_180 = (float)(Math.PI / 180);
  951. /// <summary>
  952. /// Math.PI / 2
  953. /// </summary>
  954. public const float PI_DIV_2 = (float)(Math.PI / 2);
  955. /// <summary>
  956. /// Math.PI * 2
  957. /// </summary>
  958. public const float PI_MUL_2 = (float)(Math.PI * 2);
  959. /// <summary>
  960. /// 角度转弧度
  961. /// </summary>
  962. /// <param name="degrees"></param>
  963. /// <returns></returns>
  964. public static float DegreesToRadians(float degrees)
  965. {
  966. return (PI_DIV_180 * degrees);
  967. }
  968. /// <summary>
  969. /// 弧度转角度
  970. /// </summary>
  971. /// <param name="radians"></param>
  972. /// <returns></returns>
  973. public static float RadiansToDegrees(float radians)
  974. {
  975. return (radians / PI_DIV_180);
  976. }
  977. public static float ToPI(float angle)
  978. {
  979. return (angle * PI_DIV_180);
  980. }
  981. public static float To360(float angle)
  982. {
  983. return (angle / PI_DIV_180);
  984. }
  985. /// <summary>
  986. /// 将角度控制在360度范围内
  987. /// </summary>
  988. /// <param name="degrees"></param>
  989. /// <returns></returns>
  990. public static float OpitimizeDegrees(float degrees)
  991. {
  992. while (degrees > 360)
  993. {
  994. degrees -= 360;
  995. }
  996. while (degrees < 0)
  997. {
  998. degrees += 360;
  999. }
  1000. return degrees;
  1001. }
  1002. /// <summary>
  1003. /// 将弧度控制在2PI度范围内
  1004. /// </summary>
  1005. /// <param name="degrees"></param>
  1006. /// <returns></returns>
  1007. public static float OpitimizeRadians(float radians)
  1008. {
  1009. while (radians > PI_MUL_2)
  1010. {
  1011. radians -= PI_MUL_2;
  1012. }
  1013. while (radians < 0)
  1014. {
  1015. radians += PI_MUL_2;
  1016. }
  1017. return radians;
  1018. }
  1019. /// <summary>
  1020. /// 判断弧度是否在范围内
  1021. /// </summary>
  1022. /// <param name="angle"></param>
  1023. /// <param name="startAngle"></param>
  1024. /// <param name="endAngle"></param>
  1025. /// <returns></returns>
  1026. public static bool RadiansInRange(float angle, float startAngle, float endAngle)
  1027. {
  1028. startAngle = OpitimizeRadians(startAngle);
  1029. endAngle = OpitimizeRadians(endAngle);
  1030. if (endAngle < startAngle)
  1031. {
  1032. if (angle < endAngle)
  1033. {
  1034. angle += PI_MUL_2;
  1035. }
  1036. endAngle += PI_MUL_2;
  1037. }
  1038. if (angle >= startAngle && angle <= endAngle)
  1039. {
  1040. return true;
  1041. }
  1042. return false;
  1043. }
  1044. /// <summary>
  1045. /// 获取两个角度间的最小距离
  1046. /// </summary>
  1047. /// <param name="src_angle"></param>
  1048. /// <param name="dst_angle"></param>
  1049. /// <returns></returns>
  1050. public static float GetMinRadians(float src_angle, float dst_angle)
  1051. {
  1052. float dd = CMath.OpitimizeRadians(src_angle);
  1053. float cd = CMath.OpitimizeRadians(dst_angle);
  1054. float d1 = Math.Abs(dd - cd);
  1055. if (d1 > PI_F)
  1056. {
  1057. return PI_MUL_2 - d1;
  1058. }
  1059. return d1;
  1060. }
  1061. //--------------------------------------------------------------------------------------------------
  1062. public static void RandomPosInRound(Random rand, float x, float y, float r, ref Vector.Vector2 pos)
  1063. {
  1064. float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
  1065. float distance = (float)rand.NextDouble() * r;
  1066. pos.SetX(x + (float)Math.Cos(angle) * distance);
  1067. pos.SetY(y + (float)Math.Sin(angle) * distance);
  1068. }
  1069. public static void RandomPosInRound(Random rand, float x, float y, float r, out float out_x, out float out_y)
  1070. {
  1071. float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
  1072. float distance = (float)rand.NextDouble() * r;
  1073. out_x = x + (float)Math.Cos(angle) * distance;
  1074. out_y = y + (float)Math.Sin(angle) * distance;
  1075. }
  1076. public static void RandomPosInCycle(Random rand, float x, float y, float r, out float out_x, out float out_y)
  1077. {
  1078. float angle = (float)rand.NextDouble() * CMath.PI_MUL_2;
  1079. out_x = x + (float)Math.Cos(angle) * r;
  1080. out_y = y + (float)Math.Sin(angle) * r;
  1081. }
  1082. public static float RandomAngle(Random rand)
  1083. {
  1084. return (float)rand.NextDouble() * CMath.PI_MUL_2;
  1085. }
  1086. //--------------------------------------------------------------------------------------------------
  1087. public enum PointOnLineResult : byte
  1088. {
  1089. Left, Right, Touch,
  1090. }
  1091. /// <summary>
  1092. /// 输入三个点,并且判断第三个点在前两个点连成的直线的左边还是右边或者是在线上
  1093. /// </summary>
  1094. /// <param name="x0"></param>
  1095. /// <param name="y0"></param>
  1096. /// <param name="x1"></param>
  1097. /// <param name="y1"></param>
  1098. /// <param name="x2"></param>
  1099. /// <param name="y2"></param>
  1100. /// <returns></returns>
  1101. public static PointOnLineResult PointOnLine(float x0, float y0, float x1, float y1, float x2, float y2)
  1102. {
  1103. float f = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
  1104. if (f > 0)
  1105. return PointOnLineResult.Right;
  1106. if (f < 0)
  1107. return PointOnLineResult.Left;
  1108. return PointOnLineResult.Touch;
  1109. }
  1110. }
  1111. }