明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3800|回复: 6

如何判断曲线与直线相交,怎么求出交点

[复制链接]
发表于 2006-4-18 16:19:00 | 显示全部楼层 |阅读模式
高手进来看一下,如果CAD的dwg图形上有一个曲线,在边上任取一点,往曲线上画,可以有一个交点,如果用VBA编程的话,可以选定一个曲线,再选定一个点,那么,如何判断该点可以以一个角度作的直线与曲线相交呢,交点坐标如何求呢,高手在的话,回一下贴啊,看看有没有什么好的算法,或者一些具体的函数,求助!!!!!!!!!!!!!!!!
发表于 2006-4-18 21:50:00 | 显示全部楼层
[WEB]http://www.mccad.net/object/acad2004/idh_intersectwith.htm[/WEB]
 楼主| 发表于 2006-4-19 11:07:00 | 显示全部楼层

你这个方法我在AUTOCAD帮助中的开发文档里找到了,里面有个例子,是画线,画圆,然后求交点, 我复制过去,运行出现了错误,代码如下:


Public Sub CommandButton1_Click()
UserForm1.Hide
Dim lineObj As AcadLine
    Dim startPt(0 To 2) As Double
    Dim endPt(0 To 2) As Double
    startPt(0) = 1: startPt(1) = 1: startPt(2) = 0
    endPt(0) = 5: endPt(1) = 5: endPt(2) = 0
    Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt)
    UserForm1.Show
End Sub

Public Sub CommandButton2_Click()
UserForm1.Hide
Dim circleObj As AcadCircle
    Dim centerPt(0 To 2) As Double
    Dim radius As Double
    centerPt(0) = 3: centerPt(1) = 3: centerPt(2) = 0
    radius = 1
    Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)
    ZoomAll
UserForm1.Show

End Sub

Public Sub CommandButton3_Click()
UserForm1.Hide
Dim intPoints As Variant
Set intPoints = lineObj.IntersectWith(circleObj, acExtendNone)
   
    ' Print all the intersection points
    Dim I As Integer, j As Integer, k As Integer
    Dim str As String
    If VarType(intPoints) <> vbEmpty Then
        For I = LBound(intPoints) To UBound(intPoints)
            str = "Intersection Point[" & k & "] is: " & intPoints(j) & "," & intPoints(j + 1) & "," & intPoints(j + 2)
            MsgBox str, , "IntersectWith Example"
            str = ""
            I = I + 2
            j = j + 3
            k = k + 1
        Next
    End If
UserForm1.Show

End Sub
提示错误如下:运行错误‘424’

在Set intPoints = lineObj.IntersectWith(circleObj, acExtendNone)行上标示错误,

版主帮忙看一下,怎么回事

发表于 2006-4-19 13:34:00 | 显示全部楼层
intPoints = lineObj.IntersectWith(circleObj, acExtendNone)
 楼主| 发表于 2006-4-20 10:48:00 | 显示全部楼层

现在又有一个新问题,想问一下,我是做了一个循环,先画线,选取一个曲线,然后求交点,删除原来的直线,重新画线,以原起点为起点,交点为端点,结果出错,程序如下:

Private Sub CommandButton1_Click()
Dim angle As Double
Dim p1 As Variant
Dim p2(0 To 2) As Double
Dim lineobj As AcadLine
Dim lineobj2 As AcadLine
Dim curveobj As AcadObject
Dim intersectpoint As Variant '定义交点
Dim temppt(0 To 2) As Double
UserForm1.hide
p1 = ThisDrawing.Utility.GetPoint(, "选取坝体上某一点")
ThisDrawing.Utility.GetEntity curveobj, pickedpoint, "请选定曲线"
For angle = 180 To 360 Step 5
p2(0) = p1(0) + 100 * Cos(angle) '计算初始直线另一端点
p2(1) = p1(1) + 100 * Sin(angle)
p2(2) = p1(2)
Set lineobj = ThisDrawing.ModelSpace.AddLine(p1, p2)
intersectpoint = lineobj.IntersectWith(curveobj, acExtendthisEntity) '得到交点
temppt(0) = intersectpoint(0)
temppt(1) = intersectpoint(1)
temppt(2) = 0
lineobj.Delete
Set lineobj = ThisDrawing.ModelSpace.AddLine(p1, temppt) '重新画线

lineobj.color = Int(255 * Rnd + 1)
lineobj.Highlight True
lineobj.Update

Next angle

UserForm1.Show

End Sub

结果:temp(0)=intersectpoint(0)上提示下标越界

问一下,怎么回事,应该怎么解决

发表于 2006-4-20 14:53:00 | 显示全部楼层

实体间没有交点时,IntersectWith函数返回上界为-3的数组,会出现这种情况,要先判断一下

 楼主| 发表于 2006-4-20 20:16:00 | 显示全部楼层

谢谢,回答,再问一个问题,如果在直线上选定一个点,能不能在选定点的同时,返回直线上的标注,同时如果有一个曲线与直线上标注的数字一样的话,选择该曲线,

版主提示一下,用什么方法,,

谢谢啦,听他们说好像不能,不知道你有没有什么高招,

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-27 06:37 , Processed in 0.154112 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表