Author Topic: Cresent Pattern Challenge from JB  (Read 8911 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #15 on: June 28, 2019, 03:57:41 pm »
Good news for all you math guys

my graphic idea has an overlapping logic bug ...
but it does the same something.

  [ You are not allowed to view this attachment ]  
Code: QB64: [Select]
  1. CONST d = 100
  2. SCREEN _NEWIMAGE(800, 600, 32)
  3. _TITLE "Cresent"
  4. PAINT (1, 1), _RGB32(255, 255, 255)
  5. DIM NumX AS INTEGER, NumY AS INTEGER, i AS INTEGER, i2 AS INTEGER
  6. NumX = _CEIL(_WIDTH / d): NumY = _CEIL(_HEIGHT / d)
  7. FOR i = 0 TO NumX
  8.     FOR i2 = 0 TO NumY
  9.         IF i2 MOD 2 = 0 THEN k = 0 ELSE k = d / 4
  10.         x = (i * d) + k
  11.         y = i2 * d
  12.         r = d / 2
  13.         c~& = _RGB32(0, 0, 0)
  14.         MakeSlice x, y, r, c~&
  15.     NEXT i2
  16.  
  17. SUB MakeSlice (xp AS INTEGER, yp AS INTEGER, r AS INTEGER, col AS _UNSIGNED LONG)
  18.     DIM a AS INTEGER, ax AS INTEGER, ay AS INTEGER, ar AS INTEGER, col2 AS _UNSIGNED LONG
  19.     ar = INT(r / 2)
  20.     FOR a = 6 TO 3 STEP -1
  21.         col2 = col
  22.         ax = SIN(a) * ar
  23.         ay = COS(a) * ar
  24.         'CIRCLE (ax + xp, ay + yp), ar, col2
  25.         'PAINT STEP(0, 0), col2, col2
  26.         CALL CircleFill(ax + xp, ay + yp, ar, col2)
  27.         ax = SIN(a + 6) * ar
  28.         ay = COS(a + 6) * ar
  29.         '       CIRCLE (ax + xp, ay + yp), ar, _RGB32(255, 255, 255)
  30.         '        PAINT STEP(0, 0), _RGB32(255, 255, 255), _RGB32(255, 255, 255)
  31.         CALL CircleFill(ax + xp, ay + yp, ar, _RGB32(255, 255, 255))
  32.     NEXT
  33.  
  34. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  35.     ' CX = center x coordinate
  36.     ' CY = center y coordinate
  37.     '  R = radius
  38.     '  C = fill color
  39.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  40.     DIM X AS INTEGER, Y AS INTEGER
  41.     Radius = ABS(R)
  42.     RadiusError = -Radius
  43.     X = Radius
  44.     Y = 0
  45.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  46.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  47.     WHILE X > Y
  48.         RadiusError = RadiusError + Y * 2 + 1
  49.         IF RadiusError >= 0 THEN
  50.             IF X <> Y + 1 THEN
  51.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  52.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  53.             END IF
  54.             X = X - 1
  55.             RadiusError = RadiusError - X * 2
  56.         END IF
  57.         Y = Y + 1
  58.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  59.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  60.     WEND
  61.  

to complete the image a workaround may  be to join two specular images of that showed on the screen... but it is so complex near your fantastic linear math code!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #16 on: June 28, 2019, 04:27:39 pm »
Hi TempodiBasic,

It's difficult trying to fit coding in with job and sleep, I remember burning the candle from both ends in 1993 or so...


Anyway for your overlap problem try drawing 1/2 circles, arcs!, instead of full circles. Get one going right, then worry about wallpapering the screen with them.

And thanks for using your precious time for this crazy thing. :-)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #17 on: June 28, 2019, 06:12:00 pm »
@Bplus

Thanks

about
Quote
Anyway for your overlap problem try drawing 1/2 circles, arcs!, instead of full circles.
I cannot face  again with  PAINT and CIRCLE lacking color.... not now!
And I have no knownledge that CircleFill and its brothers can draw arches!
... or is there  no lacking color for halfcircle?
Instead to have an urticaria attack I left my keyboard and go walking with my dog before to go sleep!
See the next time. GoodNight
Programming isn't difficult, only it's  consuming time and coffee

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #18 on: June 28, 2019, 08:29:21 pm »
I found bplus's thread:
http://justbasiccom.proboards.com/thread/304/pattern-challenge?page=1&scrollTo=1804

The original image is slightly different than bplus's and is easier to draw, technotitlick is on the right track, just needs some fine tuning.  I like bplus's better though, looks like calligraphy.

Since some of you are playing with rotating the swirls, one neat effect is to have each rotating at different speeds, cleverly chosen, for a nice hypnotic effect.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #19 on: June 28, 2019, 08:40:15 pm »
reminds me of geometry problems from school where you have to find unknown distances and angles in a partially labelled diagrams, fun exercise

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #20 on: June 28, 2019, 08:48:56 pm »
I found bplus's thread:
http://justbasiccom.proboards.com/thread/304/pattern-challenge?page=1&scrollTo=1804

The original image is slightly different than bplus's and is easier to draw, technotitlick is on the right track, just needs some fine tuning.  I like bplus's better though, looks like calligraphy.

Since some of you are playing with rotating the swirls, one neat effect is to have each rotating at different speeds, cleverly chosen, for a nice hypnotic effect.

Hi _vince,

The original image is easier to draw? The original image is a Hexagonal shape of twirls and at slight angle, which I imagine would be harder to replicate.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #21 on: June 28, 2019, 08:54:13 pm »
Hi TempodiBasic,

This must be what you are trying to say:
Code: QB64: [Select]
  1. _TITLE "Crescent Pattern by Half Circle Arcs"
  2. CONST xmax = 800, ymax = 600, pi = 3.14159265
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4.  
  5. crescent xmax / 2, ymax / 2, 50
  6.  
  7. SUB crescent (x0, y0, r6) 'r6 is radius of 6 crescent pattern
  8.     DIM a12, a, r1, x12, y12, stopa
  9.     r1 = r6 / 2 ' the radius of each crescent
  10.     a12 = 2 * pi / 12 ' 30 degrees to draw 12 arcs about x0, y0
  11.     FOR a = 0 TO 2 * pi STEP a12 'draw 12 arcs
  12.         'we need origin of the 12 arcs
  13.         x12 = x0 + r1 * COS(a)
  14.         y12 = y0 + r1 * SIN(a)
  15.         'arc start angle = a end angle = a + pi = 1/2 circle
  16.         IF a + pi > 2 * pi THEN
  17.             stopa = 2 * pi - (a + pi)
  18.             CIRCLE (x12, y12), r1, , a, 2 * pi '<<< as I've said before CIRCLE sub sucks!
  19.             CIRCLE (x12, y12), r1, , 0, stopa
  20.         ELSE
  21.             CIRCLE (x12, y12), r1, , a, a + pi
  22.         END IF
  23.     NEXT
  24.  

