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
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DisplayRectangleBox
- {
- public partial class Form1 : Form
- {
- List<Rectangle> Rectangles = new List<Rectangle>();
-
- Dictionary<Pen, Rectangle> rectangle = new Dictionary<Pen, Rectangle>();
- Pen pen1;
- int x, y, w, h,x1,y1,w1,h1;
- int r1, r2;
- int height, width;
- bool isvalid = false;
- const int padding = 1;
- public string color;
- public string intersectcolor;
- Color c ;
- Graphics g;
- int Dimension;
-
- public Form1()
- {
- InitializeComponent();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- if (isvalid)
- {
- Color c = Color.FromName(color);
- Pen pen1 = new Pen(c,2);
- Rectangle rect = new Rectangle(x, y, w, h);
- rectangle.Add(pen1,rect);
- foreach (KeyValuePair<Pen, Rectangle> iteam in rectangle)
- {
- Graphics g = e.Graphics;
- g.DrawRectangle(iteam.Key,iteam.Value);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
-
-
-
-
-
-
-
-
- bool CheckIfAnyInteresect(IEnumerable<Rectangle> rectangles)
- {
- return rectangles.Any(rect => rectangles.Where(r => !r.Equals(rect)).Any(r => r.IntersectsWith(rect)));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private bool Validate()
- {
- if (string.IsNullOrEmpty(txtX.Text) || string.IsNullOrEmpty(txtY.Text) || string.IsNullOrEmpty(txtWidth.Text) || string.IsNullOrEmpty(txtHeight.Text))
- {
- MessageBox.Show("Please fill all the feilds ");
- }
- else
- {
- x = Convert.ToInt32(txtX.Text);
- y = Convert.ToInt32(txtY.Text);
- w = Convert.ToInt32(txtWidth.Text);
- h = Convert.ToInt32(txtHeight.Text);
- height = panel1.Height - y - padding;
- width = panel1.Width - x - padding;
- if (w > width || h > height)
- {
- h = Math.Min(h, height);
- w = Math.Min(w, width);
- 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'");
- string title = ("Please Confirm Your Action");
- MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
- DialogResult result = MessageBox.Show(message, title, buttons);
- if (result == DialogResult.OK)
- {
- Graphics g = panel1.CreateGraphics();
- g.DrawRectangle(Pens.Black, x, y, w, h);
- txtHeight.Text = Convert.ToString(height);
- txtWidth.Text = Convert.ToString(width);
- panel1.Invalidate();
- }
- else if (result == DialogResult.Cancel)
- {
- isvalid = false;
- return isvalid;
- }
- }
- }
- isvalid = true;
- return isvalid;
- }
- private void panel2_SizeChanged(object sender, EventArgs e)
- {
- var w = Math.Max(panel1.Width, panel2.Width);
- var h = Math.Max(panel1.Height, panel2.Height);
- panel1.MinimumSize = new Size(w, h);
- }
- private void ClearForm()
- {
- rectangle.Clear();
- cmbBoarderclr.Items.Clear();
- cmbIntersectcolor.Items.Clear();
- txtX.Text = "";
- txtY.Text = "";
- txtWidth.Text = "";
- txtHeight.Text = "";
- cmbBoarderclr.Text = "";
- isvalid = false;
- panel1.Invalidate();
- txtX.Focus();
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- if (Validate())
- {
- panel1.Invalidate();
- }
- }
- private void btnReset_Click_1(object sender, EventArgs e)
- {
- ClearForm();
- }
- private void txtX_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
- e.Handled = true;
- }
- private void txtY_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
- e.Handled = true;
- }
- private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
- e.Handled = true;
- }
- private void txtWidth_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
- e.Handled = true;
- }
- private void cmbBoarderclr_SelectedIndexChanged(object sender, EventArgs e)
- {
- color = this.cmbBoarderclr.SelectedItem.ToString();
- }
- private void cmbBoarderclr_DrawItem(object sender, DrawItemEventArgs e)
- {
-
-
-
-
-
-
-
-
-
-
-
- }
- private void cmbBoarderclr_Click(object sender, EventArgs e)
- {
- Type colorType = typeof(System.Drawing.Color);
- PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
- foreach (PropertyInfo c in propInfoList)
- {
- this.cmbBoarderclr.Items.Add(c.Name);
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void cmbIntersectcolor_Click(object sender, EventArgs e)
- {
- Type colorType = typeof(System.Drawing.Color);
- PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
- foreach (PropertyInfo c in propInfoList)
- {
- this.cmbIntersectcolor.Items.Add(c.Name);
- }
- }
- private void cmbIntersectcolor_SelectedIndexChanged(object sender, EventArgs e)
- {
- intersectcolor = this.cmbIntersectcolor.SelectedItem.ToString();
- }
- }
- }