Click here to Skip to main content
11,631,992 members (70,984 online)
Click here to Skip to main content

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

, 16 Dec 2011 CPOL 64.2K 4.1K 160
Rate this:
Please Sign up or sign in to vote.
Using C to blend Mathematics and Art that will produce a carving

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:

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

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

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:

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()):

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:

    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:

    /*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:

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()):

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:

    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:

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)

Share

About the Author

Mardani Dani
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.

You may also be interested in...

Comments and Discussions

 
Questionnone Pin
Homero Rivera27-Nov-13 9:52
memberHomero 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
memberfengzhiyou23-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
memberMardani Dani9-Jan-13 20:02 
Generalit's great! Pin
frank_fan27-Sep-12 19:15
memberfrank_fan27-Sep-12 19:15 
GeneralRe: it's great! Pin
Mardani Dani10-Oct-12 15:30
memberMardani Dani10-Oct-12 15:30 
GeneralMy vote of 5 Pin
suryakant4it17-Sep-12 3:30
membersuryakant4it17-Sep-12 3:30 
GeneralRe: My vote of 5 Pin
Mardani Dani10-Oct-12 15:31
memberMardani Dani10-Oct-12 15:31 
GeneralNice work Pin
Kenneth Haugland30-Jul-12 0:16
memberKenneth Haugland30-Jul-12 0:16 
GeneralRe: Nice work Pin
Mardani Dani3-Nov-12 23:31
memberMardani Dani3-Nov-12 23:31 
GeneralMy vote of 5 Pin
Mohammad A Rahman25-Mar-12 17:05
memberMohammad A Rahman25-Mar-12 17:05 
GeneralRe: My vote of 5 Pin
MarkDani326-Mar-12 15:06
memberMarkDani326-Mar-12 15:06 
QuestionNice work. Pin
kartalyildirim3-Mar-12 6:34
memberkartalyildirim3-Mar-12 6:34 
GeneralRe: Nice work. Pin
MarkDani326-Mar-12 14:39
memberMarkDani326-Mar-12 14:39 
GeneralMy vote of 4 Pin
sushilmate00722-Feb-12 19:49
membersushilmate00722-Feb-12 19:49 
GeneralRe: My vote of 4 Pin
MarkDani326-Mar-12 14:35
memberMarkDani326-Mar-12 14:35 
QuestionUsing C to Blend Mathematics and Art (When Math goes Beautiful) Pin
Sunarya31-Jan-12 16:15
memberSunarya31-Jan-12 16:15 
AnswerRe: Using C to Blend Mathematics and Art (When Math goes Beautiful) Pin
MarkDaniel31-Jan-12 17:31
memberMarkDaniel31-Jan-12 17:31 
QuestionBeautiful work! Pin
ChemDave31-Jan-12 6:57
memberChemDave31-Jan-12 6:57 
AnswerRe: Beautiful work! Pin
MarkDaniel31-Jan-12 11:24
memberMarkDaniel31-Jan-12 11:24 
GeneralMy vote of 5 Pin
steferson31-Jan-12 2:31
membersteferson31-Jan-12 2:31 
GeneralRe: My vote of 5 Pin
MarkDaniel31-Jan-12 5:27
memberMarkDaniel31-Jan-12 5:27 
GeneralMy vote of 5 Pin
Member 428961330-Jan-12 22:54
memberMember 428961330-Jan-12 22:54 
Very nice article, thanks.
And I hope to see an updated version which conquers the stupid DWM in Windows 7 Smile | :) (Currently the demo project can hardly draw something under Windows 7 with Aero...)
GeneralRe: My vote of 5 Pin
MarkDaniel31-Jan-12 1:01
memberMarkDaniel31-Jan-12 1:01 
GeneralMy vote of 4 Pin
ma897576930-Jan-12 19:08
memberma897576930-Jan-12 19:08 
GeneralMy vote of 5 Pin
Kelvin Armstrong30-Jan-12 18:51
memberKelvin Armstrong30-Jan-12 18:51 
GeneralRe: My vote of 5 Pin
MarkDaniel30-Jan-12 19:07
memberMarkDaniel30-Jan-12 19:07 
GeneralMy vote of 5 Pin
manoj kumar choubey30-Jan-12 17:51
membermanoj kumar choubey30-Jan-12 17:51 
GeneralRe: My vote of 5 Pin
MarkDaniel30-Jan-12 18:53
memberMarkDaniel30-Jan-12 18:53 
QuestionWell done Mark Pin
mysorian30-Jan-12 15:57
membermysorian30-Jan-12 15:57 
AnswerRe: Well done Mark Pin
MarkDaniel30-Jan-12 17:06
memberMarkDaniel30-Jan-12 17:06 
QuestionMore Math-oriented code samples available using JavaScript/SVG Pin
ocampesato30-Jan-12 9:48
memberocampesato30-Jan-12 9:48 
AnswerRe: More Math-oriented code samples available using JavaScript/SVG Pin
MarkDaniel30-Jan-12 15:37
memberMarkDaniel30-Jan-12 15:37 
GeneralMy vote of 5 Pin
Lakamraju Raghuram18-Jan-12 20:58
memberLakamraju Raghuram18-Jan-12 20:58 
GeneralRe: My vote of 5 Pin
MarkDaniel19-Jan-12 2:45
memberMarkDaniel19-Jan-12 2:45 
QuestionGreat Project Pin
Dave Kerr9-Jan-12 22:50
mvpDave Kerr9-Jan-12 22:50 
AnswerRe: Great Project Pin
MarkDaniel10-Jan-12 3:10
memberMarkDaniel10-Jan-12 3:10 
GeneralMy vote of 4 Pin
ranjith m amin9-Jan-12 21:27
memberranjith m amin9-Jan-12 21:27 
GeneralMy vote of 5 Pin
Halil ibrahim Kalkan9-Jan-12 0:11
memberHalil ibrahim Kalkan9-Jan-12 0:11 
GeneralRe: My vote of 5 Pin
MarkDaniel9-Jan-12 1:40
memberMarkDaniel9-Jan-12 1:40 
GeneralMy vote of 5 Pin
thatraja8-Jan-12 22:00
mvpthatraja8-Jan-12 22:00 
GeneralRe: My vote of 5 Pin
MarkDaniel8-Jan-12 22:41
memberMarkDaniel8-Jan-12 22:41 
QuestionVery nice Pin
Olivier Giulieri21-Dec-11 16:25
memberOlivier Giulieri21-Dec-11 16:25 
AnswerRe: Very nice Pin
MarkDaniel24-Dec-11 20:40
memberMarkDaniel24-Dec-11 20:40 
Questionmy 5 Pin
Shahriar Iqbal Chowdhury21-Dec-11 9:52
memberShahriar Iqbal Chowdhury21-Dec-11 9:52 
AnswerRe: my 5 Pin
MarkDaniel24-Dec-11 13:06
memberMarkDaniel24-Dec-11 13:06 
GeneralMy vote of 5 Pin
David MacDermot20-Dec-11 12:44
memberDavid MacDermot20-Dec-11 12:44 
GeneralRe: My vote of 5 Pin
MarkDaniel24-Dec-11 20:11
memberMarkDaniel24-Dec-11 20:11 
GeneralMy vote of 4 Pin
DavidCrow20-Dec-11 3:06
memberDavidCrow20-Dec-11 3:06 
GeneralMy vote of 5 Pin
Simon Hughes19-Dec-11 22:28
memberSimon Hughes19-Dec-11 22:28 
GeneralRe: My vote of 5 Pin
MarkDaniel24-Dec-11 13:02
memberMarkDaniel24-Dec-11 13:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Mardani Dani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid