Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to figure algorithm to draw and paint polygons
#19
(09-01-2023, 10:28 PM)bplus Wrote: Don't have to figure fill point with triangle fills:
Code: (Select All)
_Title "Polygonal Fills without Paint or MapTriangle"
Screen _NewImage(800, 600, 32)
_ScreenMove 250, 50
Dim x(1 To 200), y(1 To 200)
Color &HFFFFFF00
Do
    While _MouseInput: Wend
    If _MouseButton(1) Then
        mx = _MouseX: my = _MouseY
        p = p + 1
        x(p) = mx: y(p) = my
        If p >= 3 Then
            filltri x(p - 2), y(p - 2), x(p - 1), y(p - 1), x(p), y(p)
        End If
        _Delay .25
    End If
    K$ = InKey$
    If K$ = "c" Then
        Cls
        p = 0
        Erase x, y
    End If
Loop Until _KeyDown(27)


'Andy Amaya's triangle fill modified for QB64, use if color already set
Sub filltri (xx1, yy1, xx2, yy2, xx3, yy3)
    Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single, x3 As Single, y3 As Single
    Dim slope1 As Single, slope2 As Single, length As Single, x As Single, lastx%, y As Single
    Dim slope3 As Single
    'make copies before swapping
    x1 = xx1: y1 = yy1: x2 = xx2: y2 = yy2: x3 = xx3: y3 = yy3

    'triangle coordinates must be ordered: where x1 < x2 < x3
    If x2 < x1 Then Swap x1, x2: Swap y1, y2
    If x3 < x1 Then Swap x1, x3: Swap y1, y3
    If x3 < x2 Then Swap x2, x3: Swap y2, y3
    If x1 <> x3 Then slope1 = (y3 - y1) / (x3 - x1)

    'draw the first half of the triangle
    length = x2 - x1
    If length <> 0 Then
        slope2 = (y2 - y1) / length
        For x = 0 To length
            Line (Int(x + x1), Int(x * slope1 + y1))-(Int(x + x1), Int(x * slope2 + y1))
            lastx% = Int(x + x1)
        Next
    End If

    'draw the second half of the triangle
    y = length * slope1 + y1: length = x3 - x2
    If length <> 0 Then
        slope3 = (y3 - y2) / length
        For x = 0 To length
            If Int(x + x2) <> lastx% Then
                Line (Int(x + x2), Int(x * slope1 + y))-(Int(x + x2), Int(x * slope3 + y2))
            End If
        Next
    End If
End Sub



There may be an art to keeping last 2 points going in the direction you want the poly to go.

I thought you were referencing your first no,second (good grief...) first post in this thread.

Later when I revisit my POLYGON fill method, I'll compare with James' approach and figure out which way I want to go.
Reply


Messages In This Thread
RE: Trying to figure algorithm to draw and paint polygons - by CharlieJV - 09-01-2023, 10:47 PM



Users browsing this thread: 12 Guest(s)