File talk:Color complex plot.jpg
Licensing
[edit]How is the program licensed? --Thinboy00 22:10, 24 November 2007 (UTC)
Help?
[edit]Has anyone had any success compiling this code using only standard C++ libraries? 65.6.213.12 01:44, 21 May 2008 (UTC)
I had the same problem with gcc 4.1, thanks for your workaround. --Arichelo (talk) 10:42, 29 July 2009 (UTC)
Using Dev-C++ under WinXp no. I have made small changes ( complex number and operations on it) and it goes :
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
//#include <cmath> // floor
const double PI = 3.1415926535897932384626433832795;
const double E = 2.7182818284590452353602874713527;
#include <complex> // ISO C++ 14882: 26.2 Complex Numbers
using namespace std; //
void SetHSV( double h, double s, double v, unsigned char color[3] ) {
double r,g,b;
if(s==0)
r = g = b = v;
else {
if(h==1) h = 0;
double z = floor(h*6); int i = int(z);
double f = double(h*6 - z);
double p = v*(1-s);
double q = v*(1-s*f);
double t = v*(1-s*(1-f));
switch(i){
case 0: r=v; g=t; b=p; break;
case 1: r=q; g=v; b=p; break;
case 2: r=p; g=v; b=t; break;
case 3: r=p; g=q; b=v; break;
case 4: r=t; g=p; b=v; break;
case 5: r=v; g=p; b=q; break;
}
}
int c;
c = int(256*r); if(c>255) c = 255; color[0] = c;
c = int(256*g); if(c>255) c = 255; color[1] = c;
c = int(256*b); if(c>255) c = 255; color[2] = c;
}
std::complex<double> fun(std::complex<double> & c ){return sqrt(c)*sqrt(c);} //
int main(){
const int dimx = 800; const int dimy = 800;
const double rmi = -2; const double rma = 2;
const double imi = -2; const double ima = 2;
FILE * fp = fopen("complex.ppm","wb");
fprintf(fp,"P6\n%d %d\n255\n",dimx,dimy);
int i,j;
for(j=0;j<dimy;++j){
double im = ima - (ima-imi)*j/(dimy-1);
for(i=0;i<dimx;++i){
double re = rma - (rma-rmi)*i/(dimx-1);
std::complex<double> c(re,im); //
std::complex<double> v = fun(c); //
double a = arg(v); //
while(a<0) a += 2*PI; a /= 2*PI;
double m = abs(v); //
double ranges = 0;
double rangee = 1;
while(m>rangee){
ranges = rangee;
rangee *= E;
}
double k = (m-ranges)/(rangee-ranges);
double sat = k<0.5 ? k*2: 1 - (k-0.5)*2;
sat = 1 - pow( (1-sat), 3); sat = 0.4 + sat*0.6;
double val = k<0.5 ? k*2: 1 - (k-0.5)*2; val = 1 - val;
val = 1 - pow( (1-val), 3); val = 0.6 + val*0.4;
static unsigned char color[3];
SetHSV(a,sat,val,color);
fwrite(color,1,3,fp);
}
}
fclose(fp);
return 0;
}
Changes are marked by // at the end of the line
It is different plot. I try to make it the same.
--Adam majewski (talk) 09:49, 17 July 2009 (UTC)
C code
[edit]Hi. See also this C code. and python code --Adam majewski (talk) 15:11, 11 March 2011 (UTC) I have translated cpp into c. It works. See file page. --Adam majewski (talk) 19:53, 7 November 2012 (UTC)