.NET

## .NET

Valued Contributor
Posts: 95
Registered: ‎04-04-2012
Message 1 of 9 (515 Views)

# Efficient way to determine whether an area intersecting another area?

515 Views, 8 Replies
04-14-2013 05:31 PM

Hi, I need to determine whether an area intersects with another area.

For example, as you can see the picture below...

The left case can be easily determined by Polyline.IntersectWith(Polyline...).

But in the right case, the two polygons intersect/overlap each other while polylines do not intersect.

What would be the effiicient way to determine the right case polygons are intersecting polygons?

If someone can help me with a simple sample code (preferably in C#), it would be very appreciated.

Thank you very much in advance.

(FYI, I am currently using ObjectARX 2007.)

*Expert Elite*
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 9 (493 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-14-2013 11:16 PM in reply to: dynamicscope

Not sure about if this will be works in 2007

tested on A2010:

```        [CommandMethod("care")]
public void checkAreasIntersect()
{

Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
bool yeah=false;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{

ObjectId id1 = ObjectId.Null;
ObjectId id2 = ObjectId.Null;

PromptEntityOptions peo = new PromptEntityOptions("\nSelect first area >>");
peo.SetRejectMessage("\nSelect Polyline only");
peo.AddAllowedClass(typeof(Polyline), false);
PromptEntityResult res;
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK) return;
id1 = res.ObjectId;

peo.Message = "\nSelect second area >>";
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK) return;
id2 = res.ObjectId;

Entity en1 = tr.GetObject(id1, OpenMode.ForWrite) as Entity;
if (en1 == null) return;

Entity en2 = tr.GetObject(id2, OpenMode.ForRead) as Entity;
if (en1 == null) return;

DBObjectCollection exids = new DBObjectCollection();
en1.Explode(exids);

foreach (DBObject obj in exids)
{
Entity en = obj as Entity;
Point3dCollection pts= new Point3dCollection();

en.IntersectWith(en2, Intersect.OnBothOperands, pts, 0, 0);
if (pts.Count != 0) {  yeah=true;break;  }

}
ed.WriteMessage("\nAreas were {0} an intersection", yeah ? "has" : "doesn't have");
}

}```

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
Posts: 95
Registered: ‎04-04-2012
Message 3 of 9 (479 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-15-2013 01:17 AM in reply to: Hallex

Thank you, Hallex.

I tried with the code, but it couldn't handle the right case.

So, I changed the code a little bit and tried again.

```//en.IntersectWith(en2, Intersect.OnBothOperands, pts, 0, 0);
en.IntersectWith(en2, Intersect.ExtendThis, pts, 0, 0);```

Well, this could handle the right case but also creates another issue.

It will consider the following case as the intersecting polygons as well.

*Expert Elite*
Posts: 2,069
Registered: ‎04-29-2006
Message 4 of 9 (476 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-15-2013 01:23 AM in reply to: dynamicscope

Hi,

What about creating regions from the polylines, operate a boolean union and compare the region area with the sum of the polylines area ?

Gilles Chanteau
Valued Contributor
Posts: 95
Registered: ‎04-04-2012
Message 5 of 9 (465 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-15-2013 03:07 AM in reply to: _gile
That looks working. I will try it out. Thank you.
Valued Mentor
Posts: 370
Registered: ‎05-06-2012
Message 6 of 9 (447 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-15-2013 09:44 AM in reply to: _gile
Works if the polylines aren't self-intersecting, and you probably can just check for inner-loops in that case
Valued Contributor
Posts: 95
Registered: ‎04-04-2012
Message 7 of 9 (404 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-17-2013 07:14 PM in reply to: _gile
Do you know if there is any method that is related to Union command? I need to compute the area of intersecting region. So I am planning to do Region1 + Region2 - UnionRegion = IntersectingArea. I need to do this programmatically (no user interaction).
*Expert Elite*
Posts: 2,069
Registered: ‎04-29-2006
Message 8 of 9 (400 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-17-2013 10:21 PM in reply to: dynamicscope

Hi,

Look at the Region.BooleanOperation() method.

Gilles Chanteau
Valued Contributor
Posts: 95
Registered: ‎04-04-2012
Message 9 of 9 (394 Views)

# Re: Efficient way to determine whether an area intersecting another area?

04-17-2013 10:27 PM in reply to: _gile
Great~!

### You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

### Need installation help?

Start with some of our most frequented solutions to get help installing your software.

### Maintenance Subscription Resources

Upgrading to a 2015 product? Make sure to check these out 1st!