Author Topic: Optical Illusion  (Read 237 times)

Optical Illusion
« on: January 12, 2019, 04:21:19 PM »
Found interesting optical illusion on Internet, was curious if I could duplicate it.
Code: [Select]
_TITLE "Do the dots in disk look like they are spinning?" ' B+ 2019-01-12
'try an optical illusion saw on Internet

CONST xmax = 600
CONST ymax = 600
SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 300, 60

x0 = xmax / 2: y0 = ymax / 2: a24 = _PI(2 / 24): r = 240
WHILE _KEYHIT <> 27
    IF loopcnt < 2 THEN stopit = 11
    IF loopcnt = 2 THEN stopit = 0
    IF loopcnt > 2 THEN
        IF stopit < 11 THEN stopit = stopit + 1
    END IF
    FOR a = 0 TO _PI(2) STEP _PI / 180
        COLOR _RGB32(128, 0, 0): fcirc x0, y0, 251
        FOR i = 0 TO stopit
            IF loopcnt > 1 THEN
                xs = x0 + r * COS(a24 * i)
                ys = y0 + r * SIN(a24 * i)
                xe = x0 + r * COS(a24 * i + _PI)
                ye = y0 + r * SIN(a24 * i + _PI)
                LINE (xs, ys)-(xe, ye), _RGB32(255, 255, 255)
            END IF
            x = x0 + COS(a + _PI(i / 12)) * r * COS(a24 * i)
            y = y0 + COS(a + _PI(i / 12)) * r * SIN(a24 * i)
            COLOR _RGB32(255, 255, 255)
            fcirc x, y, 10
        NEXT
        _DISPLAY
        _LIMIT 90
    NEXT
    loopcnt = loopcnt + 1
WEND

'Steve McNeil's  copied from his forum   note: Radius is too common a name
SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
    DIM subRadius AS LONG, RadiusError AS LONG
    DIM X AS LONG, Y AS LONG

    subRadius = ABS(R)
    RadiusError = -subRadius
    X = subRadius
    Y = 0

    IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB

    ' Draw the middle span here so we don't draw it twice in the main loop,
    ' which would be a problem with blending turned on.
    LINE (CX - X, CY)-(CX + X, CY), , BF

    WHILE X > Y
        RadiusError = RadiusError + Y * 2 + 1
        IF RadiusError >= 0 THEN
            IF X <> Y + 1 THEN
                LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
                LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
            END IF
            X = X - 1
            RadiusError = RadiusError - X * 2
        END IF
        Y = Y + 1
        LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
        LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
    WEND
END SUB
B = B + ...

Re: Optical Illusion
« Reply #1 on: January 12, 2019, 04:49:44 PM »
Again, this is just dots going back and forth on a line!
Code: [Select]
_TITLE "Do the dots in disk look like they are spinning?" ' B+ 2019-01-12
'try an optical illusion saw on Internet

CONST xmax = 600
CONST ymax = 600
SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 300, 60

x0 = xmax / 2: y0 = ymax / 2: a24 = _PI(2 / 24): r = 240
WHILE _KEYHIT <> 27
    FOR a = 0 TO _PI(2) STEP _PI / 180
        COLOR _RGB32(128, 0, 0): fcirc x0, y0, 251
        FOR i = 0 TO 23
            IF loopcnt > 4 THEN
                xs = x0 + r * COS(a24 * i)
                ys = y0 + r * SIN(a24 * i)
                xe = x0 + r * COS(a24 * i + _PI)
                ye = y0 + r * SIN(a24 * i + _PI)
                LINE (xs, ys)-(xe, ye), _RGB32(255, 255, 255)
            END IF
            x = x0 + COS(a + _PI(i / 6)) * r * COS(a24 * i)
            y = y0 + COS(a + _PI(i / 6)) * r * SIN(a24 * i)
            COLOR _RGB32(i * 10, (23 - i) * 10, 0)
            fcirc x, y, 10
        NEXT
        _DISPLAY
        _LIMIT 90
    NEXT
    loopcnt = loopcnt + 1
WEND

