S J

S J

  • NA
  • 166
  • 22.3k

Check two rectangles are intersecting or not in c# windows application

Nov 11 2020 10:46 PM
hi
 
how to check two rectangles are intersecting or not....
 
I have list with how many rectangles I have drawn over panel. when I am drawing a new rectangle I need to check whether this rectangle is intersecting or not. If intersecting means that intersected area need to fill with user choose color. I tried so many ways I could not found any suitable answer for my application.i have present drawing rectangle details but I need to check with other rectangle details whether intersecting or not means I am not getting how to do. So please anyone help me this is highly preferable.
 
below i am sharing code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.Reflection;  
  9. using System.Text;  
  10. using System.Text.RegularExpressions;  
  11. using System.Threading.Tasks;  
  12. using System.Windows.Forms;  
  13. namespace DisplayRectangleBox  
  14. {  
  15. public partial class Form1 : Form  
  16. {  
  17. List<Rectangle> Rectangles = new List<Rectangle>();  
  18. //List<Pen> pens = new List<Pen>();  
  19. Dictionary<Pen, Rectangle> rectangle = new Dictionary<Pen, Rectangle>();  
  20. Pen pen1;  
  21. int x, y, w, h,x1,y1,w1,h1;  
  22. int r1, r2;  
  23. int height, width;  
  24. bool isvalid = false;  
  25. const int padding = 1;  
  26. public string color;  
  27. public string intersectcolor;  
  28. Color c ;  
  29. Graphics g;  
  30. int Dimension;  
  31. // Brush brush;  
  32. public Form1()  
  33. {  
  34. InitializeComponent();  
  35. }  
  36. //class Point  
  37. //{  
  38. // public int a,b;  
  39. //}  
  40. //static bool overlap(Point l1,Point r1, Point l2, Point r2)  
  41. //{  
  42. // if (l1.a >= r2.a || l2.a >= r2.a)  
  43. // {  
  44. // return false;  
  45. // }  
  46. // if (l1.b >= r2.b || l2.b >= r2.b)  
  47. // {  
  48. // return false;  
  49. // }  
  50. // return true;  
  51. //}  
  52. //public void Intersectclip(System.Drawing.Rectangle rct);  
  53. //private void IntersectclipRectangle(PaintEventArgs e)  
  54. //{  
  55. // //Rectangle cliprectangle = new Rectangle(x, y, w, h);  
  56. // //e.Graphics.SetClip(cliprectangle);  
  57. // Rectangle intersectrect = new Rectangle(x1, y1, w1, h1);  
  58. // e.Graphics.IntersectClip(intersectrect);  
  59. // //e.Graphics.DrawRectangle(iteam.Key, iteam.Value);  
  60. //}  
  61. private void panel1_Paint(object sender, PaintEventArgs e)  
  62. {  
  63. if (isvalid)  
  64. {  
  65. Color c = Color.FromName(color);  
  66. Pen pen1 = new Pen(c,2);  
  67. Rectangle rect = new Rectangle(x, y, w, h);  
  68. rectangle.Add(pen1,rect);  
  69. foreach (KeyValuePair<Pen, Rectangle> iteam in rectangle)  
  70. {  
  71. Graphics g = e.Graphics;  
  72. g.DrawRectangle(iteam.Key,iteam.Value);  
  73. }  
  74. //var rectdetails = from angle in rectangle  
  75. //where angle.Key= rect  
  76. //select new Rectangle(x1,y1,w1,h1);  
  77. //if(CheckIfAnyInteresect())  
  78. //{  
  79. //}  
  80. //Color clr = Color.FromName(intersectcolor);  
  81. //Brush brush = new SolidBrush(clr);  
  82. //Rectangle rect1 = new Rectangle(x, y, w, h);  
  83. //e.Graphics.SetClip(rect1);  
  84. //Rectangles.Add(rect1);  
  85. //Rectangle intersectrect = new Rectangle(x1, y1, w1, h1);  
  86. //e.Graphics.IntersectClip(intersectrect);  
  87. //e.Graphics.FillRectangle(brush, intersectrect);  
  88. //foreach(KeyValuePair<Pen,Rectangle> val in rectangle)  
  89. //{  
  90. // int n=rectangle.Count;  
  91. //}  
  92. //int left = max(r1.left, r2.left);  
  93. //int right = min(r1.right, r2.right)  
  94. //int bottom = max(r1.bottom, r2.bottom)  
  95. //int top = min(r1.top, r2.top)  
  96. //Point l1 = new Point(), r1 = new Point(),  
  97. //l2 = new Point(), r2 = new Point();  
  98. // l1.a = x; l1.b = y; r1.a = y; r1.b = x;  
  99. // l2.a = x; l2.b = y; r2.a = y; r2.b = x;  
  100. // if (overlap(l1, r1, l2, r2))  
  101. // {  
  102. // MessageBox.Show("Rectangle overlaps");  
  103. // }  
  104. // else  
  105. // {  
  106. // MessageBox.Show("Rectangle not overlaps");  
  107. // }  
  108. }  
  109. }  
  110. //static void FindPoints(int x1, int y1,  
  111. // int x2, int y2,  
  112. // int x3, int y3,  
  113. // int x4, int y4)  
  114. //{  
  115. // int x5 = Math.Max(x1, x3);  
  116. // int y5 = Math.Max(y1, y3);  
  117. //}  
  118. bool CheckIfAnyInteresect(IEnumerable<Rectangle> rectangles)  
  119. {  
  120. return rectangles.Any(rect => rectangles.Where(r => !r.Equals(rect)).Any(r => r.IntersectsWith(rect)));  
  121. }  
  122. //static internal Rectangle intersect(Rectangle lhs, Rectangle rhs)  
  123. //{  
  124. // //int lhsLeft = lhs.Location.X;  
  125. // //int rhsLeft = rhs.Location.X;  
  126. // //int lhsTop = lhs.Location.Y;  
  127. // //int rhsTop = rhs.Location.Y;  
  128. // //int lhsRight = lhs.Right;  
  129. // //int rhsRight = rhs.Right;  
  130. // //int lhsBottom = lhs.Bottom;  
  131. // //int rhsBottom = rhs.Bottom;  
  132. // //Dimension left = Dimension.max(lhsLeft, rhsLeft);  
  133. // //Dimension top = Dimension.max(lhsTop, rhsTop);  
  134. // //Dimension right = Dimension.min(lhsRight, rhsRight);  
  135. // //Dimension bottom = Dimension.min(lhsBottom, rhsBottom);  
  136. // //Point location = new Point(left, top);  
  137. // //Dimension width = (right > left) ? (right - left) : new Dimension(0);  
  138. // //Dimension height = (bottom > top) ? (bottom - top) : new Dimension(0);  
  139. // return new Rectangle(location, new Size(width, height));  
  140. //}  
  141. private bool Validate()  
  142. {  
  143. if (string.IsNullOrEmpty(txtX.Text) || string.IsNullOrEmpty(txtY.Text) || string.IsNullOrEmpty(txtWidth.Text) || string.IsNullOrEmpty(txtHeight.Text))  
  144. {  
  145. MessageBox.Show("Please fill all the feilds ");  
  146. }  
  147. else  
  148. {  
  149. x = Convert.ToInt32(txtX.Text);  
  150. y = Convert.ToInt32(txtY.Text);  
  151. w = Convert.ToInt32(txtWidth.Text);  
  152. h = Convert.ToInt32(txtHeight.Text);  
  153. height = panel1.Height - y - padding;  
  154. width = panel1.Width - x - padding;  
  155. if (w > width || h > height)  
  156. {  
  157. h = Math.Min(h, height);  
  158. w = Math.Min(w, width);  
  159. string message = ("The given size and location are not fit to this area, so we are adjusting rectangle withine drawing area.If you want to draw press 'OK'");  
  160. string title = ("Please Confirm Your Action");  
  161. MessageBoxButtons buttons = MessageBoxButtons.OKCancel;  
  162. DialogResult result = MessageBox.Show(message, title, buttons);  
  163. if (result == DialogResult.OK)  
  164. {  
  165. Graphics g = panel1.CreateGraphics();  
  166. g.DrawRectangle(Pens.Black, x, y, w, h);  
  167. txtHeight.Text = Convert.ToString(height);  
  168. txtWidth.Text = Convert.ToString(width);  
  169. panel1.Invalidate();  
  170. }  
  171. else if (result == DialogResult.Cancel)  
  172. {  
  173. isvalid = false;  
  174. return isvalid;  
  175. }  
  176. }  
  177. }  
  178. isvalid = true;  
  179. return isvalid;  
  180. }  
  181. private void panel2_SizeChanged(object sender, EventArgs e)  
  182. {  
  183. var w = Math.Max(panel1.Width, panel2.Width);  
  184. var h = Math.Max(panel1.Height, panel2.Height);  
  185. panel1.MinimumSize = new Size(w, h);  
  186. }  
  187. private void ClearForm()  
  188. {  
  189. rectangle.Clear();  
  190. cmbBoarderclr.Items.Clear();  
  191. cmbIntersectcolor.Items.Clear();  
  192. txtX.Text = "";  
  193. txtY.Text = "";  
  194. txtWidth.Text = "";  
  195. txtHeight.Text = "";  
  196. cmbBoarderclr.Text = "";  
  197. isvalid = false;  
  198. panel1.Invalidate();  
  199. txtX.Focus();  
  200. }  
  201. private void btnAdd_Click(object sender, EventArgs e)  
  202. {  
  203. if (Validate())  
  204. {  
  205. panel1.Invalidate();  
  206. }  
  207. }  
  208. private void btnReset_Click_1(object sender, EventArgs e)  
  209. {  
  210. ClearForm();  
  211. }  
  212. private void txtX_KeyPress(object sender, KeyPressEventArgs e)  
  213. {  
  214. if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))  
  215. e.Handled = true;  
  216. }  
  217. private void txtY_KeyPress(object sender, KeyPressEventArgs e)  
  218. {  
  219. if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))  
  220. e.Handled = true;  
  221. }  
  222. private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)  
  223. {  
  224. if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))  
  225. e.Handled = true;  
  226. }  
  227. private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)  
  228. {  
  229. if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))  
  230. e.Handled = true;  
  231. }  
  232. private void cmbBoarderclr_SelectedIndexChanged(object sender, EventArgs e)  
  233. {  
  234. color = this.cmbBoarderclr.SelectedItem.ToString();  
  235. }  
  236. private void cmbBoarderclr_DrawItem(object sender, DrawItemEventArgs e)  
  237. {  
  238. //Graphics g = e.Graphics;  
  239. //Rectangle rect = e.Bounds;  
  240. //if (e.Index >= 0)  
  241. //{  
  242. // string n = ((ComboBox)sender).Items[e.Index].ToString();  
  243. // Font f = new Font("Arial", 9, FontStyle.Regular);  
  244. // Color c = Color.FromName(n);  
  245. // Brush b = new SolidBrush(c);  
  246. // g.DrawString(n, f, Brushes.Black,rect);  
  247. // g.FillRectangle(b, rect);  
  248. //}  
  249. }  
  250. private void cmbBoarderclr_Click(object sender, EventArgs e)  
  251. {  
  252. Type colorType = typeof(System.Drawing.Color);  
  253. PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);  
  254. foreach (PropertyInfo c in propInfoList)  
  255. {  
  256. this.cmbBoarderclr.Items.Add(c.Name);  
  257. }  
  258. }  
  259. private void Form1_Load(object sender, EventArgs e)  
  260. {  
  261. }  
  262. private void cmbIntersectcolor_Click(object sender, EventArgs e)  
  263. {  
  264. Type colorType = typeof(System.Drawing.Color);  
  265. PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);  
  266. foreach (PropertyInfo c in propInfoList)  
  267. {  
  268. this.cmbIntersectcolor.Items.Add(c.Name);  
  269. }  
  270. }  
  271. private void cmbIntersectcolor_SelectedIndexChanged(object sender, EventArgs e)  
  272. {  
  273. intersectcolor = this.cmbIntersectcolor.SelectedItem.ToString();  
  274. }  
  275. }  
  276. } 

Answers (1)