# Thread: TEST FOR HORIZONTAL OR VERTICAL LINE

#### Hybrid View

1. Login to Give a bone

## TEST FOR HORIZONTAL OR VERTICAL LINE

Hello,

I have the following code to see if a selected line is horizontal or vertical but some lines that have the same StartY and EndY are still returning as angled. What am I missing? I even went so far as to restrain the line horizontal.

Code:
```Public Shared Function HorizontalVerticle(Line As Line) As Tuple(Of String, Double, Double, Double, Double) 'Determin if line is horizontal or verticle
If Line.StartPoint.Y = Line.EndPoint.Y And Line.StartPoint.X <> Line.EndPoint.X Then
'MsgBox("Horizontal, " & Line.StartPoint.Y & vbCrLf & "Horizontal, " & Line.EndPoint.Y,, "HorizontalVerticle Function")
Dim y = New Tuple(Of String, Double, Double, Double, Double)("Horizontal", Line.StartPoint.Y, Line.StartPoint.X, Line.EndPoint.Y, Line.EndPoint.X)
Return y
ElseIf Line.StartPoint.X = Line.EndPoint.X And Line.StartPoint.Y <> Line.EndPoint.Y Then
'MsgBox("Verticle, " & Line.StartPoint.Y & vbCrLf & "Verticle, " & Line.EndPoint.Y,, "HorizontalVerticle Function")
Dim y = New Tuple(Of String, Double, Double, Double, Double)("Verticle", Line.StartPoint.Y, Line.StartPoint.X, Line.EndPoint.Y, Line.EndPoint.X)
Return y
ElseIf Line.StartPoint.X <> Line.EndPoint.X And Line.StartPoint.Y <> Line.EndPoint.Y Then
'MsgBox("Line is neither horizontal or verticle, " & Line.StartPoint.Y & vbCrLf & "Verticle, " & Line.EndPoint.Y,, "HorizontalVerticle Function")
Dim y = New Tuple(Of String, Double, Double, Double, Double)("Angled", Line.StartPoint.Y, Line.StartPoint.X, Line.EndPoint.Y, Line.EndPoint.X)
Return y
End If
End Function```

2. Login to Give a bone

## Re: TEST FOR HORIZONTAL OR VERTICAL LINE

Did you use ORTHO when creating the line. It could look horizontal but have small variations in Y. You might not see it if you have your unit resolution set low. You could use the line's angle an specify a fuzz factor by comparing if its between two values.

Also, in your function, you declared a variable Line As type Line. Its bad practice to name variables using language keywords. My naming convention is to use a 3-letter prefix with a meaningful name. e.g. linTest, strMessage.

Also, the calling function passes in a Line object, so it already knows the start point and end point. Why return those same values?

3. Login to Give a bone

## Re: TEST FOR HORIZONTAL OR VERTICAL LINE

I did use ortho mode and constrained the line to be horizontal.

4. Login to Give a bone

## Re: TEST FOR HORIZONTAL OR VERTICAL LINE

Here's my version. I tested it in a 2D dwg done in WCS.

Code:
```Public Function PI() As Double
PI = Atn(1) * 4
End Function

Public Sub CheckLines()

Dim vPoint As Variant
Dim HV As Boolean

GetLine:
ThisDrawing.Utility.getEntity oEnt, vPoint, "Select a Line. "
If TypeOf oEnt Is AcadLine Then
'check for horiz or vert
HV = CkHoriz(oEnt)
If HV = True Then
ThisDrawing.Utility.Prompt "Selected line is Horizontal. "
ElseIf HV = False Then
HV = CkVert(oEnt)
If HV = True Then
ThisDrawing.Utility.Prompt "Selected line is Vertical. "
Else
ThisDrawing.Utility.Prompt "Selected line is not Horizontal or Vertical. "
End If
End If

Else
ThisDrawing.Utility.Prompt "You didn't select a Line. "
GoTo GetLine
End If
End Sub

Public Function CkHoriz(linTest As AcadLine) As Boolean
CkHoriz = False
If linTest.Angle = 0 Or linTest.Angle = PI() Then
CkHoriz = True
End If
End Function

Public Function CkVert(linTest As AcadLine) As Boolean
CkVert = False
If linTest.Angle = 0.5 * PI() Or linTest.Angle = 1.5 * PI() Then
CkVert = True
End If
End Function```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•