1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include <Magick++.h> // Magick::InitializeMagick(), Magick::Image, Magick::Geometry
#include <cstdlib> // atof()
#include <cmath> // sin(), cos()
#inlcude <iostream> // std::cout, std::endl
const double pi = 3.14159265359;
const int X = 640;
const int Y = 480;
int main(int argc,char *argv[])
{
if ((argc < 2) || (argc%2==0)) // If too few arguments, or an even number of arguments, quit
{
std::cout << "Usage: wave-type phase [wave-type][phase] ..." << std::endl
<< " Valid types are sin or cos " << std::endl;
return -1;
}
double output[X] = {0}; // Contains an Y value for each pixel in the X axis;
int nb_waves = (argc-1)/2; // number of waves
// let's calculate the Y values for X=[0,4pi].
for (int i = 0; i < nb_waves; ++i)
{
double scalar = 4. * pi / (double)X; // This will scale the X value from [0,X] to [0,4pi], each X-pixel is 0.01963 radians
double phase = atof(argv[2*i+2]); // This is the phase offset in radians as defined by the command line
if (argv[2*i+1] == "cos")
{
for (j = 0; j < X; ++j)
{
output[j] += cos(j * scalar + phase); // cos(x + phase) from 0 to 4pi
}
}
else // argv[2i+1] == "sin", we'll assume
{
for (j = 0; j < X; ++j)
{
output[j] += sin(j * scalar + phase); // sin(x + phase) from 0 to 4pi
}
}
}
// We have our waves added, now lets normalize them to the Y axis:
// We now have waves between +- (argc-1) / 2 (if there were 3 waves, between +- 3.0).
for (int i = 0; i < X; ++i)
{
output[i] *= (double)Y / ((double)nb_waves*2.); // now it's [-240,+240] (we can convert it to pixels easily now
output[i] += X/2; // now it's [0,480]
}
// Draw your graph
Magick::InitializeMagick(*argv);
Magick::Image image( Magick::Geometry(X,Y, 0, 0), "white" );
for (int i = 0; i < X; i++)
{
image.pixelColor( i, (int)output[i], "red");
}
image.write( "red_pixel.jpg" );
return 0;
}
|