我需要定义一个过程来实现一个算法:我有n个字符串(n=1,2,3,.......),想编一个算法来确定每个字符串输出的左上角位置X,Y坐标。
关于X,Y坐标应遵循以下原则:
1.这n个字符串输出按左右两列排列;
2.这n个字符串以中心坐标(0,0)对称排列;
3.左列字符串的X坐标为-25;
4.右列字符串的X坐标为+25;
5.行与行之间Y坐标间隔30;
6.如果n是奇数行,中间一行Y为中心坐标0;如果是偶数行,一定是上下两行分别偏离Y的中心坐标+15、-15排列。寻求VB的算法,最好封装成一个函数来实现。
关于X,Y坐标应遵循以下原则:
1.这n个字符串输出按左右两列排列;
2.这n个字符串以中心坐标(0,0)对称排列;
3.左列字符串的X坐标为-25;
4.右列字符串的X坐标为+25;
5.行与行之间Y坐标间隔30;
6.如果n是奇数行,中间一行Y为中心坐标0;如果是偶数行,一定是上下两行分别偏离Y的中心坐标+15、-15排列。寻求VB的算法,最好封装成一个函数来实现。
·设整个字符区的左上角坐标为(0,0)
·按两列N行排列,使用.TextWidth和TextHeight方法计算出每个字符串将要占据的矩形大小
·计算得到左列和右列的最大宽度,加上预留的中间间隔(比如32个像素宽),得到总宽度。
·总高度=(每行高度+预留行间隔)*总行数-预留行间隔,(比如预留行间隔4个像素高)
·最后将所有X坐标减去‘总宽度/2’,所有Y坐标减去‘总高度/2’即为所求
'求关于中心点0,0对称的函数
Public Sub XY_ZuoBiao(ByVal n As Integer, ByRef a() As Integer)
'参数n为点个数,参数a()为要求的XY坐标组成的数组
ReDim a(n, 2)
。’正在编写,还未实现,敬请期待
End Sub
If (intRow Mod 2) = 0 Then
x0 = .Width / 2 - 25
y0 = .Height / 2 - intRow / 2 * 30
Else
x0 = .Width / 2 - 25
y0 = .Height / 2 - ((intRow - 1) / 2 * 30 + 15)
End If
For i = 1 To Text1.Text
If (i Mod 2) = 1 Then
.CurrentX = x0
.CurrentY = y0 + (i - 1) / 2 * 30
Else
.CurrentX = x0 + 50
.CurrentY = y0 + (i - 2) / 2 * 30
End If
.Print Trim(Str(i))
Next i
Dim n As Integer
Dim a() As Integer
n = 8
Call XY_ZuoBiao(n, a)
Dim i As Integer, j As Integer
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
Debug.Print a(i, j),
Next
Debug.Print
Next
End SubPublic Sub XY_ZuoBiao(ByVal n As Integer, ByRef a() As Integer)
Dim Row As Integer
Dim Idx As Integer
Dim tmpY As Integer
Dim i As Integer
Const X1 = -25: Const X2 = 25
Const Y = 30
Row = Abs(Fix(-n / 2))
ReDim a(n, 2)
tmpY = Row * Y / 2
For i = 0 To Row
a(Idx, 0) = Idx: a(Idx, 1) = X1: a(Idx, 2) = tmpY
If Idx + 1 <= n Then
a(Idx + 1, 0) = Idx + 1: a(Idx + 1, 1) = X2: a(Idx + 1, 2) = tmpY
End If
Idx = Idx + 2
tmpY = tmpY - Y
Next
End Sub以上为本帖正解,感谢“家人”的帮助!请“家人”过来接分!
, ByVal intCount As Integer, ByRef ary2() As Integer) As Boolean
Dim i As Integer
Dim intRow As Integer
Dim x1 As Integer
Dim y1 As Integer
If intCount < 1 Then
P_fun_GetXY = False
End If
ReDim ary2(intCount, 1) As Integer
intRow = Format(intCount / 2, "0")
x1 = intX0 - 25
y1 = intY0 - (intRow - 1) / 2 * 30
For i = 1 To intCount
If (i Mod 2) = 1 Then
ary2(i, 0) = x1
ary2(i, 1) = y1 + (i - 1) / 2 * 30
Else
ary2(i, 0) = x1 + 50
ary2(i, 1) = y1 + (i - 2) / 2 * 30
End If
Debug.Print ary2(i, 0) & ", " & ary2(i, 1)
Next i
P_fun_GetXY = True
End Function
窗体
Private Sub Command1_Click()
Dim ary2() As Integer
Dim i As Integer
If P_fun_GetXY(0, 0, Text1.Text, ary2()) Then
Debug.Print "***********************"
For i = 1 To Text1.Text
Debug.Print ary2(i, 0) & ", " & ary2(i, 1)
Next i
End If
End Sub
恩,你的结果也是正确的,不过你的代码没有vbman2003 的简洁,不过我会给分的,谢谢了!
那个 VB Code 是个什么东西呀?
Public Sub XY_ZuoBiao(ByVal n As Integer, ByRef a() As Integer)
'n为点的个数,起始从0开始;a用来存每个点的坐标
Dim Row As Integer
Dim Idx As Integer
Dim tmpY As Integer
Dim i As Integer
Const X1 = -25: Const X2 = 25
Const Y = 30
Row = Abs(Fix(-n / 2))
ReDim a(n, 1)
tmpY = Row * Y / 2
For i = 0 To Row
a(Idx, 0) = X1: a(Idx, 1) = tmpY
If Idx + 1 <= n Then
a(Idx + 1, 0) = X2: a(Idx + 1, 1) = tmpY
End If
Idx = Idx + 2
tmpY = tmpY - Y
Next
End Sub
如果行数为偶数,则中心点(0,0)上下两行的Y分别为+15和-15,如果行数为奇数,则中间的行的Y=0.这样才能保证无论n为多大,所有的点以中心点(0,0)为中心对称排列。
'n为点的个数,起始从0开始;a用来存每个点的坐标
Dim Row As Integer
Dim Idx As Integer
Dim tmpY As Integer
Dim i As Integer
Const X1 = -25: Const X2 = 25
Const Y = 30
Row = Abs(Fix(-n / 2))
ReDim a(n, 1)
tmpY = Row * Y / 2
For i = 0 To Row
a(Idx, 0) = X1: a(Idx, 1) = tmpY
If Idx + 1 <= n Then
a(Idx + 1, 0) = X2: a(Idx + 1, 1) = tmpY
End If
Idx = Idx + 2
tmpY = tmpY - Y
Next
End Sub
'n为点的个数,起始从0开始;a用来存每个点的坐标
Dim Row As Integer
Dim Idx As Integer
Dim tmpY As Integer
Dim i As Integer
Const X1 = -25: Const X2 = 25
Const Y = 30
Row = Abs(Fix(-n / 2))
ReDim a(n, 1)
tmpY = Row * Y / 2
For i = 0 To Row
a(Idx, 0) = X1: a(Idx, 1) = tmpY
If Idx + 1 <= n Then
a(Idx + 1, 0) = X2: a(Idx + 1, 1) = tmpY
End If
Idx = Idx + 2
tmpY = tmpY - Y
Next
End Sub