'Steve McNeil's  copied from his forum   note: Radius is too common a name
SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
    DIM subRadius AS LONG, RadiusError AS LONG
    DIM X AS LONG, Y AS LONG

    subRadius = ABS(R)
    RadiusError = -subRadius
    X = subRadius
    Y = 0

    IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB

    ' Draw the middle span here so we don't draw it twice in the main loop,
    ' which would be a problem with blending turned on.
    LINE (CX - X, CY)-(CX + X, CY), , BF

    WHILE X > Y
        RadiusError = RadiusError + Y * 2 + 1
        IF RadiusError >= 0 THEN
            IF X <> Y + 1 THEN
                LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
                LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
            END IF
            X = X - 1
            RadiusError = RadiusError - X * 2
        END IF
        Y = Y + 1
        LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
        LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
    WEND
END SUB

B = B + ...

Offline Pete

  • Cuz I sez so, varmint!
Re: Optical Illusion
« Reply #2 on: January 12, 2019, 05:53:25 PM »
Oh I get it now, bplus isn't a grade, it stands for balls plus! It's some kind of cognitive  illusion!

Well that was cool! I could follow the straight line movement just find but as soon as you lose focus, bam, they appear to be traveling in a circle.

Nice recreation!

Pete


Offline johnno56

  • Live long and prosper.
Re: Optical Illusion
« Reply #3 on: January 13, 2019, 08:30:41 AM »
Both are very clever. Discs travelling in a straight line, but due to the timing, appear to travel in circles. Nicely done!

I only have one question. Do they come in blue? lol

J
Logic is the beginning of wisdom.

Offline FellippeHeitor

  • QB64 Developer
  • LET IT = BE
    • QB64.org
Re: Optical Illusion
« Reply #4 on: January 13, 2019, 08:54:15 AM »
I too had seen this online a good while back and tried writing it but the math involved just flew over my head and I gave up before I closed my first DO LOOP.

Great job, bplus! Really fun to watch - I like the "trick explained" mode in the first program.

Re: Optical Illusion
« Reply #5 on: January 13, 2019, 09:55:33 AM »
Yes, lovely.

Offline Pete

  • Cuz I sez so, varmint!
Re: Optical Illusion
« Reply #6 on: January 13, 2019, 12:06:47 PM »
Both are very clever. Discs travelling in a straight line, but due to the timing, appear to travel in circles. Nicely done!

I only have one question. Do they come in blue? lol

J

LOL! Silly question. Mark lives in Canada, so of course his balls come in blue!

Pete :D

Re: Optical Illusion
« Reply #7 on: January 13, 2019, 01:44:44 PM »
Thanks guys!

Blue balls? Here are some magic ones:
Code: [Select]
_TITLE "Magic Blue Balls?" ' OI #4 Blue Balls.bas B+ 2019-01-13
'try another optical illusion saw on Internet

CONST xmax = 1100
CONST ymax = 700
SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 100, 0

COLOR _RGBA32(255, 255, 255, 100), _RGB32(0, 0, 80)
CLS
FOR i = 40 TO 1040 STEP 50
    LINE (i, 0)-(i + 10, ymax), , BF
    LINE (0, i)-(xmax, i + 10), , BF
NEXT
COLOR _RGB32(255, 255, 255)
FOR y = 45 TO 645 STEP 50
    FOR x = 45 TO 1045 STEP 50
        fcirc x, y, 8
    NEXT
NEXT
SLEEP
'Steve McNeil's  copied from his forum   note: Radius is too common a name
SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
    DIM subRadius AS LONG, RadiusError AS LONG
    DIM X AS LONG, Y AS LONG

    subRadius = ABS(R)
    RadiusError = -subRadius
    X = subRadius
    Y = 0

    IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB

    ' Draw the middle span here so we don't draw it twice in the main loop,
    ' which would be a problem with blending turned on.
    LINE (CX - X, CY)-(CX + X, CY), , BF

    WHILE X > Y
        RadiusError = RadiusError + Y * 2 + 1
        IF RadiusError >= 0 THEN
            IF X <> Y + 1 THEN
                LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
                LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
            END IF
            X = X - 1
            RadiusError = RadiusError - X * 2
        END IF
        Y = Y + 1
        LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
        LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
    WEND
