Пусть дан отрезок AB, A(x1, y1) а B(x2, y2), тогда чтобы получить Y по заданному X ( X от X1 до X2 ) воспользуемся формулой (я не упрощал - лень):
y = (y1(x2 -x1) + (x - x1)(y2 - y1))/(x2 - x1)
for x=x1 to x2
y = (y1(x2 -x1) + (x - x1)(y2 - y1))/(x2 - x1)
next
Sync писал(а):Пожалуйста если можно
можете подробнее рассказать
я както не врублюсь
Sub drawline(x1, y1, x2, y2)
Dim i, x, y, d, d1, d2, dx, dy, sx, sy
dx = Abs(x2 - x1)
dy = Abs(y2 - y1)
If x2 >= x1 Then
sx = 1
Else
sx = -1
End If
If y2 >= y1 Then
sy = 1
Else
sy = -1
End If
If dy <= dx Then
d = dy * 2 - dx
d1 = dy * 2
d2 = (dy - dx) * 2
'display(x1, y1) = col
x = x1 + sx
y = y1
For i = 1 To dx
If d > 0 Then
d = d + d2
y = y + sy
Else
d = d + d1
End If
'display(x, y) = col
x = x + sx
Next
Else
d = dx * 2 - dy
d1 = dx * 2
d2 = (dx - dy) * 2
'display(x1, y1) = col
x = x1
y = y1 + sy
For i = 1 To dy
If d > 0 Then
d = d + d2
x = x + sx
Else
d = d + d1
End If
'display(x, y) = col
y = y + sy
Next
End If
End Sub
alibek писал(а):Rainbow, рулез!
alibek писал(а):Только если линия будет нулевой длины (x1=x2, y1=y2), то алгоритм поставит одну точку
Лёха_Virus писал(а):ну ёлки палки... чё так усложнять то?
Sub SimpleLine(x1 As Long, y1 As Long, x2 As Long, y2 As Long)
Dim dx, dy, lb, hb, xi, yi, x As Long, y As Long
dx = Abs(x1 - x2)
dy = Abs(y1 - y2)
If dx > dy Then
If (x2 - x1) > 0 Then
lb = x1
hb = x2
Else
lb = x2
hb = x1
End If
For xi = lb To hb
y = (y2 - y1) * (xi - x1) / (x2 - x1) + y1
'Debug.Print "(" & xi & ", " & y & ")"
Next
Else
If (y2 - y1) > 0 Then
lb = y1
hb = y2
Else
lb = y2
hb = y1
End If
For yi = lb To hb
x = (yi - y1) * (x2 - x1) / (y2 - y1) + x1
'Debug.Print "(" & x & ", " & yi & ")"
Next
End If
End Sub
Dim i As Long
Dim t As Long
t = Timer()
For i = 0 To 50000
SimpleLine 2, 3, 1001, 702
Next
Debug.Print "simple: " & Timer() - t
t = Timer()
For i = 0 To 50000
drawline 1, 1, 1001, 702
Next
Debug.Print "drawline: " & Timer() - t
Sub SimpleLine(x1 As Long, y1 As Long, x2 As Long, y2 As Long)
Dim dx, dy, lb, hb, xi, yi, x As Long, y As Long
dx = Abs(x1 - x2)
dy = Abs(y1 - y2)
If dx > dy Then
If (x2 - x1) > 0 Then
lb = x1
hb = x2
Else
lb = x2
hb = x1
End If
For xi = lb To hb
y = (y2 - y1) * (xi - x1) / (x2 - x1) + y1
'Debug.Print "(" & xi & ", " & y & ")"
Next
Else
If (y2 - y1) > 0 Then
lb = y1
hb = y2
Else
lb = y2
hb = y1
End If
For yi = lb To hb
x = (yi - y1) * (x2 - x1) / (y2 - y1) + x1
'Debug.Print "(" & x & ", " & yi & ")"
Next
End If
End Sub
Sub SimpleLine2(x1 As Long, y1 As Long, x2 As Long, y2 As Long)
Dim dx, dy, lb, hb, xi, yi, x As Long, y As Long
dx = Abs(x1 - x2)
dy = Abs(y1 - y2)
Select Case dx > dy
Case True
Select Case (x2 - x1) > 0
Case True
lb = x1
hb = x2
Case False
lb = x2
hb = x1
End Select
For xi = lb To hb
y = (y2 - y1) * (xi - x1) / (x2 - x1) + y1
'Debug.Print "(" & xi & ", " & y & ")"
Next
Case False
Select Case (y2 - y1) > 0
Case True
lb = y1
hb = y2
Case False
lb = y2
hb = y1
End Select
For yi = lb To hb
x = (yi - y1) * (x2 - x1) / (y2 - y1) + x1
'Debug.Print "(" & x & ", " & yi & ")"
Next
End Select
End Sub
Private Sub Command1_Click()
Dim T#
T = Timer
SimpleLine 0, 0, 100, 100
Debug.Print Round(Timer - T, 4)
End Sub
Private Sub Command2_Click()
Dim T#
Cls
T = Timer
SimpleLine2 0, 0, 100, 100
Debug.Print Round(Timer - T, 4)
End Sub
Rainbow писал(а):А в чем разница?
A.A.Z. писал(а):Select Case во много раз быстрее If'а, если мне память не изменяет.
Сейчас этот форум просматривают: Yandex-бот и гости: 3