Optical Illusions
Generating a Pair of Optical Illusions in SVG Format
Macros | Functions | Variables
main.cpp File Reference
#include <math.h>
#include <stdio.h>
#include <string>

Go to the source code of this file.

Macros

#define _USE_MATH_DEFINES
 Generate a pair of optical illusions in SVG format.
 

Functions

bool OpenSVG (FILE *&output, const std::string &fname, size_t w, size_t h)
 Open SVG file. More...
 
void CloseSVG (FILE *&output)
 Close SVG file. More...
 
void DrawCircleOfSquares (FILE *output, size_t cx, size_t cy, float r, size_t sw, bool parity)
 Draw a circle of squares to a file in SVG format. More...
 
void OpticalIllusion1 (const std::string &fname, size_t w, size_t n, float r0, float dr, size_t sw, const char dark[], const char light[], const char bgclr[])
 Draw the first optical illusion to a file in SVG format. More...
 
void SelectEllipseColor (FILE *output, size_t i, bool parity)
 Select ellipse color based on index. More...
 
void DrawCircleOfEllipses (FILE *output, size_t cx, size_t cy, float r, float r0, float r1, size_t n, float theta, float dtheta, bool parity, size_t flip=999999)
 Draw circle of ellipses to a file in SVG format. More...
 
void DrawTripleCircle (FILE *output, size_t cx, size_t cy, float r, float r0, float r1, size_t n, bool flip=false)
 Draw 3 concentric circles of ellipses to a file in SVG format. More...
 
void OpticalIllusion2 (const std::string &fname, size_t w, size_t n, float r, float r0, float r1, const char dark[], const char light[], const char bgclr[])
 Draw the second optical illusion to a file in SVG format. More...
 
int main ()
 Main. More...
 

Variables

const float PI = 3.14159265358979323846f
 Pi.
 

Function Documentation

◆ CloseSVG()

void CloseSVG ( FILE *&  output)

Close SVG file.

Print a close svg tag and close the SVG file.

Parameters
outputReference to file pointer.

Definition at line 79 of file main.cpp.

◆ DrawCircleOfEllipses()

void DrawCircleOfEllipses ( FILE *  output,
size_t  cx,
size_t  cy,
float  r,
float  r0,
float  r1,
size_t  n,
float  theta,
float  dtheta,
bool  parity,
size_t  flip = 999999 
)

Draw circle of ellipses to a file in SVG format.

Draw a circle of elipses oriented so that the long axis of eah ellipse is perpendicular to a line drawn from the center of the circles to the center of the ellipse. This function outputs SVG transform and SVG ellipse tags to the output file. Used for optical illusion 2.

Parameters
outputOutput file pointer.
cxX coordinate of center of image in pixels.
cyY coordinate of center of image in pixels.
rRadius of circle.
r0Long radius of ellipses.
r1Short radius of ellipses.
nNumber of ellipses in ring.
thetaAngle to first ellipse.
dthetaAngle delta.
parityTrue if first ellipse is black, false if white.
flipTrue to clip the ordering of colots of ellipses.

Definition at line 232 of file main.cpp.

◆ DrawCircleOfSquares()

void DrawCircleOfSquares ( FILE *  output,
size_t  cx,
size_t  cy,
float  r,
size_t  sw,
bool  parity 
)

Draw a circle of squares to a file in SVG format.

This function outputs SVG transform and SVG rect tags to the output file, alternating between black and white. The squares are spaced apart by approximately half a square width and tilted slightly from the perpendicular to a line drawn from the center of the circle to the center of the square. The number of squares is chosen so as to fit the spacing constraint, which need not be exact for the optical illusion to work. Used for optical illusion 1.

OneRingOfSquares.svg
Parameters
outputFile pointer.
cxImage center x.
cyImage center y.
rCircle radius in pixels.
swSquare width and height.
paritySquare initial orientation parity.

Definition at line 113 of file main.cpp.

◆ DrawTripleCircle()

void DrawTripleCircle ( FILE *  output,
size_t  cx,
size_t  cy,
float  r,
float  r0,
float  r1,
size_t  n,
bool  flip = false 
)