The operator of the CIRCLE command is probably partly to blame. :P
« Last Edit: June 28, 2019, 09:51:11 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #22 on: June 28, 2019, 08:59:09 pm »
Never fear, B+ is here:
Code: QB64: [Select]
  1. _TITLE "Crescent Pattern by Half Circle Arcs"
  2. CONST xmax = 800, ymax = 600, pi = 3.14159265
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4.  
  5. crescent xmax / 2, ymax / 2, 50
  6.  
  7. SUB crescent (x0, y0, r6) 'r6 is radius of 6 crescent pattern
  8.     DIM a12, a, r1, x12, y12
  9.     r1 = r6 / 2 ' the radius of each crescent
  10.     a12 = 2 * pi / 12 ' 30 degrees to draw 12 arcs about x0, y0
  11.     FOR a = 0 TO 2 * pi STEP a12 'draw 12 arcs
  12.         'we need origin of the 12 arcs
  13.         x12 = x0 + r1 * COS(a)
  14.         y12 = y0 + r1 * SIN(a)
  15.         'arc start angle = a end angle = a + pi = 1/2 circle
  16.         arc x12, y12, r1, a, a + pi, &HFF0000FF
  17.     NEXT
  18.  
  19.  
  20. SUB arc (x, y, r, raStart, raStop, c AS _UNSIGNED LONG)
  21.     'x, y origin, r = radius, c = color
  22.  
  23.     'raStart is first angle clockwise from due East = 0 degrees
  24.     ' arc will start drawing there and clockwise until raStop angle reached
  25.  
  26.     DIM al, a
  27.     IF raStop < raStart THEN
  28.         arc x, y, r, raStart, _PI(2), c
  29.         arc x, y, r, 0, raStop, c
  30.     ELSE
  31.         ' modified to easier way suggested by Steve
  32.         'Why was the line method not good? I forgot.
  33.         al = _PI * r * r * (raStop - raStart) / _PI(2)
  34.         FOR a = raStart TO raStop STEP 1 / al
  35.             PSET (x + r * COS(a), y + r * SIN(a)), c
  36.         NEXT
  37.     END IF
  38.  

hmm... looks like we need arcs a little longer than half circles.  :o
« Last Edit: June 28, 2019, 09:01:14 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #23 on: June 28, 2019, 09:50:05 pm »
Outline looks OK, the start angle is the outer point, I was thinking wrongly it was the end angle.
Code: QB64: [Select]
  1. _TITLE "Crescent Pattern by Half Circle Arcs"
  2. CONST xmax = 800, ymax = 600, pi = 3.14159265
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4.  
  5. crescent xmax / 2, ymax / 2, 50
  6.  
  7. SUB crescent (x0, y0, r6) 'r6 is radius of 6 crescent pattern
  8.     DIM a12, a, r1, x12, y12
  9.     r1 = r6 / 2 ' the radius of each crescent
  10.     a12 = 2 * pi / 12 ' 30 degrees to draw 12 arcs about x0, y0
  11.     FOR a = 0 TO 2 * pi STEP a12 'draw 12 arcs
  12.         'we need origin of the 12 arcs
  13.         x12 = x0 + r1 * COS(a)
  14.         y12 = y0 + r1 * SIN(a)
  15.         'arc start angle = a end angle = a + pi = 1/2 circle
  16.         arc x12, y12, r1, a - pi / 6, a + pi, &HFF0000FF '<<<<<<<<<<<<<<<<<<<<<<<<<<<<  start angle earlier
  17.     NEXT
  18.  
  19.  
  20. SUB arc (x, y, r, raStart, raStop, c AS _UNSIGNED LONG)
  21.     'x, y origin, r = radius, c = color
  22.  
  23.     'raStart is first angle clockwise from due East = 0 degrees
  24.     ' arc will start drawing there and clockwise until raStop angle reached
  25.  
  26.     DIM al, a
  27.     IF raStop < raStart THEN
  28.         arc x, y, r, raStart, _PI(2), c
  29.         arc x, y, r, 0, raStop, c
  30.     ELSE
  31.         ' modified to easier way suggested by Steve
  32.         'Why was the line method not good? I forgot.
  33.         al = _PI * r * r * (raStop - raStart) / _PI(2)
  34.         FOR a = raStart TO raStop STEP 1 / al
  35.             PSET (x + r * COS(a), y + r * SIN(a)), c
  36.         NEXT
  37.     END IF
  38.  

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #24 on: June 28, 2019, 10:00:25 pm »
im on freebasic so ill just post the algorithm, but this one needed some manual fiddling, close enough for me

