File talk:Color complex plot2.jpg
Jump to navigation
Jump to search
compiling errors[edit]
Dev-C++[edit]
Hi. Thx for nice image and src code. I'm trying to compile it under Dev-c++. Here is my compile log:
Compiler: Default compiler Building Makefile: "D:\doc\programming\cpp\prog\complex\cp2\Makefile.win" Executing make clean rm -f main.o Project1.exe g++.exe -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include" -I"include" main.cpp: In function `void SetHSV(double, double, double, unsigned char*)': main.cpp:12: warning: converting to `int' from `double' main.cpp: In function `int main()': main.cpp:77: error: invalid conversion from `unsigned char*' to `const char*' main.cpp:77: error: initializing argument 1 of `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'
What can I change to compile it ?
Regards
--Adam majewski (talk) 16:24, 18 May 2010 (UTC)
g++ & Linux[edit]
g++ c.cpp c.cpp: In function ‘int main()’: c.cpp:77: error: invalid conversion from ‘unsigned char*’ to ‘const char*’ c.cpp:77: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]’
--Adam majewski (talk) 08:09, 7 May 2011 (UTC)
Src[edit]
This code works for me ( Dev-C++ ). Changes are marked by comment signs.
#include <complex>
#include <fstream>
using namespace std;
void SetHSV(double h, double s, double v, char _color[3]) //
{
double r=0, g=0, b=0;
if(s==0)
r = g = b = v;
else {
h /= 60;
int i = (int) floor(h); ///
double f = h - i;
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; //
}
complex<double> fun(complex<double>& c ){
const complex<double> i(0., 1.);
return (pow(c,2) - 1.) * pow(c -2. -i, 2) / (pow(c, 2) + 2. + 2. * i);
}
int main(){
const int dimx = 800; const int dimy = 800;
const double rmi = -3; const double rma = 3;
const double imi = -3; const double ima = 3;
ofstream f("complex.ppm", ios::binary);
f << "P6" << endl
<< dimx << " " << dimy << endl
<< "255" << endl;
for(int j=0; j < dimy; ++j){
double im = ima - (ima -imi) *j /(dimy -1);
for(int i=0; i < dimx; ++i){
double re = rmi +(rma -rmi) *i /(dimx -1);
complex<double> c(re, im);
complex<double> v = fun(c);
double a = arg(v)*180/M_PI;
if(a < 0) a += 360;
double m = abs(v);
double ranges;
double rangee = 1;
while(m>rangee){
ranges = rangee;
rangee *= M_E;
}
double k = (m-ranges)/(rangee-ranges);
double sat = k < 0.5 ? 1-2.8*k : 2.8*k-1.1;
if(sat < 0.3) sat = 0.3;
else if(sat > 1) sat = 1;
double val = k < 0.5 ? 1.4-1.6*k : 1.6*k-0.6;
if(val < 0.6) val = 0.6;
else if(val > 1) val = 1;
char color[3]; //
SetHSV(a,sat,val,color);
f.write(color,3);
}
}
return 0;
}