PDA

View Full Version : TEST FOR HORIZONTAL OR VERTICAL LINE



mike.lee771419
2019-05-23, 02:52 PM
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.



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

Ed Jobe
2019-05-23, 03:22 PM
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?

mike.lee771419
2019-05-23, 04:13 PM
I did use ortho mode and constrained the line to be horizontal.

Ed Jobe
2019-05-24, 03:12 PM
Here's my version. I tested it in a 2D dwg done in WCS.



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

Public Sub CheckLines()

Dim oEnt As AcadEntity
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