Click here to Skip to main content
15,867,895 members
Articles / Programming Languages / C++

Using C to Blend Mathematics and Art (When Math goes Beautiful)

Rate me:
Please Sign up or sign in to vote.
4.86/5 (108 votes)
16 Dec 2011CPOL4 min read 130K   4.8K   164   87
Using C to blend Mathematics and Art that will produce a carving
Image 1

Introduction

Many people think that mathematics is a difficult thing where unsuspected turns out to be a beautiful thing. You may know about Cycloids, Epicycloids, Epitrochoids, Lissajous, Hypocycloids, and Hypotrochoids that are some cases in mathematics where a circle rolls around within another circle by an equation that traces of points of the two circles that are (X1,Y1) and (X2,Y2). All cases can be made as basic methods to make an art in mathematics. You can see more explanation about all cases in Wikipedia. Intuitively, I make some mathematical arts in C programming and I use a method that is similar with the all cases where the algorithm of the program is so simple. This time, the program has been improved but anyway there are many bugs that have not yet been fixed.

Background

Initially, I make a mathematical program about circle where most of circles rolling around within other circles with 2 up to no limit total of the circles (Circle on Circle on Circle on ........). Every circle has its unique rotation and radius that would be determined by its point for X(a),Y(a), where "a" is a sensitive angle. Every point of a circle will be the center for point of the next circle, and so on. To make it easy for you to imagine it, see the picture below where there are two circles that are red point with 4 radius rolling around black point with 2 radius where the black point is as center for the red point and "0,0" is as center point for the black point:

Download now

After I make the mathematical program, then I have an idea of how if every point is linked to another point with line. For example, 1st point will be linked to the 2nd point, the 2nd point will be linked to the 3rd point, and so on. With this method will be produced the beautiful pictures. In the program, I used 2 or 3 circles. To make it easy for you to understand, you can see in the demo file in slow drawing (slide show and full screen) by DOWN control on keyboard (Note: Up control to increase of the drawing speed). Some of mathematical functions that used are sin() and cos(), where "a" as the sensitive angle that will be determined the smoothness of pictures. Note that this is not a fractal, so there is no iteration.

Using the Code

The basics of equations for making a circle for "X,Y" are:

C++
X = CenterPointX + (Radius x sin(Angle))
Y = CenterPointY + (Radius x cos(Angle))

Here I simplify the "X,Y" in the C code into:

C++
CX = (Px/2) + (Py * sin(_AGL));
CY = (Py/2) + (Py * cos(_AGL));

Where CX = X and CY = Y, Px is Pixel for sb.x and Py is Pixel for sb.y, that is used to define the center point and radius with constant value, and then _AGL = Angle that in the equations are CX(_AGL), CY(_AGL)where per step is 0.5 degree, for example: X(0.5),Y(0.5) -> X(1),Y(1) -> X(1.5),Y(1.5) -> X(2),Y(2). The step angle will take effect to smoothness of the pictures that will produce a gradation of the color.

To make more variant of pictures, I modify the equations into:

C++
CX = (Px/2) + ((Py/4) * sin(_AGL*_COEF));
CY = (Py/2) + ((Py/4) * cos(_AGL*_COEF));
CX = (Px/2) + ((Py/4) * sin(_AGL*_COEF) * cos(_AGL*_COEF));
CY = (Py/2) + ((Py/4) * cos(_AGL*_COEF) * sin(_AGL*_COEF));

_COEF is the coefficient where the value is by random method per picture to produce many more variants of pictures that consist of curves. There are 3 type coefficients, that are _COEF for CURVE I, _COEF2 for CURVE II, and _COEF3 for CURVE III. The _COEF is how many times the CURVE I rolls around the center point per period, the _COEF2 is how many times the CURVE II rolls around the CURVE I per period, and the _COEF3 is how many times the CURVE III rolls around the CURVE II per period. Remember, circle on circle on circle where this is the basic principle to produce the mathematical art.

The program is divided into 3 types of art pictures, they are carving A, carving B, and graffiti where technically, they make no odds, but I am just exchanging between the equations. You can see the algorithm below that is actually so simple and there is no complex matter, and you can easily learn it.

