11,639,593 members (61,539 online)

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

, 16 Dec 2011 CPOL 64.3K 4.1K 160
 Rate this:
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`:

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:

#### Other Picture Samples

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:

#### Other Picture Samples

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

## History

• 14.12.2011: Initial post

## About the Author

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

## About the Author

 Engineer 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

 First PrevNext
 none Homero Rivera27-Nov-13 9:52 Homero Rivera 27-Nov-13 9:52
 Can you send to me the resouce demo to my email(yangshenglislsl@163.com)? thank you fengzhiyou23-Nov-12 4:57 fengzhiyou 23-Nov-12 4:57
 Re: Can you send to me the resouce demo to my email(yangshenglislsl@163.com)? thank you Mardani Dani9-Jan-13 20:02 Mardani Dani 9-Jan-13 20:02
 it's great! frank_fan27-Sep-12 19:15 frank_fan 27-Sep-12 19:15
 Re: it's great! Mardani Dani10-Oct-12 15:30 Mardani Dani 10-Oct-12 15:30
 My vote of 5 suryakant4it17-Sep-12 3:30 suryakant4it 17-Sep-12 3:30
 Re: My vote of 5 Mardani Dani10-Oct-12 15:31 Mardani Dani 10-Oct-12 15:31
 Nice work Kenneth Haugland30-Jul-12 0:16 Kenneth Haugland 30-Jul-12 0:16
 Re: Nice work Mardani Dani3-Nov-12 23:31 Mardani Dani 3-Nov-12 23:31
 My vote of 5 Mohammad A Rahman25-Mar-12 17:05 Mohammad A Rahman 25-Mar-12 17:05
 Re: My vote of 5 MarkDani326-Mar-12 15:06 MarkDani3 26-Mar-12 15:06
 Nice work. kartalyildirim3-Mar-12 6:34 kartalyildirim 3-Mar-12 6:34
 Re: Nice work. MarkDani326-Mar-12 14:39 MarkDani3 26-Mar-12 14:39
 My vote of 4 sushilmate00722-Feb-12 19:49 sushilmate007 22-Feb-12 19:49
 Re: My vote of 4 MarkDani326-Mar-12 14:35 MarkDani3 26-Mar-12 14:35
 Using C to Blend Mathematics and Art (When Math goes Beautiful) Sunarya31-Jan-12 16:15 Sunarya 31-Jan-12 16:15
 Re: Using C to Blend Mathematics and Art (When Math goes Beautiful) MarkDaniel31-Jan-12 17:31 MarkDaniel 31-Jan-12 17:31
 Beautiful work! ChemDave31-Jan-12 6:57 ChemDave 31-Jan-12 6:57
 Re: Beautiful work! MarkDaniel31-Jan-12 11:24 MarkDaniel 31-Jan-12 11:24
 My vote of 5 steferson31-Jan-12 2:31 steferson 31-Jan-12 2:31
 Re: My vote of 5 MarkDaniel31-Jan-12 5:27 MarkDaniel 31-Jan-12 5:27
 Thanks.
 My vote of 5 Member 428961330-Jan-12 22:54 Member 4289613 30-Jan-12 22:54
 Re: My vote of 5 MarkDaniel31-Jan-12 1:01 MarkDaniel 31-Jan-12 1:01
 My vote of 4 ma897576930-Jan-12 19:08 ma8975769 30-Jan-12 19:08
 My vote of 5 Kelvin Armstrong30-Jan-12 18:51 Kelvin Armstrong 30-Jan-12 18:51
 Last Visit: 31-Dec-99 18:00     Last Update: 31-Jul-15 9:56 Refresh 1234 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

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

Web04 | 2.8.150731.1 | Last Updated 16 Dec 2011
Article Copyright 2011 by Mardani Dani