Click here to Skip to main content
15,867,568 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

 
Questionmy vote of 5 Pin
Southmountain17-Feb-23 6:07
Southmountain17-Feb-23 6:07 
QuestionCan you send me the demo of the resource - Mardani Dani Pin
Jubiababa5-Nov-21 11:34
Jubiababa5-Nov-21 11:34 
QuestionWOW SUPER GREAT BRO!!! Pin
Member 1459295423-Oct-19 19:07
Member 1459295423-Oct-19 19:07 
Questionnone Pin
Homero Rivera27-Nov-13 9:52
Homero Rivera27-Nov-13 9:52 
GeneralCan you send to me the resouce demo to my email(yangshenglislsl@163.com)? thank you Pin
fengzhiyou23-Nov-12 4:57
fengzhiyou23-Nov-12 4:57 
GeneralRe: Can you send to me the resouce demo to my email(yangshenglislsl@163.com)? thank you Pin
Mardani Dani9-Jan-13 20:02
Mardani Dani9-Jan-13 20:02 
Generalit's great! Pin
frank_fan27-Sep-12 19:15
frank_fan27-Sep-12 19:15 
GeneralRe: it's great! Pin
Mardani Dani10-Oct-12 15:30
Mardani Dani10-Oct-12 15:30 
GeneralMy vote of 5 Pin
suryakant4it17-Sep-12 3:30
suryakant4it17-Sep-12 3:30 
GeneralRe: My vote of 5 Pin
Mardani Dani10-Oct-12 15:31
Mardani Dani10-Oct-12 15:31 
GeneralNice work Pin
Kenneth Haugland30-Jul-12 0:16
mvaKenneth Haugland30-Jul-12 0:16 
GeneralRe: Nice work Pin
Mardani Dani3-Nov-12 23:31
Mardani Dani3-Nov-12 23:31 
When the sun rises from the west and the sunset from the east! Smile | :)
GeneralMy vote of 5 Pin
Mohammad A Rahman25-Mar-12 17:05
Mohammad A Rahman25-Mar-12 17:05 
GeneralRe: My vote of 5 Pin
Mardani Dani26-Mar-12 15:06
Mardani Dani26-Mar-12 15:06 
QuestionNice work. Pin
kartalyildirim3-Mar-12 6:34
kartalyildirim3-Mar-12 6:34 
GeneralRe: Nice work. Pin
Mardani Dani26-Mar-12 14:39
Mardani Dani26-Mar-12 14:39 
GeneralMy vote of 4 Pin
Sushil Mate22-Feb-12 19:49
Sushil Mate22-Feb-12 19:49 
GeneralRe: My vote of 4 Pin
Mardani Dani26-Mar-12 14:35
Mardani Dani26-Mar-12 14:35 
QuestionUsing C to Blend Mathematics and Art (When Math goes Beautiful) Pin
Sunarya31-Jan-12 16:15
Sunarya31-Jan-12 16:15 
AnswerRe: Using C to Blend Mathematics and Art (When Math goes Beautiful) Pin
Mardani Dani31-Jan-12 17:31
Mardani Dani31-Jan-12 17:31 
QuestionBeautiful work! Pin
ChemDave31-Jan-12 6:57
ChemDave31-Jan-12 6:57 
AnswerRe: Beautiful work! Pin
Mardani Dani31-Jan-12 11:24
Mardani Dani31-Jan-12 11:24 
GeneralMy vote of 5 Pin
steferson31-Jan-12 2:31
steferson31-Jan-12 2:31 
GeneralRe: My vote of 5 Pin
Mardani Dani31-Jan-12 5:27
Mardani Dani31-Jan-12 5:27 
GeneralMy vote of 5 Pin
Member 428961330-Jan-12 22:54
Member 428961330-Jan-12 22:54 

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.