怎样判断一个点在一条直线上(vba)
rt 垂距为零 判断该直线与直线某端点的斜率与直线斜率是否相等。 <P> 该点到直线两端点的距离和等于直线的长度</P> <P>可以用VBA实现:</P><P>得到起点:line.startpoint</P>
<P>得到端点:line.endpoint</P>
<P>然后再照三楼的方法,三个点确定两个斜率,相等就在直线上,否则,就返回false值</P> <P>三楼的方法可以判断点是否在线段或线段的延长线上;</P>
<P>四楼的方法可以判断点是否在线段上。</P>
<P>如果三楼的判断成立而四楼的判断不成立则点在线段的延长线上。</P> <p>四楼ok</p><p></p> 计算误差很烦人,老是要判断 本帖最后由 作者 于 2007-6-17 13:41:10 编辑 <br /><br /> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">设直线的两端点分别<b style="mso-bidi-font-weight: normal;">A</b>和<b style="mso-bidi-font-weight: normal;">B</b>, 判断任意点<b style="mso-bidi-font-weight: normal;">P</b>在直线<b style="mso-bidi-font-weight: normal;">AB</b>上否?<p></p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">解:</p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><span style="mso-spacerun: yes;"><font face="Times New Roman"> </font></span>以直线的一端点<b style="mso-bidi-font-weight: normal;"><font face="Times New Roman">A</font></b>为起点<font face="Times New Roman">, </font>另一端点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">B</b> </font>和任意点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P</b></font>为终点的矢量分别设为<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>和<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub>. </font>任意点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P</b><br/> </font>点和在直线<b style="mso-bidi-font-weight: normal;">AB</b>上的话,外积<font face="Times New Roman">(<b style="mso-bidi-font-weight: normal;"><span style="COLOR: red;">V</b><sub>0</sub></font></span>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub>)</font>的大小为<font face="Times New Roman">0.<p></p></font></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">即:<b style="mso-bidi-font-weight: normal;"><p></p></b></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><b style="mso-bidi-font-weight: normal;"> <font face="Times New Roman">||</font></b><font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub>|| </font>=<font face="Times New Roman">0</font> <font face="Times New Roman">---</font> 任意点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P</b> </font>在直线<b style="mso-bidi-font-weight: normal;">AB</b>上<p></p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><span style="mso-spacerun: yes;"> </span><font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">||</b><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub>|| <>0</font> <font face="Times New Roman">---</font> 任意点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P</b></font>不在直线<b style="mso-bidi-font-weight: normal;">AB</b>上<p></p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">式中:<font face="Times New Roman"><br/> </font>×为矢量的外积运算符<font face="Times New Roman">.</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman">************************************************</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">具体计算如下:</p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">设直线的端点坐标为<font face="Times New Roman"><br/> <b style="mso-bidi-font-weight: normal;">A </b>= (A(0), A(1), A(2) )<sup>T</sup>, <b style="mso-bidi-font-weight: normal;">B </b>= (B(0), B(1), B(2) )<sup>T</sup>, </font>任意点<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P </b>= (P(0), P(1), P(2) )<sup>T</sup><p></p></font></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">则<font face="Times New Roman">, </font>矢量</p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> </span><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>=<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">B </b>– <b style="mso-bidi-font-weight: normal;">A</b>, <b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>的成分为:<font face="Times New Roman">V<sub>0</sub>(i)<b style="mso-bidi-font-weight: normal;"> </b>= B(i) – A(i ), i=0,2<p></p></font></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> </span><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub></font>=<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">P </b>– <b style="mso-bidi-font-weight: normal;">A</b>, <b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub></font>的成分为:<font face="Times New Roman"><span style="mso-spacerun: yes;"> V</span><sub>1</sub>(i)<b style="mso-bidi-font-weight: normal;"> </b>= P(i) – A(i ), i=0,2</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">又设矢量的<span style="COLOR: fuchsia;">外积</span><font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b> = <b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub>, <b style="mso-bidi-font-weight: normal;">V</b></font>还是矢量<font face="Times New Roman">.</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> <b style="mso-bidi-font-weight: normal;">V</b> = </span><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub><b style="mso-bidi-font-weight: normal;"><p></p></b></font></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> <b style="mso-bidi-font-weight: normal;">V</b></font></span>的成分如下<font face="Times New Roman">,</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> V</span>(0)= V<sub>0</sub> (1) V<sub>1</sub> (2) – <span style="mso-spacerun: yes;"> V</span><sub>0</sub> (2) V<sub>1</sub> (1)</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> V</span>(1)= V<sub>0</sub> (2) V<sub>1</sub> (0) – <span style="mso-spacerun: yes;"> V</span><sub>0</sub> (0) V<sub>1</sub> (2)</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;"> V</span>(2)= V<sub>0</sub> (0) V<sub>1</sub> (1) – <span style="mso-spacerun: yes;"> V</span><sub>0</sub> (1) V<sub>1</sub> (0)</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt;">则<font face="Times New Roman">, <b style="mso-bidi-font-weight: normal;">||</b><b style="mso-bidi-font-weight: normal;">V</b><sub>0</sub></font>×<font face="Times New Roman"><b style="mso-bidi-font-weight: normal;">V</b><sub>1</sub><b style="mso-bidi-font-weight: normal;">||= </b>sqr(V(0)^2+ V(1)^2+ V(2)^2)</font></p><font face="Times New Roman"><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: "Times New Roman"; mso-hansi-font-family: "Times New Roman"; mso-bidi-font-size: 10.5pt;">然后用上面的结论即可判断出点<b style="mso-bidi-font-weight: normal;">P</b>在直线<b style="mso-bidi-font-weight: normal;">AB</b>上否<span lang="EN-US" style="mso-bidi-font-size: 10.5pt;"><font face="Times New Roman">.</font></span></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan;"><span lang="EN-US" style="mso-bidi-font-size: 10.5pt;">以上计算中只要进行简单的<span style="COLOR: fuchsia;">加</span>,减,乘,乘方,开方运算即可.</span></p><span lang="EN-US" style="FONT-SIZE: 10.5pt; FONT-FAMILY: "Times New Roman"; mso-fareast-language: ZH-CN; mso-fareast-font-family: 宋体; mso-bidi-language: AR-SA; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US;"><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: "Times New Roman"; mso-hansi-font-family: "Times New Roman"; mso-bidi-font-size: 10.5pt;">该方法不论是在2维空间还是3维空间,也不论点在线段AB上,还是在其延长线上,都能使用.<p></p></p></span></span><p></p><p></p><p></p><p></p><p></p><p></p><p></p></span></font> ''''''''''''''''''''''''''''''''''''''''''''''<br/>''''''''''''''''''''''''''''''''''''''''''''''<br/>'''<br/>'''这是一个判断点在直线上否的简单Sample<br/>'''<br/>'''Coded By ChunYue,Zou 2007.06.17<br/>''''''''''''''''''''''''''''''''''''''''''''''<br/>''''''''''''''''''''''''''''''''''''''''''''''<br/>Option Explicit<br/>Const CONST_AbsZero = 10 ^ (-4) '大小由计算精度确定<br/>Public Sub Test()<br/> Dim objline As AcadLine<br/> Dim returnPnt As Variant<br/> Dim A(0 To 2) As Double<br/> Dim B(0 To 2) As Double<br/> Dim P(0 To 2) As Double<br/> Dim V(0 To 2) As Double<br/> Dim V0(0 To 2) As Double<br/> Dim V1(0 To 2) As Double<br/> Dim Norm As Double<br/> <br/>'''<br/> '1.指定直线AB<br/> ThisDrawing.Utility.GetEntity objline, returnPnt, "Select a line:"<br/> returnPnt = objline.StartPoint<br/> A(0) = returnPnt(0)<br/> A(1) = returnPnt(1)<br/> A(2) = returnPnt(2)<br/> returnPnt = objline.EndPoint<br/> B(0) = returnPnt(0)<br/> B(1) = returnPnt(1)<br/> B(2) = returnPnt(2)<br/> '指定一任意点P<br/> returnPnt = ThisDrawing.Utility.GetPoint(, "Enter a point: ")<br/> P(0) = returnPnt(0)<br/> P(1) = returnPnt(1)<br/> P(2) = returnPnt(2)<br/> '2.计算矢量V0=B-A, V1=P-A<br/> V0(0) = B(0) - A(0)<br/> V0(1) = B(1) - A(1)<br/> V0(2) = B(2) - A(2)<br/> '<br/> V1(0) = P(0) - A(0)<br/> V1(1) = P(1) - A(1)<br/> V1(2) = P(2) - A(2)<br/> '3.求外积 V = V0 x V1<br/> V(0) = V0(1) * V1(2) - V0(2) * V1(1)<br/> V(1) = V0(2) * V1(0) - V0(0) * V1(2)<br/> V(2) = V0(0) * V1(1) - V0(1) * V1(0)<br/> '4.计算V的大小: Norm = ||V|| = ||V0 X V1||<br/> Norm = Sqr(V(0) ^ 2 + V(1) ^ 2 + V(2) ^ 2)<br/> '5.判断<br/> If Norm < CONST_AbsZero Then<br/> '点P在直线AB上<br/> MsgBox "点在直线上, Norm=" & Norm<br/> Else<br/> '点P不在直线AB上<br/> MsgBox "点不在直线上, Norm=" & Norm<br/> End If<br/>End Sub
页:
[1]
2