Applies to: Nevron Diagram for .NET
How to check whether 2 arbitrary shapes intersect/overlap in the Diagram?
In Nevron Diagram for .NET, you may need to know if a shape is intersecting another shape. For example, if you draw a circle (or other) shape, you may need to determine if another shape is intersecting the circle shape.
In order to check whether two arbitrary shapes intersect you can use the Region class. Following are 2 helper methods that can assist you with this:
[C#]
private
static
Region GetRegion(NShape shape)
{
if
(shape
is
NPrimitiveShape)
{
// This is a primitive shape -> is has only one primitive
NPathPrimitive pathPrimitive = ((NPrimitiveShape)shape).Primitive
as
NPathPrimitive;
if
(pathPrimitive !=
null
)
{
return
new
Region(pathPrimitive.Path);
}
}
else
if
(shape
is
NCompositeShape)
{
// This is a composite shape, so loop through its primitives to create a region
Region region =
new
Region();
NNodeList pathPrimitives = ((NCompositeShape)shape).Primitives.Children(NFilters.TypeNPathPrimitive);
for
(
int
i = 0, count = pathPrimitives.Count; i < count; i++)
{
NPathPrimitive pathPrimitive = (NPathPrimitive)pathPrimitives[i];
region.Union(pathPrimitive.Path);
}
return
region;
}
return
null
;
}
private
static
bool
Intersects(NShape shape1, NShape shape2, Graphics g)
{
if
(shape1 ==
null
|| shape2 ==
null
)
return
false
;
Region region1 = GetRegion(shape1);
if
(region1 ==
null
|| region1.IsEmpty(g))
return
false
;
Region region2 = GetRegion(shape2);
if
(region2 ==
null
|| region2.IsEmpty(g))
return
false
;
region1.Intersect(region2);
return
region1 !=
null
&& region1.IsEmpty(g) ==
false
;
}
[VB.NET]
Private
Shared
Function
GetRegion(shape
As
NShape)
As
Region
If
TypeOf
shape
Is
NPrimitiveShape
Then
' This is a primitive shape -> is has only one primitive
Dim
pathPrimitive
As
NPathPrimitive = TryCast(
DirectCast
(shape, NPrimitiveShape).Primitive, NPathPrimitive)
If
pathPrimitive IsNot
Nothing
Then
Return
New
Region(pathPrimitive.Path)
End
If
ElseIf
TypeOf
shape
Is
NCompositeShape
Then
' This is a composite shape, so loop through its primitives to create a region
Dim
region
As
New
Region()
Dim
pathPrimitives
As
NNodeList =
DirectCast
(shape, NCompositeShape).Primitives.Children(NFilters.TypeNPathPrimitive)
Dim
i
As
Integer
= 0, count
As
Integer
= pathPrimitives.Count
While
i < count
Dim
pathPrimitive
As
NPathPrimitive =
DirectCast
(pathPrimitives(i), NPathPrimitive)
region.Union(pathPrimitive.Path)
i += 1
End
While
Return
region
End
If
Return
Nothing
End
Function
Private
Shared
Function
Intersects(shape1
As
NShape, shape2
As
NShape, g
As
Graphics)
As
Boolean
If
shape1
Is
Nothing
OrElse
shape2
Is
Nothing
Then
Return
False
End
If
Dim
region1
As
Region = GetRegion(shape1)
If
region1
Is
Nothing
OrElse
region1.IsEmpty(g)
Then
Return
False
End
If
Dim
region2
As
Region = GetRegion(shape2)
If
region2
Is
Nothing
OrElse
region2.IsEmpty(g)
Then
Return
False
End
If
region1.Intersect(region2)
Return
region1 IsNot
Nothing
AndAlso
region1.IsEmpty(g) =
False
End
Function
Simply call the Intersects method to check whether the two shapes intersect or not.
Article ID: 233, Created On: 1/25/2013, Modified: 1/25/2013