[Visual Basic 6.0]
Option Explicit
'This example demonstrates all of the predefined relational operators.
Public Sub RelationalOperatorsDemo()
Dim pLine1 As ILine
Dim pLine2 As ILine
Dim pBase As ISegmentCollection
Dim pComparison As ISegmentCollection
Set pBase = New Polyline
Set pComparison = New Polyline
Dim pRelOp As IRelationalOperator
Set pRelOp = pBase
Dim pBaseGeom As IGeometry
Dim pCompGeom As IGeometry
Set pBaseGeom = pBase
Set pCompGeom = pComparison
Dim pPoint1 As IPoint
Dim pPoint2 As IPoint
Dim pPoints(0 To 1) As IPoint
Set pLine1 = New Line
Set pLine2 = New Line
Set pPoint1 = New Point
Set pPoint2 = New Point
Set pPoints(0) = New Point
Set pPoints(1) = New Point
'General Relational Operator Example. Crosses is true, all others are false.
'Put points using PutCoords and by setting X and Y values individually
pPoint1.PutCoords 0, 0
pPoint2.X = 100
pPoint2.Y = 100
pLine1.PutCoords pPoint1, pPoint2
'Put points using an array of points
pPoints(0).PutCoords 0, 100
pPoints(1).PutCoords 100, 0
pLine2.PutCoords pPoints(0), pPoints(1)
'Wrap lines in a polyline which supports the IRelationalOperator interface
pBase.AddSegment pLine1 'Base polyline
pComparison.AddSegment pLine2 'Comparison polyline
'Note: Both the Base and Comparison geometries must support
' the IRelationalOperator interface.
'If the Base contains the Comparison
MsgBox "Contains: " & pRelOp.Contains(pComparison)
'If the Base crosses the Comparison
MsgBox "Crosses: " & pRelOp.Crosses(pComparison)
'If the Base and the Comparison are disjoint
MsgBox "Disjoint: " & pRelOp.Disjoint(pComparison)
'If the Base and the Comparison are equal
MsgBox "Equals: " & pRelOp.Equals(pComparison)
'If the Base and the Comparison overlap
MsgBox "Overlaps: " & pRelOp.Overlaps(pComparison)
'If the Base and the Comparison touch
MsgBox "Touches: " & pRelOp.Touches(pComparison)
'If the Base is within the Comparison
MsgBox "Within: " & pRelOp.Within(pComparison)
'Set polylines empty so they can be reused
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Contains
pPoints(0).PutCoords 20, 20
pPoints(1).PutCoords 80, 80
pLine2.PutCoords pPoints(0), pPoints(1)
pBase.AddSegment pLine1 '(0,0) to (100,100)
pComparison.AddSegment pLine2 '(20,20) to (80,80)
If pRelOp.Contains(pComparison) Then
MsgBox "Contained"
Else
MsgBox "Not Contained"
End If
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Within (Within is the complement of Contains)
pBase.AddSegment pLine2 '(20,20) to (80,80)
pComparison.AddSegment pLine1 '(0,0) to (100,100)
MsgBox "Within: " & pRelOp.Within(pComparison)
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Equals
pPoints(0).PutCoords 0, 0
pPoints(1).PutCoords 100, 100
pLine2.PutCoords pPoints(0), pPoints(1)
pBase.AddSegment pLine1 '(0,0) to (100,100)
pComparison.AddSegment pLine2 '(0,0) to (100,100)
'If Equals is True, then Within and Contains should also be true
If pRelOp.Equals(pComparison) Then
MsgBox "Equals is true"
MsgBox "Within: " & pRelOp.Within(pComparison) & vbCrLf & _
"Contains: " & pRelOp.Contains(pComparison)
End If
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Disjoint
pPoints(0).PutCoords -40, 40
pPoints(1).PutCoords -100, 80
pLine2.PutCoords pPoints(0), pPoints(1)
pBase.AddSegment pLine1 '(0,0) to (100,100)
pComparison.AddSegment pLine2 '(-40,40) to (-100,80)
MsgBox "Disjoint: " & pRelOp.Disjoint(pComparison)
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Touches (Objects touch on their boundaries)
pPoints(0).PutCoords 100, 100
pPoints(1).PutCoords 200, 150
pLine2.PutCoords pPoints(0), pPoints(1)
pBase.AddSegment pLine1 '(0,0) to (100,100)
pComparison.AddSegment pLine2 '(100,100) to (200,150)
MsgBox "Touches: " & pRelOp.Touches(pComparison)
pBaseGeom.SetEmpty
pCompGeom.SetEmpty
'Example of Overlaps
pPoints(0).PutCoords 140, 140
pPoints(1).PutCoords 60, 60
pLine2.PutCoords pPoints(0), pPoints(1)
pBase.AddSegment pLine1 '(0,0) to (100,100)
pComparison.AddSegment pLine2 '(140,140) to (60,60)
MsgBox "Overlaps: " & pRelOp.Overlaps(pComparison)
End Sub
[C#]//This example demonstrates all of the predefined relational operators. public void DemoRelationalOperators() { //General Relational Operator Example. Crosses is true, all others are false. //define first line IPoint point1 = new PointClass(); point1.PutCoords(0, 0); IPoint point2 = new PointClass(); point2.PutCoords(100, 100); ILine line1 = new LineClass(); line1.PutCoords(point1, point2); //define second line IPoint point3 = new PointClass(); point3.PutCoords(0, 100); IPoint point4 = new PointClass(); point4.PutCoords(100, 0); ILine line2 = new LineClass(); line2.PutCoords(point3, point4); //Wrap lines in a polyline which supports the IRelationalOperator interface ISegmentCollection basePolylineSegments = new PolylineClass(); object missing = Type.Missing; basePolylineSegments.AddSegment(line1 as ISegment, ref missing, ref missing); ISegmentCollection comparisonPolylineSegments = new PolylineClass(); IGeometry comparisonGeometry = comparisonPolylineSegments as IGeometry; comparisonPolylineSegments.AddSegment(line2 as ISegment, ref missing, ref missing); //Note: Both the Base and Comparison geometries must support // the IRelationalOperator interface. IRelationalOperator relationalOperator = basePolylineSegments as IRelationalOperator; //If the Base contains the Comparison String report1 = "Contains: " + relationalOperator.Contains(comparisonGeometry) + "\n"; //If the Base crosses the Comparison report1 = report1 + "Crosses: " + relationalOperator.Crosses(comparisonGeometry) + "\n"; //If the Base and the Comparison are disjoint report1 = report1 + "Disjoint: " + relationalOperator.Disjoint(comparisonGeometry) + "\n"; //If the Base and the Comparison are equal report1 = report1 + "Equals: " + relationalOperator.Equals(comparisonGeometry) + "\n"; //If the Base and the Comparison overlap report1 = report1 + "Overlaps: " + relationalOperator.Overlaps(comparisonGeometry) + "\n"; //If the Base and the Comparison touch report1 = report1 + "Touches: " + relationalOperator.Touches(comparisonGeometry) + "\n"; //If the Base is within the Comparison report1 = report1 + "Within: " + relationalOperator.Within(comparisonGeometry) + "\n"; //Example of Contains point3.PutCoords(20, 20); point4.PutCoords(80, 80); line2.PutCoords(point3, point4); //line1 (0,0) to (100,100) //line2 (20, 20) to (80, 80) //Note: comparisonPolylineSegments holds now line2 with the new coordinates, //because it stores only references report1 = report1 + "contains: " + relationalOperator.Contains(comparisonGeometry) + "\n"; //Example of Within (Within is the complement of Contains) report1 = report1 + "Within: " + relationalOperator.Within(comparisonGeometry) + "\n"; //Example of Equals line2.PutCoords(point1, point2); //line1 (0,0) to (100,100) //line2 (0,0) to (100,100) //If Equals is True, then Within and Contains should also be true if(relationalOperator.Equals(comparisonGeometry)) { report1 = report1 + "Equals is true"+ "\n"; report1 = report1 + "Within: " + relationalOperator.Within(comparisonGeometry)+ "\n"; report1 = report1 + "Contains: " + relationalOperator.Contains(comparisonGeometry) + "\n"; } //Example of Disjoint point3.PutCoords(-40, 40); point4.PutCoords(-100, 80); line2.PutCoords(point3, point4); //line1 (0,0) to (100,100) //line2 (-40, 40) to (-100, 80) //Note: comparisonPolylineSegments holds now line2 with the new coordinates, //because it stores only references report1 = report1 + "Disjoint: " + relationalOperator.Disjoint(comparisonGeometry) + "\n"; //Example of Touches (Objects touch on their boundaries) point3.PutCoords(100, 100); point4.PutCoords(200, 150); line2.PutCoords(point3, point4); //line1 (0,0) to (100,100) //line2 (100,100) to (200,150) //Note: comparisonPolylineSegments holds now line2 with the new coordinates, //because it stores only references report1 = report1 + "Touches: " + relationalOperator.Touches(comparisonGeometry) + "\n"; //Example of Overlaps point3.PutCoords(140,140); point4.PutCoords(60,60); line2.PutCoords(point3, point4); //line1 (0,0) to (100,100) //line2 (140,140) to (60,60) //Note: comparisonPolylineSegments hold now line2 with the new coordinates, //because it stores only references report1 = report1 + "Overlaps: " + relationalOperator.Overlaps(comparisonGeometry) + "\n"; System.Windows.Forms.MessageBox.Show(report1); }
[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button
in the upper-left corner of the page.