Optical Illusions
Generating a Pair of Optical Illusions in SVG Format
|
#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. | |
void CloseSVG | ( | FILE *& | output | ) |
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.
output | Output file pointer. |
cx | X coordinate of center of image in pixels. |
cy | Y coordinate of center of image in pixels. |
r | Radius of circle. |
r0 | Long radius of ellipses. |
r1 | Short radius of ellipses. |
n | Number of ellipses in ring. |
theta | Angle to first ellipse. |
dtheta | Angle delta. |
parity | True if first ellipse is black, false if white. |
flip | True to clip the ordering of colots of ellipses. |
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.
output | File pointer. |
cx | Image center x. |
cy | Image center y. |
r | Circle radius in pixels. |
sw | Square width and height. |
parity | Square initial orientation parity. |
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.
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.
The outer circle of a ring is similar to the inner circle but has black and white interchanged.
Used for optical illusion 2.
output | Pointer to output file. |
cx | X coordinate of center of image in pixels. |
cy | Y coordinate of center of image in pixels. |
r | Radius of braid. |
r0 | Long radius of ellipses. |
r1 | Short radius of ellipses. |
n | Number of ellipses in ring. |
flip | True to flip the ordering of colors of ellipses. |
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.
bool OpenSVG | ( | FILE *& | output, |
const std::string & | fname, | ||
size_t | w, | ||
size_t | h | ||
) |
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.
fname | File name without extension. |
w | Width and height of image in pixels. |
n | Number of circles. |
r0 | Initial circle radius. |
dr | Radius delta. |
sw | Width of squares. |
dark | A dark SVG color. |
light | A light SVG color. |
bgclr | A mid-range SVG color for the background. |
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.
fname | File name without extension. |
w | Width and height of image in pixels. |
n | Number of ellipses in ring. |
r | Radius of braid. |
r0 | Long radius of ellipses. |
r1 | Short radius of ellipses. |
dark | A dark SVG color. |
light | A light SVG color. |
bgclr | A mid-range SVG color for the background. |
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.
output | Output file pointer. |
i | Ellipse index about circle. |
parity | True if first ellipse is black, false if white. |