mine isnt pixel perfect for a few precision reasons, i challenge someone to perfectly reproduce the JB forum image, bonus points for antialiased edges
Code: [Select]
sw = 1920
sh = 1080

'fill in

xx = sw/2
yy = sh/2
a = pi/2 - pi/4 - pi/15 + 0.05 - 0.15
b = pi/6
c = 0.2
r = 100

for i = 0 to 5
x = xx + 2*r*cos(i*pi/3 + c)
y = yy + 2*r*sin(i*pi/3 + c)
for j = 0 to 5
p = x + 2*r*cos(j*pi/3 + c)
q = y + 2*r*sin(j*pi/3 + c)

rr = 1.03*r
for k = 0 to 5
u = k*pi/3 + a

circle (p + 0.5*rr*cos(u), q - 0.5*rr*sin(u)), 0.5*rr,, u+b, u+pi
circle (p + 0.5*rr*cos(u+ b), q - 0.5*rr*sin(u+ b)), 0.5*rr,, u, u+pi+b
'paint (p + 0.5*rr*cos(u+0.2), q - 0.5*rr*sin(u+0.2))
next
next
next

« Last Edit: June 28, 2019, 10:10:39 pm by _vince »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #25 on: June 28, 2019, 10:43:59 pm »
Looks good _vince!

I'm up to twirling:
Code: QB64: [Select]
  1. _TITLE "Crescent Pattern by Half Circle Arcs"
  2. CONST xmax = 800, ymax = 600, pi = 3.14159265
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4. DIM aof
  5. COLOR &HFF000000, &HFFFFFFFF
  6.     CLS
  7.     aof = aof + 4 * pi / 36
  8.     crescent xmax / 2, ymax / 2, 50, aof
  9.     _DISPLAY
  10.     _LIMIT 10
  11. SUB crescent (x0, y0, r6, aoff) 'r6 is radius of 6 crescent pattern
  12.     DIM a12, a, r1, x12, y12, i, px, py
  13.     r1 = r6 / 2 ' the radius of each crescent
  14.     a12 = 2 * pi / 12 ' 30 degrees to draw 12 arcs about x0, y0
  15.     FOR i = 0 TO 11 'draw 12 arcs
  16.         'we need origin of the 12 arcs
  17.         x12 = x0 + r1 * COS(i * a12 + aoff)
  18.         y12 = y0 + r1 * SIN(i * a12 + aoff)
  19.         'arc start angle = a end angle = a + pi = 1/2 circle
  20.         arc x12, y12, r1, i * a12 + aoff - pi / 6, i * a12 + aoff + pi, &HFFFFFFFE '<<<<<<<<<<<<<<<<<<<<<<<<<<<<  start angle earlier
  21.     NEXT
  22.     FOR i = 0 TO 11
  23.         IF i MOD 2 = 0 THEN
  24.             px = x0 + r1 * COS(i * a12 + aoff + pi / 12)
  25.             py = y0 + r1 * SIN(i * a12 + aoff + pi / 12)
  26.             'CIRCLE (px, py), 2 'test for paint
  27.             PAINT (px, py), &HFF000000, &HFFFFFFFE 'leaks try line arc
  28.         END IF
  29.     NEXT
  30.  
  31.  
  32. SUB arc (x, y, r, raStart, raStop, c AS _UNSIGNED LONG)
  33.     'x, y origin, r = radius, c = color
  34.  
  35.     'raStart is first angle clockwise from due East = 0 degrees
  36.     ' arc will start drawing there and clockwise until raStop angle reached
  37.  
  38.     DIM al, a
  39.     IF raStop < raStart THEN
  40.         arc x, y, r, raStart, _PI(2), c
  41.         arc x, y, r, 0, raStop, c
  42.     ELSE
  43.         ' modified to easier way suggested by Steve
  44.         'Why was the line method not good? I forgot.
  45.         al = _PI * r * r * (raStop - raStart) / _PI(2)
  46.         FOR a = raStart TO raStop STEP 1 / al
  47.             PSET (x + r * COS(a), y + r * SIN(a)), c
  48.         NEXT
  49.     END IF
  50.  
« Last Edit: June 28, 2019, 10:45:26 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #26 on: June 29, 2019, 05:03:00 am »
@Bplus

thanks to translate my poor discussion
Quote
I cannot face  again with  PAINT and CIRCLE lacking color.... not now!
And I have no knownledge that CircleFill and its brothers can draw arches!
... or is there  no lacking color for halfcircle?

in code experience that others can have
Quote
Hi TempodiBasic,

This must be what you are trying to say:
OPTION _EXPLICIT
_TITLE "Crescent Pattern by Half Circle Arcs"
CONST xmax = 800, ymax = 600, pi = 3.14159265
SCREEN _NEWIMAGE(xmax, ymax, 32)
 
crescent xmax / 2, ymax / 2, 50
 
SUB crescent (x0, y0, r6) 'r6 is radius of 6 crescent pattern
    DIM a12, a, r1, x12, y12, stopa
    r1 = r6 / 2 ' the radius of each crescent
    a12 = 2 * pi / 12 ' 30 degrees to draw 12 arcs about x0, y0
    FOR a = 0 TO 2 * pi STEP a12 'draw 12 arcs
        'we need origin of the 12 arcs
        x12 = x0 + r1 * COS(a)
        y12 = y0 + r1 * SIN(a)
        'arc start angle = a end angle = a + pi = 1/2 circle
        IF a + pi > 2 * pi THEN
            stopa = 2 * pi - (a + pi)
            CIRCLE (x12, y12), r1, , a, 2 * pi '<<< as I've said before CIRCLE sub sucks!
            CIRCLE (x12, y12), r1, , 0, stopa
        ELSE
            CIRCLE (x12, y12), r1, , a, a + pi
        END IF
    NEXT
END SUB
 

However great I see that now we have a new function for toolbox section!  :-)

Code: QB64: [Select]
  1.  
  2. SUB arc (x, y, r, raStart, raStop, c AS _UNSIGNED LONG)
  3.     'x, y origin, r = radius, c = color
  4.  
  5.     'raStart is first angle clockwise from due East = 0 degrees
  6.     ' arc will start drawing there and clockwise until raStop angle reached
  7.  
  8.     DIM al, a
  9.     IF raStop < raStart THEN
  10.         arc x, y, r, raStart, _PI(2), c
  11.         arc x, y, r, 0, raStop, c
  12.     ELSE
  13.         ' modified to easier way suggested by Steve
  14.         'Why was the line method not good? I forgot.
  15.         al = _PI * r * r * (raStop - raStart) / _PI(2)
  16.         FOR a = raStart TO raStop STEP 1 / al
  17.             PSET (x + r * COS(a), y + r * SIN(a)), c
  18.         NEXT
  19.     END IF

Very cool boys and girls of QB64! Very very cool!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Cresent Pattern Challenge from JB
« Reply #27 on: June 29, 2019, 11:12:10 am »
TempodiBasic, you are not the only one confused by Drawing Arcs with CIRCLE. I learned Basic trig functions before using CIRCLE for Arcs and I find it hard to think backwards for CIRCLE command.

Why the CIRCLE command works Counter-Clockwise with the start and end angles is beyond me but it is inconsistent with all the Basic trig functions that go Clockwise around origin. It doesn't even make sense with the Y-axis increasing from due East 0 degrees or radians.

So yeah, if you need a Arc command that works consistently with the other Basic commands or functions put this Arc sub in your toolbox. It's even been reviewed by Steve. :-))

I think it always gets enough points to avoid leaks if you use the line as a border for PAINTing. Originally I used a line to connect the dots to assure no leaks (and you can do with less dots) but Steve did not like that one so I changed to just dots.

