Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Challenges
#24
Happy Easter to all who celebrate it on this day!

Here is the first solution i came up with for Flower of Life:
Code: (Select All)
_Title "Flower of life 2" ' b+ 2024-03-26
' fix first version of flower of life around edges

Screen _NewImage(700, 700, 32)
_ScreenMove 300, 40

'drawPetal 300, 350, 400, 350, &HFF8888FF
'End
cx = _Width / 2: cy = _Height / 2: r = 100: c~& = &HFF8888FF
drawflower6 cx, cy, r, c~&
'End
For i = 0 To 5
    px = cx + 2 * r * Cos(i * _Pi(1 / 3) - _Pi(.5))
    py = cy + 2 * r * Sin(i * _Pi(1 / 3) - _Pi(.5))
    If i <> 0 Then
        drawflower6 (px + lastpx) / 2, (py + lastpy) / 2, r, c~&
    Else
        savex = px: savey = py
    End If
    drawflower6 px, py, r, c~&
    lastpx = px: lastpy = py
Next
drawflower6 (savex + lastpx) / 2, (savey + lastpy) / 2, r, c~&

For i = 0 To 35
    x = cx + 300 * Cos(i * _Pi(2 / 36))
    y = cy + 300 * Sin(i * _Pi(2 / 36))
    If i = 0 Then savex = x: savey = y Else drawPetal lastx, lasty, x, y, &HFFFFFF00
    lastx = x: lasty = y
Next
drawPetal lastx, lasty, savex, savey, &HFFFFFF00
Sleep

Sub drawPetal (x1, y1, x2, y2, c~&)
    dist = _Hypot(x1 - x2, y1 - y2)
    a = _Atan2(y1 - y2, x1 - x2)
    x0 = x2 + dist * Cos(a + _Pi(1 / 3))
    y0 = y2 + dist * Sin(a + _Pi(1 / 3))
    mx = (x1 + x2) / 2
    my = (y1 + y2) / 2
    a1 = _Atan2(my - y0, mx - x0)
    a2 = a1 + _Pi(1 / 6)
    a3 = a1 - _Pi(1 / 6)
    If a2 < a3 Then starta = a2: stopa = a3 Else starta = a3: stopa = a2
    arc x0, y0, dist, starta, stopa, c~&
    arc x0, y0, dist + 1, starta, stopa, c~&
    x0 = x2 + dist * Cos(a - _Pi(1 / 3))
    y0 = y2 + dist * Sin(a - _Pi(1 / 3))
    a1 = _Atan2(my - y0, mx - x0)
    a2 = a1 + _Pi(1 / 6)
    a3 = a1 - _Pi(1 / 6)
    If a2 < a3 Then starta = a2: stopa = a3 Else starta = a3: stopa = a2
    arc x0, y0, dist, starta, stopa, c~&
    arc x0, y0, dist + 1, starta, stopa, c~&
End Sub

Sub drawflower6 (x, y, r, c~&)
    Dim px(5), py(5)
    For i = 0 To 5
        px(i) = x + r * Cos(i * _Pi(1 / 3) - _Pi(.5))
        py(i) = y + r * Sin(i * _Pi(1 / 3) - _Pi(.5))
    Next
    For i = 0 To 5
        drawPetal x, y, px(i), py(i), c~&
        drawPetal px(i), py(i), px((i + 1) Mod 6), py((i + 1) Mod 6), c~&
    Next
End Sub

'use radians
Sub arc (x, y, r, raStart, raStop, c As _Unsigned Long) ' this does not check raStart and raStop like arcC does
    Dim al, a
    'x, y origin, r = radius, c = color

    'raStart is first angle clockwise from due East = 0 degrees
    ' arc will start drawing there and clockwise until raStop angle reached

    If raStop < raStart Then
        arc x, y, r, raStart, _Pi(2), c
        arc x, y, r, 0, raStop, c
    Else
        ' modified to easier way suggested by Steve
        'Why was the line method not good? I forgot.
        al = _Pi * r * r * (raStop - raStart) / _Pi(2)
        For a = raStart To raStop Step 1 / al
            PSet (x + r * Cos(a), y + r * Sin(a)), c
        Next
    End If