Draw 3 concentric circles of ellipses to a file in SVG format.

This function calls DrawCircleOfEllipses() three times, once for each circle of ellipses. The middle circle is drawn first, then the inner circle, then the outer circle. The parameters for the calls DrawCircleOfEllipses() are chosen so as to achieve the following.

The inner circle starts with a black ellipse centered at the top and alternates with white ellipses each spaced roughly one ellipse long-axis apart for a total of 36 ellipses as shown in the next image.

ring1-middle.svg

The inner circle also has 36 ellipses, but it starts with a gap centered at the top with black ellipses to the left and right and a gap centered at the bottom with white ellipses to the left and right. Black and white alternate for the rest of the circle.

ring2-inner.svg

The outer circle of a ring is similar to the inner circle but has black and white interchanged.

ring3-outer.svg

Used for optical illusion 2.

Parameters
outputPointer to output file.
cxX coordinate of center of image in pixels.
cyY coordinate of center of image in pixels.
rRadius of braid.
r0Long radius of ellipses.
r1Short radius of ellipses.
nNumber of ellipses in ring.
flipTrue to flip the ordering of colors of ellipses.

Definition at line 293 of file main.cpp.

◆ main()

int main ( )

Main.

Create two optical illusions and save them as SVG files. The actual work is done by functions OpticalIllusion1() and OpticalIllusion2(), called with various parameters.

Returns
0.

Definition at line 367 of file main.cpp.

◆ OpenSVG()

bool OpenSVG ( FILE *&  output,
const std::string &  fname,
size_t  w,
size_t  h 
)

Open SVG file.

Open an SVG file for writing and print the header tag and an open svg tag.

Parameters
output[out] Reference to output file pointer.
fnameFile name without extension.
wImage width.
hImage height.
Returns
true if open succeeded.

Definition at line 50 of file main.cpp.

◆ OpticalIllusion1()

void OpticalIllusion1 ( const std::string &  fname,
size_t  w,
size_t  n,
float  r0,
float  dr,
size_t  sw,
const char  dark[],
const char  light[],
const char  bgclr[] 
)

Draw the first optical illusion to a file in SVG format.

The image consists of four concentric circles of tilted squares, alternating between light and dark squares. This function outputs an SVG style tag (the use of which refices the SVG file size) and the background rectangle tag, then calls DrawCircleOfSquares() once for each circle of squares required.

output1.svg
Parameters
fnameFile name without extension.
wWidth and height of image in pixels.
nNumber of circles.
r0Initial circle radius.
drRadius delta.
swWidth of squares.
darkA dark SVG color.
lightA light SVG color.
bgclrA mid-range SVG color for the background.

Definition at line 159 of file main.cpp.

◆ OpticalIllusion2()

void OpticalIllusion2 ( const std::string &  fname,
size_t  w,
size_t  n,
float  r,
float  r0,
float  r1,
const char  dark[],
const char  light[],
const char  bgclr[] 
)

Draw the second optical illusion to a file in SVG format.

The image consists of a pair of concentric rings, each of which is made up of three concentric circles of ellipses. This function outputs an SVG style tag (the use of which refices the SVG file size) and the background rectangle tag, then calls DrawTripleCircle() twice, once for each triplet of circles.

output2.svg
Parameters
fnameFile name without extension.
wWidth and height of image in pixels.
nNumber of ellipses in ring.
rRadius of braid.
r0Long radius of ellipses.
r1Short radius of ellipses.
darkA dark SVG color.
lightA light SVG color.
bgclrA mid-range SVG color for the background.

Definition at line 327 of file main.cpp.

◆ SelectEllipseColor()

void SelectEllipseColor ( FILE *  output,
size_t  i,
bool  parity 
)

Select ellipse color based on index.

If parity is true, ellipse is black when i%4=0, white when ji%4==2, and blank when i%4==1 and i%4==3. If parity is false, black and white are flipped. This function outputs the appropriate class name, class="b" for black and class="w" for white, to the output file. Used for optical illusion 2.

Parameters
outputOutput file pointer.
iEllipse index about circle.
parityTrue if first ellipse is black, false if white.

Definition at line 204 of file main.cpp.