@Steve if you remember the reason, I wouldn't mind being reminded. :-)

Update: with twirling the patterns, they draw too slow to do a screen full of twirlers, so I will use Petr's method of snap shot images. I also like the shape of crescent pattern drawn originally, trailing edge is ellipse or ellipse like but not a circle arc or at least not a painted circular arc that leaves holes in middle and won't look right close to surrounding crescent patterns. Maybe I will try a line method to fill between leading and trailing edge of circular arcs first.
« Last Edit: June 29, 2019, 11:24:43 am by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Cresent Pattern Challenge from JB
« Reply #28 on: June 29, 2019, 11:45:01 am »
A different approach to this (I think; I haven't tried all the other demos fully, yet):

Code: [Select]
DEFLNG A-Z
SCREEN _NEWIMAGE(640, 480, 32)
DIM Colors(-1 TO 0)
Colors(-1) = &HFFFF0000 'Red
Colors(0) = &HFF0000FF 'Blue


FOR i = 0 TO 8
    k = NOT k
    x = 25 * SIN(_D2R(30 * i)) + 50
    y = 25 * COS(_D2R(30 * i)) + 50
    CIRCLE (x, y), 25, Colors(k)
    PAINT (x, y), Colors(k)
NEXT

halfimage = _NEWIMAGE(50, 100, 32)
_PUTIMAGE , 0, halfimage, (50, 0)-(100, 100)
fullimage = _NEWIMAGE(100, 100, 32)
_DEST fullimage
DisplayImage halfimage, 50, 0, 0, 1
DisplayImage halfimage, 0, 0, 180, 4
_DEST 0

CLS , Colors(-1)

DO
    angle = (angle + 1) MOD 360
    FOR y = -50 TO _HEIGHT + 50 STEP 82
        Insert = NOT Insert
        FOR x = -50 TO _WIDTH + 50 STEP 100
            DisplayImage fullimage, x + Insert * 50, y, angle, 0
        NEXT
    NEXT
    _LIMIT 120
    _DISPLAY
LOOP UNTIL _KEYHIT


SUB PlacePattern (Xwhere, Ywhere, WhichPattern)
    DisplayImage WhichPattern, Xwhere, Ywhere, 0, 1
    DisplayImage tempimage, Xwhere - 50, Ywhere, 180, 4
END SUB



SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
    'Image is the image handle which we use to reference our image.
    'x,y is the X/Y coordinates where we want the image to be at on the screen.
    'angle is the angle which we wish to rotate the image.
    'mode determines HOW we place the image at point X,Y.
    'Mode 0 we center the image at point X,Y
    'Mode 1 we place the Top Left corner of oour image at point X,Y
    'Mode 2 is Bottom Left
    'Mode 3 is Top Right
    'Mode 4 is Bottom Right


    DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
    DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
    w = _WIDTH(Image): h = _HEIGHT(Image)
    SELECT CASE mode
        CASE 0 'center
            px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
            px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
        CASE 1 'top left
            px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
            px(1) = 0: py(1) = h: px(2) = w: py(2) = h
        CASE 2 'bottom left
            px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
            px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
        CASE 3 'top right
            px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
            px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
        CASE 4 'bottom right
            px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
            px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
    END SELECT
    sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
    FOR i = 0 TO 3
        x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
        px(i) = x2: py(i) = y2
    NEXT
    _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
    _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
END SUB

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Cresent Pattern Challenge from JB
« Reply #29 on: June 29, 2019, 11:51:27 am »
And a slight mod to the above, to give the "cogs" directionality, so that they appear as if they'd actually be turning as we'd expect to see in a machine.

Code: [Select]
DEFLNG A-Z
SCREEN _NEWIMAGE(640, 480, 32)
DIM Colors(-1 TO 0)
Colors(-1) = &HFFFF0000 'Red
Colors(0) = &HFF0000FF 'Blue


FOR i = 0 TO 8
    k = NOT k
    x = 25 * SIN(_D2R(30 * i)) + 50
    y = 25 * COS(_D2R(30 * i)) + 50
    CIRCLE (x, y), 25, Colors(k)
    PAINT (x, y), Colors(k)
NEXT

halfimage = _NEWIMAGE(50, 100, 32)
_PUTIMAGE , 0, halfimage, (50, 0)-(100, 100)
fullimage = _NEWIMAGE(100, 100, 32)
_DEST fullimage
DisplayImage halfimage, 50, 0, 0, 1
DisplayImage halfimage, 0, 0, 180, 4
_DEST 0

CLS ', Colors(-1)

DO
    angle = (angle + 1) MOD 360
    FOR y = -50 TO _HEIGHT + 50 STEP 82
        Insert = NOT Insert
        IF Insert THEN direction = -1 ELSE direction = 1
        FOR x = -50 TO _WIDTH + 50 STEP 100
            DisplayImage fullimage, x + Insert * 50, y, direction * angle, 0
        NEXT
    NEXT
    _DISPLAY
LOOP UNTIL _KEYHIT


SUB PlacePattern (Xwhere, Ywhere, WhichPattern)
    DisplayImage WhichPattern, Xwhere, Ywhere, 0, 1
    DisplayImage tempimage, Xwhere - 50, Ywhere, 180, 4
END SUB



SUB DisplayImage (Image AS LONG, x AS INTEGER, y AS INTEGER, angle AS SINGLE, mode AS _BYTE)
    'Image is the image handle which we use to reference our image.
    'x,y is the X/Y coordinates where we want the image to be at on the screen.
    'angle is the angle which we wish to rotate the image.
    'mode determines HOW we place the image at point X,Y.
    'Mode 0 we center the image at point X,Y
    'Mode 1 we place the Top Left corner of oour image at point X,Y
    'Mode 2 is Bottom Left
    'Mode 3 is Top Right
    'Mode 4 is Bottom Right


    DIM px(3) AS INTEGER, py(3) AS INTEGER, w AS INTEGER, h AS INTEGER
    DIM sinr AS SINGLE, cosr AS SINGLE, i AS _BYTE
    w = _WIDTH(Image): h = _HEIGHT(Image)
    SELECT CASE mode
        CASE 0 'center
            px(0) = -w \ 2: py(0) = -h \ 2: px(3) = w \ 2: py(3) = -h \ 2
            px(1) = -w \ 2: py(1) = h \ 2: px(2) = w \ 2: py(2) = h \ 2
        CASE 1 'top left
            px(0) = 0: py(0) = 0: px(3) = w: py(3) = 0
            px(1) = 0: py(1) = h: px(2) = w: py(2) = h
        CASE 2 'bottom left
            px(0) = 0: py(0) = -h: px(3) = w: py(3) = -h
            px(1) = 0: py(1) = 0: px(2) = w: py(2) = 0
        CASE 3 'top right
            px(0) = -w: py(0) = 0: px(3) = 0: py(3) = 0
            px(1) = -w: py(1) = h: px(2) = 0: py(2) = h
        CASE 4 'bottom right
            px(0) = -w: py(0) = -h: px(3) = 0: py(3) = -h
            px(1) = -w: py(1) = 0: px(2) = 0: py(2) = 0
    END SELECT
    sinr = SIN(angle / 57.2957795131): cosr = COS(angle / 57.2957795131)
    FOR i = 0 TO 3
        x2 = (px(i) * cosr + sinr * py(i)) + x: y2 = (py(i) * cosr - px(i) * sinr) + y
        px(i) = x2: py(i) = y2
    NEXT
    _MAPTRIANGLE (0, 0)-(0, h - 1)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
    _MAPTRIANGLE (0, 0)-(w - 1, 0)-(w - 1, h - 1), Image TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
END SUB
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!