FormCollision.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using CommonAI.RTS;
  2. using CommonLang.Vector;
  3. using CommonLang;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using CommonFroms.Drawing;
  13. namespace GameEditorPlugin.Tools
  14. {
  15. public partial class FormCollision : Form
  16. {
  17. public enum TestType
  18. {
  19. RoundLine,
  20. RoundLineW,
  21. RoundRect,
  22. LineLine,
  23. RoundInclude,
  24. FanRound,
  25. }
  26. private TestType test = TestType.RoundLine;
  27. private Random random = new Random();
  28. private Color pen1 = Color.FromArgb(0xFF, 0xFF, 0, 0);
  29. private Color pen2 = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);
  30. private CommonLang.Vector.Round s_round = new CommonLang.Vector.Round();
  31. private CommonLang.Vector.Line2 s_line = new CommonLang.Vector.Line2();
  32. private CommonLang.Vector.Fan s_fan = new CommonLang.Vector.Fan();
  33. private CommonLang.Vector.Line2 mouse_line = new CommonLang.Vector.Line2();
  34. private float line_r = 50;
  35. public FormCollision()
  36. {
  37. InitializeComponent();
  38. combo_TestType.Text = test.ToString();
  39. foreach (object obj in Enum.GetValues(typeof(TestType)))
  40. {
  41. combo_TestType.Items.Add(obj.ToString());
  42. }
  43. reset();
  44. // {
  45. // s_round.x = 58.73995f;
  46. // s_round.y = 148.076263f;
  47. // s_round.r = 1f;
  48. //
  49. // mouse_line.p.x = 61.67477f;
  50. // mouse_line.p.y = 148.0653f;
  51. // mouse_line.q.x = 51.67484f;
  52. // mouse_line.q.y = 148.102631f;
  53. //
  54. // line_r = 0.15f;
  55. // }
  56. }
  57. private void btn_Reset_Click(object sender, EventArgs e)
  58. {
  59. reset();
  60. pictureBox1.Refresh();
  61. }
  62. private void reset()
  63. {
  64. int w = pictureBox1.Width;
  65. int h = pictureBox1.Height;
  66. s_round.x = random.Next(200, w - 400);
  67. s_round.y = random.Next(200, h - 400);
  68. s_round.r = random.Next(10, 200);
  69. s_line.p.SetX(random.Next(100, w - 200));
  70. s_line.p.SetY(random.Next(100, h - 200));
  71. s_line.q.SetX(random.Next(100, w - 200));
  72. s_line.q.SetY(random.Next(100, h - 200));
  73. s_fan.x = random.Next(200, w - 400);
  74. s_fan.y = random.Next(200, h - 400);
  75. s_fan.r = random.Next(10, 200);
  76. s_fan.direction = ((float)random.NextDouble() * CMath.PI_MUL_2);
  77. s_fan.angle_range = ((float)random.NextDouble() * CMath.PI_F);
  78. line_r = random.Next(10, 100);
  79. }
  80. private void combo_TestType_TextChanged(object sender, EventArgs e)
  81. {
  82. TestType value;
  83. if (Enum.TryParse<TestType>(combo_TestType.Text, out value))
  84. {
  85. test = value;
  86. reset();
  87. pictureBox1.Refresh();
  88. }
  89. }
  90. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  91. {
  92. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  93. {
  94. mouse_line.q.SetX(e.X);
  95. mouse_line.q.SetY(e.Y);
  96. pictureBox1.Refresh();
  97. }
  98. }
  99. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  100. {
  101. pictureBox1.Focus();
  102. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  103. {
  104. mouse_line.p.SetX(e.X);
  105. mouse_line.p.SetY(e.Y);
  106. mouse_line.q.SetX(e.X);
  107. mouse_line.q.SetY(e.Y);
  108. pictureBox1.Refresh();
  109. }
  110. }
  111. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  112. {
  113. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  114. {
  115. mouse_line.q.SetX(e.X);
  116. mouse_line.q.SetY(e.Y);
  117. pictureBox1.Refresh();
  118. }
  119. }
  120. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  121. {
  122. e.Graphics.FillRectangle(new SolidBrush(Color.Black), pictureBox1.DisplayRectangle);
  123. switch (test)
  124. {
  125. case TestType.RoundLine:
  126. drawRoundLine(e.Graphics);
  127. break;
  128. case TestType.RoundLineW:
  129. drawRoundLineW(e.Graphics);
  130. break;
  131. case TestType.RoundRect:
  132. drawRectRound(e.Graphics);
  133. break;
  134. case TestType.LineLine:
  135. drawLineLine(e.Graphics);
  136. break;
  137. case TestType.RoundInclude:
  138. drawRoundInclude(e.Graphics);
  139. break;
  140. case TestType.FanRound:
  141. drawFanRound(e.Graphics);
  142. break;
  143. }
  144. }
  145. private bool intersetctLineLine()
  146. {
  147. return CMath.intersectLine(s_line.p.X, s_line.p.Y, s_line.q.X, s_line.q.Y, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  148. }
  149. private bool intersectRectRound()
  150. {
  151. float sx = Math.Min(mouse_line.p.X, mouse_line.q.X);
  152. float sy = Math.Min(mouse_line.p.Y, mouse_line.q.Y);
  153. float dx = Math.Max(mouse_line.p.X, mouse_line.q.X);
  154. float dy = Math.Max(mouse_line.p.Y, mouse_line.q.Y);
  155. return CMath.intersectRectRound(sx, sy, dx, dy, s_round.x, s_round.y, s_round.r);
  156. }
  157. private bool intersetctRoundLine()
  158. {
  159. return CMath.intersectRoundStripCapsule(s_round.x, s_round.y, s_round.r, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y, line_r);
  160. }
  161. private bool intersetctRoundLineW()
  162. {
  163. return CMath.intersectRoundStripWidth(s_round.x, s_round.y, s_round.r, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y, line_r);
  164. }
  165. private bool includeRound()
  166. {
  167. float dr = CMath.getDistance(mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  168. return CMath.includeRound2(s_round.x, s_round.y, s_round.r, mouse_line.p.X, mouse_line.p.Y, dr);
  169. }
  170. private bool intersectFanRound()
  171. {
  172. return CMath.intersectFanRound(
  173. s_fan.x, s_fan.y, s_fan.r,
  174. mouse_line.q.X, mouse_line.q.Y, line_r,
  175. s_fan.direction - s_fan.angle_range / 2,
  176. s_fan.direction + s_fan.angle_range / 2);
  177. }
  178. private void drawLineLine(Graphics g)
  179. {
  180. Color color = intersetctLineLine() ? pen2 : pen1;
  181. // sline
  182. {
  183. Pen pen = new Pen(color);
  184. g.DrawLine(pen, s_line.p.X, s_line.p.Y, s_line.q.X, s_line.q.Y);
  185. }
  186. // line move
  187. {
  188. Pen pen = new Pen(color);
  189. g.DrawLine(pen, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  190. }
  191. }
  192. private void drawRectRound(Graphics g)
  193. {
  194. Color color = intersectRectRound() ? pen2 : pen1;
  195. // round
  196. {
  197. SolidBrush bursh = new SolidBrush(color);
  198. g.FillEllipse(bursh, s_round.x - s_round.r, s_round.y - s_round.r, s_round.r * 2f, s_round.r * 2f);
  199. }
  200. {
  201. SolidBrush bursh = new SolidBrush(color);
  202. float sx = Math.Min(mouse_line.p.X, mouse_line.q.X);
  203. float sy = Math.Min(mouse_line.p.Y, mouse_line.q.Y);
  204. float dx = Math.Max(mouse_line.p.X, mouse_line.q.X);
  205. float dy = Math.Max(mouse_line.p.Y, mouse_line.q.Y);
  206. g.FillRectangle(bursh, sx, sy, dx - sx, dy - sy);
  207. }
  208. }
  209. private void drawRoundLine(Graphics g)
  210. {
  211. Color color = intersetctRoundLine() ? pen2 : pen1;
  212. // round
  213. {
  214. SolidBrush bursh = new SolidBrush(color);
  215. g.FillEllipse(bursh, s_round.x - s_round.r, s_round.y - s_round.r, s_round.r * 2f, s_round.r * 2f);
  216. }
  217. // line move
  218. {
  219. SolidBrush bursh = new SolidBrush(color);
  220. g.FillEllipse(bursh, mouse_line.p.X - line_r, mouse_line.p.Y - line_r, line_r * 2f, line_r * 2f);
  221. g.FillEllipse(bursh, mouse_line.q.X - line_r, mouse_line.q.Y - line_r, line_r * 2f, line_r * 2f);
  222. Pen pen = new Pen(color);
  223. pen.Width = line_r * 2f;
  224. g.DrawLine(pen, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  225. }
  226. }
  227. private void drawRoundLineW(Graphics g)
  228. {
  229. Color color = intersetctRoundLineW() ? pen2 : pen1;
  230. // round
  231. {
  232. SolidBrush bursh = new SolidBrush(color);
  233. g.FillEllipse(bursh, s_round.x - s_round.r, s_round.y - s_round.r, s_round.r * 2f, s_round.r * 2f);
  234. }
  235. // line move
  236. {
  237. Pen pen = new Pen(color);
  238. pen.Width = line_r * 2f;
  239. g.DrawLine(pen, mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  240. }
  241. }
  242. private void drawRoundInclude(Graphics g)
  243. {
  244. Color color = includeRound() ? pen2 : pen1;
  245. // round
  246. {
  247. Pen pen = new Pen(color);
  248. g.DrawEllipse(pen, s_round.x - s_round.r, s_round.y - s_round.r, s_round.r * 2f, s_round.r * 2f);
  249. }
  250. // line move
  251. {
  252. Pen pen = new Pen(color);
  253. float dr = CMath.getDistance(mouse_line.p.X, mouse_line.p.Y, mouse_line.q.X, mouse_line.q.Y);
  254. g.DrawEllipse(pen, mouse_line.p.X - dr, mouse_line.p.Y - dr, dr * 2, dr * 2);
  255. }
  256. }
  257. private void drawFanRound(Graphics g)
  258. {
  259. Color color = intersectFanRound() ? pen2 : pen1;
  260. // fan
  261. {
  262. SolidBrush bursh = new SolidBrush(color);
  263. g.FillPie(bursh,
  264. s_fan.x - s_fan.r,
  265. s_fan.y - s_fan.r,
  266. s_fan.r * 2,
  267. s_fan.r * 2,
  268. CMath.RadiansToDegrees(CMath.PI_MUL_2 + s_fan.direction - s_fan.angle_range / 2),
  269. CMath.RadiansToDegrees(s_fan.angle_range));
  270. }
  271. // round
  272. {
  273. SolidBrush bursh = new SolidBrush(color);
  274. g.FillEllipse(bursh, mouse_line.q.X - line_r, mouse_line.q.Y - line_r, line_r * 2f, line_r * 2f);
  275. }
  276. }
  277. }
  278. }