|
- Dim x!(1 To 3), y!(1 To 3), deg, rad!, tg!, res(), rig As Boolean, _
- down As Boolean, tp, bt, lf, rg, fx, fy
- Const maxx = 20, maxy = 10
- Sub Main()
- ReDim tp(1 To 4), bt(1 To 4), lf(1 To 4), rg(1 To 4), res(1 To 2)
- Dim i%, keep(1 To 2), s As Series, co As Shape, ns%
- tp(1) = 0: tp(2) = maxy: tp(3) = maxx: tp(4) = maxy ' 上边线
- bt(1) = 0: bt(2) = 0: bt(3) = maxx: bt(4) = 0 ' 下边线
- lf(1) = 0: lf(2) = 0: lf(3) = 0: lf(4) = maxy ' 左边线
- rg(1) = maxx: rg(2) = 0: rg(3) = maxx: rg(4) = maxy ' 右边线
- rig = True
- ns = 23
- ReDim fx(1 To ns), fy(1 To ns)
- down = False
- x(1) = 1: y(1) = 3 ' 起始点
- res(1) = x(1): res(2) = y(1)
- x(2) = x(1) + maxx
- y(2) = y(1): x(3) = x(2)
- deg = 80 ' 度数
- rad = deg * 3.14159 / 180 ' 弧度
- tg = Tan(rad)
- y(3) = tg * (x(2) - x(1)) + y(2)
- fx(1) = x(1): fy(1) = y(1)
- For i = 2 To ns ' 步数
- If rig And down Then
- keep(1) = res(1): keep(2) = res(2)
- Walk rg, False, False
- If res(1) = 0 And res(2) = 0 Then
- res(1) = keep(1): res(2) = keep(2)
- Walk bt, False, False
- down = Not down
- Else
- rig = Not rig
- End If
- ElseIf rig And Not down Then ' 向右上
- keep(1) = res(1): keep(2) = res(2)
- Walk tp, False, True
- If res(1) = 0 And res(2) = 0 Then
- res(1) = keep(1): res(2) = keep(2)
- Walk rg, False, True
- rig = Not rig
- Else
- down = Not down
- End If
- ElseIf Not rig And down Then ' 左下
- keep(1) = res(1): keep(2) = res(2)
- Walk bt, True, False
- If res(1) = 0 And res(2) = 0 Then ' 没有有效的交叉
- res(1) = keep(1): res(2) = keep(2)
- Walk lf, True, False
- rig = Not rig
- Else
- down = Not down
- End If
- ElseIf Not rig And Not down Then ' 左上
- keep(1) = res(1): keep(2) = res(2)
- Walk lf, True, True
- If res(1) = 0 And res(2) = 0 Then
- res(1) = keep(1): res(2) = keep(2)
- Walk tp, True, True
- down = Not down
- Else
- rig = Not rig
- End If
- End If
- fx(i) = res(1): fy(i) = res(2)
- Next
- Set co = ActiveSheet.Shapes.AddChart2(240, xlXYScatterLines)
- Set s = co.Chart.SeriesCollection.NewSeries
- s.Values = fy
- s.XValues = fx
- With co.Chart
- .ChartTitle.Text = "Start is " & fx(1) & "," & fy(1) & " - " & _
- WorksheetFunction.Unichar(952) & "=" & deg
- If .SeriesCollection.Count > 1 Then
- .SeriesCollection(1).Delete
- .Axes(xlValue).MaximumScale = maxy
- .Axes(xlCategory).MaximumScale = maxx
- .Axes(xlCategory).MinimumScale = 0
- .Axes(xlValue).MinimumScale = 0
- End If
- End With
- End Sub
- Sub Walk(con, left As Boolean, up As Boolean)
- Dim hor
- If left Then hor = -maxx
- If Not left Then hor = maxx
- x(1) = res(1): y(1) = res(2)
- x(2) = x(1) + hor
- y(2) = y(1): x(3) = x(2)
- y(3) = tg * (x(2) - x(1)) + y(2) ' 是三角形
- If Not up And Not left Then y(3) = 2 * y(1) - y(3) ' 正确象限
- If up And left Then y(3) = 2 * y(1) - y(3)
- res = inter(x(1), y(1), x(3), y(3), con)
- End Sub
- Function inter(x1!, y1!, x2!, y2!, con) ' 两线之间的交叉
- Dim tp, den, ua!, ub!, x3!, y3!, x4!, y4!
- x3 = con(1): y3 = con(2): x4 = con(3): y4 = con(4)
- ReDim tp(1 To 2)
- If ((x1 = x2 And y1 = y2) Or (x3 = x4 And y3 = y4)) Then
- tp(1) = 0
- tp(2) = 0
- inter = tp
- Exit Function
- End If
- den = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1))
- If den = 0 Then
- tp(1) = 0
- tp(2) = 0
- inter = tp
- Exit Function
- End If
- ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / den
- ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / den
- If ua < 0 Or ua > 1 Or ub < 0 Or ub > 1 Then
- tp(1) = 0
- tp(2) = 0
- inter = tp
- Exit Function
- End If
- tp(1) = x1 + ua * (x2 - x1)
- tp(2) = y1 + ua * (y2 - y1)
- inter = tp
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|