Click here to Skip to main content
Click here to Skip to main content

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

, 16 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Using C to blend Mathematics and Art that will produce a carving
Prize winner in Competition "Best overall article of December 2011"
Prize winner in Competition "Best C++ article of December 2011"

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.

Comments and Discussions

 
GeneralNice work PinmemberKenneth Haugland30-Jul-12 0:16 
GeneralRe: Nice work PinmemberMardani Dani3-Nov-12 23:31 

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 | Mobile
Web03 | 2.8.140814.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Mardani Dani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid