1
votes

I have some problem which is grinding my gears for some time now. Description is really simple. I would like to "draw cells line" between two cells (which are somehow marked). Like you mark 2 cells, then click button and it creates line with filled cells between these two points.

Something like this

I still cant figure out how to fill every cell between these two points. I had some mathematical ideas but couldn't get them in code to work properly.

Private Sub CommandButton1_Click()
Dim i As Integer, j As Integer, k As Integer, l As Integer

Dim cred As Long
Dim cgreen As Long
Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim koef As Single
Dim arr(1 To 20, 1 To 20) As Boolean
Dim st As Integer

cgreen = Range("X5").Interior.Color
cred = Range("X6").Interior.Color

'Start and End
For i = 1 To 20
    For j = 1 To 20
        If Cells(i, j).Interior.Color = cred Then
            endx = j
            endy = -i
        ElseIf Cells(i, j).Interior.Color = cgreen Then
            startx = j
            starty = -i
        End If
    Next j
Next i

koef = (endy - starty) / (endx - startx)
If starty < endy Then
    st = 1
Else
    st = -1
End If


For i = startx To endx
    For k = starty To endy Step st
    l = -k / koef
    j = starty + koef * (i - startx)
    l = starty + koef * (i + 1 - startx)
    If k >= j Then
    arr(i, -j) = True
    End If
    Next k
Next i

For i = 1 To 20
    For j = 1 To 20
        If arr(j, i) = True Then
            Cells(i, j).Interior.Color = RGB(255, 255, 0)
        End If
    Next j
Next i
1
Is it always three down and one to the right?SJR
Drawing lines with pixels is a well-studied problem which is trickier than it first appears. Using large pixels (which you are in effect doing) makes the problem more visible but doesn't really alter it. See: en.wikipedia.org/wiki/Line_drawing_algorithmJohn Coleman
Hmm, yeah, this is it. I will study some methods to do this task. Thanks for link :)Sniper_of_Chess

1 Answers

1
votes

You can find intersection points as on image (img1), round them. Those are your cells coordinate to fill in color Or even better: you can find intersection of each cells boundaries (img2) and play with fraction value (img3) to get intensity of pixel Here is what achieved: img4