FIRST, this is piece of a function (procedure) to make graffiti art (you can see the complete source code/MathArtAnimation()):

C++
int  Px,Py;
GetPixelValue(&Px,&Py);

do{
    static int _COEF  = -7+rand()%6,
               _COEF2 = -7+rand()%14,
               _COEF3 = -10+rand()%16,
               RandPnt1 = 2,
               RandPnt2 = 2,
               RandPnt3 = 2,
               RandPnt4 = 2;
    static double _AGL = 0;
    double CX,CY,Cx,Cy;

    SetTextColor(hdc, RGB(200,255,100));

    if(_AGL==360)
    {
        _COEF  = -7+rand()%6;
        _COEF2 = -7+rand()%14;
        _COEF3 = -10+rand()%16;
        _AGL   = 0;
        RandPnt1 = rand()%25;
        RandPnt2 = rand()%25;
        RandPnt3 = rand()%25;
        RandPnt4 = rand()%25;
    }
        _AGL += StepAngle();

    CX = (Px/2)+(Py/4)*sin(_AGL);
    CY = (Py/2)+(Py/4)*cos(_AGL);

    if(RandPnt1%2 == 0)
        CX = (Px/2)+(Py/4)*sin(_AGL*_COEF);
    if(RandPnt1%3 == 0)
        CY = (Py/2)+(Py/4)*cos(_AGL*_COEF);
    
    ///////////////////////////////////////////////////////////////CURVE I

Substitute the CX to Cx and CY to Cy:

C++
Cx = CX+(Py/7)*sin(_AGL*_COEF2);
Cy = CY+(Py/7)*cos(_AGL*_COEF2);

if(RandPnt2%2 == 0)
    Cx = CX+(Py/7)*sin(_AGL*_COEF)*cos(_AGL*_COEF2);
if(RandPnt2%3 == 0)
    Cy = CY+(Py/7)*cos(_AGL*_COEF)*sin(_AGL*_COEF2);
if(RandPnt2%4 == 0)
    Cx = CX+(Py/7)*cos(_AGL*_COEF2);
if(RandPnt2%5 == 0)
    Cy = CY+(Py/7)*sin(_AGL*_COEF2);

///////////////////////////////////////////////////////////////CURVE II

Substitute the Cx to Cx and Cy to Cy:

C++
/*Sb.X*/
if(RandPnt3%4 == 0)
    Cx = Cx+(Py/15)*sin(_AGL*_COEF3);
else
if(RandPnt3%3 == 0)
    Cx = Cx+(Py/20)*cos(_AGL*_COEF3);
else
if(RandPnt3%2 == 0)
    Cx = Cx+(Py/15)*sin(_AGL*_COEF2)*cos(_AGL*_COEF3);
/*Sb.Y*/
if(RandPnt4%4 == 0)
    Cy = Cy+(Py/15)*cos(_AGL*_COEF3);
else
if(RandPnt4%3 == 0)
    Cy = Cy+(Py/20)*sin(_AGL*_COEF3);
else
if(RandPnt4%2 == 0)
    Cy = Cy+(Py/15)*cos(_AGL*_COEF2)*sin(_AGL*_COEF3);

///////////////////////////////////////////////////////////////CURVE III

//PRINT
pen = CreatePen(PS_SOLID,1,RGB(245, 255, 200));
SelectObject(hdc,pen);
for(n=0; n<=Speed()-10; n++)
LineTo(hdc,Cx,Cy);

The picture below is one of the sample pictures that is generated with the algorithm above which the polynomial equations are selected by random method where the selected equations are:

C++
CX = (Px/2)+(Py/4)*sin(_AGL);
CY = (Py/2)+(Py/4)*cos(_AGL*_COEF);

Substitute to >

Cx = CX+(Py/7)*sin(_AGL*_COEF2);
Cy = CY+(Py/7)*sin(_AGL*_COEF2);

Substitute to >

Cx = Cx+(Py/15)*sin(_AGL*_COEF3);
Cy = Cy+(Py/15)*cos(_AGL*_COEF3);

Produces:

Picture Sample

Download now

Other Picture Samples

Download now

SECOND, this is piece of a function (procedure) to make carving A (you can see the complete source code/MathArtAnimation_2()):

C++
int  Px,Py;
GetPixelValue(&Px,&Py);

do{
    static int COEF      =  -13+rand()%10,
               RAND      =  rand()%12,
               COEF2     =  -13+rand()%10,
               RAND2     =  rand()%12;
    static double AGL = 0;
    double CX,CY,Cx,Cy;

    SetTextColor(hdc, RGB(200,255,100));

    if(AGL==720) {
        COEF  = -17+rand()%15;
        RAND  = rand()%12;
        COEF2 = -17+rand()%15;
        RAND2 = rand()%12;
        AGL	  = 0;
    }
        AGL += 0.5;

    if(RAND%6 == 0) {
        CX = (Px/2)+(Py/4)*sin(AGL*COEF);
        CY = (Py/2)+(Py/4)*cos(AGL*COEF);
    }
    else
    if(RAND%5 == 0) {
        CX = (Px/2)+(Px/4)*sin(AGL*COEF)*cos(AGL*COEF);
        CY = (Py/2)+(Py/4)*cos(AGL*COEF);
    }
    else
    if(RAND%4 == 0) {
        CX = (Px/2)+(Px/4)*cos(AGL*COEF);
        CY = (Py/2)+(Py/4)*sin(AGL*COEF);
    }
    else
    if(RAND%3 == 0) {
        CX = (Px/2)+(Px/4)*sin(AGL*COEF);
        CY = (Py/2)+(Py/4)*cos(AGL*COEF)*sin(AGL*COEF);
    }
    else
    if(RAND%2 == 0) {
        CX = (Px/2)+(Py/4)*cos(AGL*COEF);
        CY = (Py/2)+(Py/4)*sin(AGL*COEF);
    }
    else {
        CX = (Px/2)+(Px/4)*sin(AGL*COEF);
        CY = (Py/2)+(Py/4)*cos(AGL*COEF);
    }
    
    ///////////////////////////////////////////////////////////CURVE I

Substitute the CX to Cx and CY to Cy:

C++
if(RAND2%6 == 0) {
    Cx = CX+(Py/5)*sin(AGL*COEF2);
    Cy = CY+(Py/5)*cos(AGL*COEF2);
}
else
if(RAND2%5 == 0) {
    Cx = CX+(Px/5)*sin(AGL*COEF2)*cos(AGL*COEF2);
    Cy = CY+(Py/5)*cos(AGL*COEF2);
}
else
if(RAND2%4 == 0) {
    Cx = CX+(Px/5)*cos(AGL*COEF2);
    Cy = CY+(Py/5)*sin(AGL*COEF2);
}
else
if(RAND2%3 == 0) {
    Cx = CX+(Px/5)*sin(AGL*COEF2);
    Cy = CY+(Py/5)*cos(AGL*COEF2)*sin(AGL*COEF2);
}
else
if(RAND2%2 == 0) {
    Cx = CX+(Py/5)*cos(AGL*COEF2);
    Cy = CY+(Py/5)*sin(AGL*COEF2);
}
else {
    Cx = CX+(Px/5)*sin(AGL*COEF2);
    Cy = CY+(Py/5)*cos(AGL*COEF2);
}

///////////////////////////////////////////////////////////CURVE II


//PRINT
pen = CreatePen(PS_SOLID,1,RGB(245, 255, 200));
SelectObject(hdc,pen);
for(n=0;n<=Speed()-10;n++)
LineTo(hdc,Cx,Cy);
RoundRect(hdc, Cx,Cy, Cx+5,Cy+4, Cx+5,Cy+4);

The picture below is one of the sample pictures that is generated with the algorithm above which the polynomial equations are selected by random method where the selected equations are:

C++
CX = (Px/2)+(Py/4)*cos(AGL*COEF);
CY = (Py/2)+(Py/4)*sin(AGL*COEF);

Substitute to >

Cx = CX+(Px/5)*sin(AGL*COEF2);
Cy = CY+(Py/5)*cos(AGL*COEF2);

Produces:

Picture sample

Download now

Other Picture Samples

Download now

THIRD, carving B (you can see the function(procedure) in the source code/MathArtAnimation_3()):

Picture Samples

Download now

History

  • 14.12.2011: Initial post

About the Author

  • Mark Daniel
  • A Freelancer of C Programming and Architecture. Flight Simulator is my hobby.
  • Indonesia

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Indonesia Indonesia
I am a construction engineer in a mining industry. Graduated from Architecture Departement at the University of Diponegoro in 2008. C Programming and Flight Simulator (Boeing 747-200,full functions)are my hobbies.

Comments and Discussions

 
GeneralRe: My vote of 5 Pin
Mardani Dani31-Jan-12 1:01
Mardani Dani31-Jan-12 1:01 
GeneralMy vote of 4 Pin
ma897576930-Jan-12 19:08
ma897576930-Jan-12 19:08 
GeneralMy vote of 5 Pin
Kelvin Armstrong30-Jan-12 18:51
Kelvin Armstrong30-Jan-12 18:51 
GeneralRe: My vote of 5 Pin
Mardani Dani30-Jan-12 19:07
Mardani Dani30-Jan-12 19:07 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey30-Jan-12 17:51
professionalManoj Kumar Choubey30-Jan-12 17:51 
GeneralRe: My vote of 5 Pin
Mardani Dani30-Jan-12 18:53
Mardani Dani30-Jan-12 18:53 
QuestionWell done Mark Pin
mysorian30-Jan-12 15:57
professionalmysorian30-Jan-12 15:57 
AnswerRe: Well done Mark Pin
Mardani Dani30-Jan-12 17:06
Mardani Dani30-Jan-12 17:06 
QuestionMore Math-oriented code samples available using JavaScript/SVG Pin
ocampesato30-Jan-12 9:48
ocampesato30-Jan-12 9:48 
AnswerRe: More Math-oriented code samples available using JavaScript/SVG Pin
Mardani Dani30-Jan-12 15:37
Mardani Dani30-Jan-12 15:37 
GeneralMy vote of 5 Pin
Lakamraju Raghuram18-Jan-12 20:58
Lakamraju Raghuram18-Jan-12 20:58 
GeneralRe: My vote of 5 Pin
Mardani Dani19-Jan-12 2:45
Mardani Dani19-Jan-12 2:45 
QuestionGreat Project Pin
Dave Kerr9-Jan-12 22:50
mentorDave Kerr9-Jan-12 22:50 
AnswerRe: Great Project Pin
Mardani Dani10-Jan-12 3:10
Mardani Dani10-Jan-12 3:10 
GeneralMy vote of 4 Pin
ranjith m amin9-Jan-12 21:27
ranjith m amin9-Jan-12 21:27 
GeneralMy vote of 5 Pin
Halil ibrahim Kalkan9-Jan-12 0:11
Halil ibrahim Kalkan9-Jan-12 0:11 
GeneralRe: My vote of 5 Pin
Mardani Dani9-Jan-12 1:40
Mardani Dani9-Jan-12 1:40 
GeneralMy vote of 5 Pin
thatraja8-Jan-12 22:00
professionalthatraja8-Jan-12 22:00 
GeneralRe: My vote of 5 Pin
Mardani Dani8-Jan-12 22:41
Mardani Dani8-Jan-12 22:41 
QuestionVery nice Pin
Olivier_Giulieri21-Dec-11 16:25
Olivier_Giulieri21-Dec-11 16:25 
AnswerRe: Very nice Pin
Mardani Dani24-Dec-11 20:40
Mardani Dani24-Dec-11 20:40 
Questionmy 5 Pin
Shahriar Iqbal Chowdhury/Galib21-Dec-11 9:52
professionalShahriar Iqbal Chowdhury/Galib21-Dec-11 9:52 
AnswerRe: my 5 Pin
Mardani Dani24-Dec-11 13:06
Mardani Dani24-Dec-11 13:06 
GeneralMy vote of 5 Pin
David MacDermot20-Dec-11 12:44
David MacDermot20-Dec-11 12:44 
GeneralRe: My vote of 5 Pin
Mardani Dani24-Dec-11 20:11
Mardani Dani24-Dec-11 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.