TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
S J
NA
171
25.4k
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
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>();
//List<Pen> pens = new List<Pen>();
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;
// Brush brush;
public
Form1()
{
InitializeComponent();
}
//class Point
//{
// public int a,b;
//}
//static bool overlap(Point l1,Point r1, Point l2, Point r2)
//{
// if (l1.a >= r2.a || l2.a >= r2.a)
// {
// return false;
// }
// if (l1.b >= r2.b || l2.b >= r2.b)
// {
// return false;
// }
// return true;
//}
//public void Intersectclip(System.Drawing.Rectangle rct);
//private void IntersectclipRectangle(PaintEventArgs e)
//{
// //Rectangle cliprectangle = new Rectangle(x, y, w, h);
// //e.Graphics.SetClip(cliprectangle);
// Rectangle intersectrect = new Rectangle(x1, y1, w1, h1);
// e.Graphics.IntersectClip(intersectrect);
// //e.Graphics.DrawRectangle(iteam.Key, iteam.Value);
//}
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);
}
//var rectdetails = from angle in rectangle
//where angle.Key= rect
//select new Rectangle(x1,y1,w1,h1);
//if(CheckIfAnyInteresect())
//{
//}
//Color clr = Color.FromName(intersectcolor);
//Brush brush = new SolidBrush(clr);
//Rectangle rect1 = new Rectangle(x, y, w, h);
//e.Graphics.SetClip(rect1);
//Rectangles.Add(rect1);
//Rectangle intersectrect = new Rectangle(x1, y1, w1, h1);
//e.Graphics.IntersectClip(intersectrect);
//e.Graphics.FillRectangle(brush, intersectrect);
//foreach(KeyValuePair<Pen,Rectangle> val in rectangle)
//{
// int n=rectangle.Count;
//}
//int left = max(r1.left, r2.left);
//int right = min(r1.right, r2.right)
//int bottom = max(r1.bottom, r2.bottom)
//int top = min(r1.top, r2.top)
//Point l1 = new Point(), r1 = new Point(),
//l2 = new Point(), r2 = new Point();
// l1.a = x; l1.b = y; r1.a = y; r1.b = x;
// l2.a = x; l2.b = y; r2.a = y; r2.b = x;
// if (overlap(l1, r1, l2, r2))
// {
// MessageBox.Show("Rectangle overlaps");
// }
// else
// {
// MessageBox.Show("Rectangle not overlaps");
// }
}
}
//static void FindPoints(int x1, int y1,
// int x2, int y2,
// int x3, int y3,
// int x4, int y4)
//{
// int x5 = Math.Max(x1, x3);
// int y5 = Math.Max(y1, y3);
//}
bool
CheckIfAnyInteresect(IEnumerable<Rectangle> rectangles)
{
return
rectangles.Any(rect => rectangles.Where(r => !r.Equals(rect)).Any(r => r.IntersectsWith(rect)));
}
//static internal Rectangle intersect(Rectangle lhs, Rectangle rhs)
//{
// //int lhsLeft = lhs.Location.X;
// //int rhsLeft = rhs.Location.X;
// //int lhsTop = lhs.Location.Y;
// //int rhsTop = rhs.Location.Y;
// //int lhsRight = lhs.Right;
// //int rhsRight = rhs.Right;
// //int lhsBottom = lhs.Bottom;
// //int rhsBottom = rhs.Bottom;
// //Dimension left = Dimension.max(lhsLeft, rhsLeft);
// //Dimension top = Dimension.max(lhsTop, rhsTop);
// //Dimension right = Dimension.min(lhsRight, rhsRight);
// //Dimension bottom = Dimension.min(lhsBottom, rhsBottom);
// //Point location = new Point(left, top);
// //Dimension width = (right > left) ? (right - left) : new Dimension(0);
// //Dimension height = (bottom > top) ? (bottom - top) : new Dimension(0);
// return new Rectangle(location, new Size(width, height));
//}
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)
{
//Graphics g = e.Graphics;
//Rectangle rect = e.Bounds;
//if (e.Index >= 0)
//{
// string n = ((ComboBox)sender).Items[e.Index].ToString();
// Font f = new Font("Arial", 9, FontStyle.Regular);
// Color c = Color.FromName(n);
// Brush b = new SolidBrush(c);
// g.DrawString(n, f, Brushes.Black,rect);
// g.FillRectangle(b, rect);
//}
}
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();
}
}
}
Reply
Answers (
1
)
How Can getting the Json Object Value In C#?
Create a C# Windows Desktop program that uses an array to store month