End Sub

i solved by first drawing a petal then drawing a flower then overlapping the flower drawings. To get the edges right, I had to calc the midpoints between the current flower to draw and the last one drawn, this part:
Code: (Select All)
If i <> 0 Then
        drawflower6 (px + lastpx) / 2, (py + lastpy) / 2, r, c~&
    Else
        savex = px: savey = py
    End If

It is curious the only 2 solves I've seen to this challenge were not in QB64, and very different approaches than mine.

vince challenged me at discord to do it without overlap. i decided i had to skip the flower drawing part and do all petals individually BUT to avoid duplicate petal drawing i had to get all the points the flower petals connect. i did that in an unusual way by collecting all the points I used in flower drawing and then removing the duplicates, 36 + 1 points THEN if i drew the petal from point 23 to point 24 i did Not want to duplicate drawing from point 24 to point 23.

index i runs through all the points but the last and index j runs through all the points after i to the last, if the distance between point i and point j is just r then draw the petal. This gets all the petals drawn without duplication.
Code: (Select All)
_Title "Flower of Life" ' b+ 2024-03-27
' the challenge here is to do this without overlapping circles
' to do this we need all the points and drawPetal
Screen _NewImage(700, 700, 32)
_ScreenMove 300, 40

cx = _Width / 2: cy = _Height / 2: r = 100: c~& = &HFF8888FF

Dim px(42), py(42)
For i = 0 To 5 ' just collect the points
    p = p + 1
    x = cx + 2 * r * Cos(i * _Pi(1 / 3) - _Pi(.5))
    y = cy + 2 * r * Sin(i * _Pi(1 / 3) - _Pi(.5))
    px(p) = x: py(p) = y
    For j = 0 To 5
        p = p + 1
        px(p) = x + r * Cos(j * _Pi(1 / 3) - _Pi(.5))
        py(p) = y + r * Sin(j * _Pi(1 / 3) - _Pi(.5))
    Next
Next
px(0) = cx: py(0) = cy
Dim qx(36), qy(36)
Print "Here are all the points we need to connect with petals if distance between them = r."
For i = 0 To 42 'remove repeated points
    If i <> 14 And i <> 16 And i <> 24 And i <> 32 And i <> 38 And i <> 40 Then
        qx(q) = px(i): qy(q) = py(i)
        Circle (qx(q), qy(q)), 1
        q = q + 1
    End If
Next
_PrintString (100, 680), "press any, to see the petals drawn to fill out Flower of Life..."
Sleep
Line (0, 0)-(_Width, 18), &HFF000000, BF ' black out text
Line (0, 679)-(_Width, _Height), &HFF000000, BF
For i = 0 To 35
    For j = i + 1 To 36
        If _Hypot(qx(i) - qx(j), qy(i) - qy(j)) - r < .1 Then
            drawPetal qx(i), qy(i), qx(j), qy(j), &HFF8888FF
            _Limit 3
        End If
    Next
Next
For i = 0 To 35 ' draw border
    x = cx + 300 * Cos(i * _Pi(2 / 36))
    y = cy + 300 * Sin(i * _Pi(2 / 36))
    If i = 0 Then savex = x: savey = y Else drawPetal lastx, lasty, x, y, &HFFFFFF00
    lastx = x: lasty = y
Next
drawPetal lastx, lasty, savex, savey, &HFFFFFF00
Sleep

Sub drawPetal (x1, y1, x2, y2, c~&)
    dist = _Hypot(x1 - x2, y1 - y2)
    a = _Atan2(y1 - y2, x1 - x2)
    x0 = x2 + dist * Cos(a + _Pi(1 / 3))
    y0 = y2 + dist * Sin(a + _Pi(1 / 3))
    mx = (x1 + x2) / 2
    my = (y1 + y2) / 2
    a1 = _Atan2(my - y0, mx - x0)
    a2 = a1 + _Pi(1 / 6)
    a3 = a1 - _Pi(1 / 6)
    If a2 < a3 Then starta = a2: stopa = a3 Else starta = a3: stopa = a2
    arc x0, y0, dist, starta, stopa, c~&
    arc x0, y0, dist + 1, starta, stopa, c~&
    x0 = x2 + dist * Cos(a - _Pi(1 / 3))
    y0 = y2 + dist * Sin(a - _Pi(1 / 3))
    a1 = _Atan2(my - y0, mx - x0)
    a2 = a1 + _Pi(1 / 6)
    a3 = a1 - _Pi(1 / 6)
    If a2 < a3 Then starta = a2: stopa = a3 Else starta = a3: stopa = a2
    arc x0, y0, dist, starta, stopa, c~&
    arc x0, y0, dist + 1, starta, stopa, c~&
End Sub

'use radians
Sub arc (x, y, r, raStart, raStop, c As _Unsigned Long) ' this does not check raStart and raStop like arcC does
    'x, y origin, r = radius, c = color
    'raStart is first angle clockwise from due East = 0 degrees
    ' arc will start drawing there and clockwise until raStop angle reached
    Dim al, a
    If raStop < raStart Then
        arc x, y, r, raStart, _Pi(2), c
        arc x, y, r, 0, raStop, c
    Else
        ' modified to easier way suggested by Steve
        'Why was the line method not good? I forgot.
        al = _Pi * r * r * (raStop - raStart) / _Pi(2)
        For a = raStart To raStop Step 1 / al
            PSet (x + r * Cos(a), y + r * Sin(a)), c
        Next
    End If
End Sub

an alternate coloring scheme that i was challenged to do after seeing Charlies submission:
   

edit: replace code with original copies, remove amp
b = b + ...
Reply


Messages In This Thread
Challenges - by bplus - 04-27-2022, 05:21 PM
RE: Challenges - by Pete - 04-27-2022, 05:33 PM
RE: Challenges - by bplus - 04-27-2022, 05:38 PM
RE: Challenges - by Pete - 04-27-2022, 06:00 PM
RE: Challenges - by bplus - 04-27-2022, 06:08 PM
RE: Challenges - by bplus - 04-28-2022, 01:17 AM
RE: Challenges - by Dav - 04-28-2022, 01:26 AM
RE: Challenges - by Pete - 04-28-2022, 01:59 AM
RE: Challenges - by bplus - 05-04-2022, 01:36 AM
RE: Challenges - by Pete - 05-04-2022, 01:51 AM
RE: Challenges - by bplus - 05-04-2022, 01:57 AM
RE: Challenges - by Pete - 05-04-2022, 02:22 AM
RE: Challenges - by bplus - 05-04-2022, 04:10 PM
RE: Challenges - by bplus - 06-18-2022, 01:10 PM
RE: Challenges - by SierraKen - 06-18-2022, 11:32 PM
RE: Challenges - by bplus - 06-19-2022, 01:09 AM
RE: Challenges - by bplus - 03-26-2024, 11:37 PM
RE: Challenges - by CharlieJV - 03-30-2024, 12:02 AM
RE: Challenges - by SMcNeill - 03-27-2024, 12:20 AM
RE: Challenges - by SMcNeill - 03-27-2024, 12:32 AM
RE: Challenges - by bplus - 03-27-2024, 01:24 AM
RE: Challenges - by bplus - 03-30-2024, 02:53 PM
RE: Challenges - by CharlieJV - 03-30-2024, 03:39 PM
RE: Challenges - by bplus - 03-31-2024, 01:33 PM
RE: Challenges - by Petr - 03-31-2024, 02:00 PM
RE: Challenges - by SMcNeill - 03-31-2024, 02:18 PM
RE: Challenges - by Petr - 03-31-2024, 02:23 PM
RE: Challenges - by bplus - 03-31-2024, 03:28 PM
RE: Challenges - by bplus - 06-18-2024, 10:42 PM
RE: Challenges - by SMcNeill - 06-19-2024, 04:32 AM
RE: Challenges - by SMcNeill - 06-19-2024, 04:45 AM
RE: Challenges - by KingLeonidas - 06-19-2024, 09:42 AM
RE: Challenges - by bplus - 06-19-2024, 01:59 PM
RE: Challenges - by bplus - 06-21-2024, 07:31 PM
RE: Challenges - by bplus - 06-23-2024, 03:07 PM



Users browsing this thread: 2 Guest(s)