END SUB

B = B + ...

Re: Optical Illusion
« Reply #8 on: January 13, 2019, 03:42:19 PM »
Blue ball buster:
Code: [Select]
_TITLE "Crazy Squares in Concentric Circles" ' OI #3 crazy sqrs on concentric circles.bas B+ 2019-01-13
'try another optical illusion saw on Internet

CONST xmax = 700
CONST ymax = 700
SCREEN _NEWIMAGE(xmax, ymax, 32)
_SCREENMOVE 300, 0

side = 20: cx = xmax / 2: cy = ymax / 2
WHILE _KEYHIT <> 27
    COLOR , _RGB32(60, 60, 100): CLS
    nstars = 1: starN = 1: d = 0
    FOR r = 1 TO 5
        d = d + 5
        rr = r * (30 + d)
        IF r MOD 2 THEN a = _PI(1 / 12) ELSE a = _PI(-1 / 12)
        circ = 2 * _PI * rr
        n = INT(circ / (1.2 * side))
        IF n MOD 2 THEN n = n - 1
        FOR i = 1 TO n
            x = cx + rr * COS(i * 2 * _PI / n)
            y = cy + rr * SIN(i * 2 * _PI / n)
            starN = starN + 1
            IF starN MOD 2 THEN COLOR _RGB32(0, 0, 255) ELSE COLOR _RGB(0, 0, 60)
            box x, y, i * 2 * _PI / n + a, side
            PAINT (x, y)
        NEXT
    NEXT
    _DELAY 3
    d = 0
    FOR r = 1 TO 5
        d = d + 5
        rr = r * (30 + d)
        CIRCLE (cx, cy), rr, _RGB32(255, 255, 255)
    NEXT
    _DELAY 1
WEND

SUB box (xCenter, yCenter, rAngle, side)
    p45 = _PI(.25)
    p135 = 3 * p45
    stside = .5 * side * SQR(2)
    x1 = xCenter + stside * COS(rAngle + p45)
    y1 = yCenter + stside * SIN(rAngle + p45)
    x2 = xCenter + stside * COS(rAngle + p135)
    y2 = yCenter + stside * SIN(rAngle + p135)
    x3 = xCenter + stside * COS(rAngle - p135)
    y3 = yCenter + stside * SIN(rAngle - p135)
    x4 = xCenter + stside * COS(rAngle - p45)
    y4 = yCenter + stside * SIN(rAngle - p45)
    LINE (x1, y1)-(x2, y2)
    LINE (x2, y2)-(x3, y3)
    LINE (x3, y3)-(x4, y4)
    LINE (x4, y4)-(x1, y1)
END SUB

B = B + ...

Offline Pete

  • Cuz I sez so, varmint!
Re: Optical Illusion
« Reply #9 on: January 13, 2019, 03:51:07 PM »
Have you considered therapy? Under the Canadian system, some other poor schmuck will pay for it.

I knew they were circles right off the bat but it does look like a spiral when the squares are slanted. This stuff probably comes to us from an alternate universe. One where QB64 does not exist, and the Bill there needs something to keep himself occupied. I'm just saying...

Pete 

Re: Optical Illusion
« Reply #10 on: January 13, 2019, 05:10:19 PM »
\_ ? _/  Canada?  Is across the lake from me.
    |
   /\
  \  |
B = B + ...

Offline Pete

  • Cuz I sez so, varmint!
Re: Optical Illusion
« Reply #11 on: January 13, 2019, 05:54:47 PM »
That just means you live on the east or west bank of said lake... in Canada!

Pete

You can time some of the fools all the people, and time all of the fools some of the people, but you can't time all of the fools all of the people -FreeBASIC

Offline johnno56

  • Live long and prosper.
Re: Optical Illusion
« Reply #12 on: January 14, 2019, 07:33:30 AM »
Perfect! Everything is blue... the optic nerves are shot... but at least everything is blue...

J
Logic is the beginning of wisdom.