circ


SUBMITTED BY: Guest

DATE: Sept. 20, 2017, 7:39 p.m.

FORMAT: C++

SIZE: 2.8 kB

HITS: 364

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. const int height=400;
  6. const int width=400;//resolution
  7. const int para_n[]={0,0,19,11,9,8,7,6};
  8. const int para_s[]={1,2,4,8,16,32,64,128};
  9. char* tested;
  10. char signs[8];
  11. double R;
  12. int N;
  13. typedef struct complex_num{double r;double i;}cpln;
  14. cpln z;
  15. const cpln one={1,0};
  16. const cpln zero={0,0};
  17. inline double ab(cpln c){return c.r*c.r+c.i*c.i;}//absolute value
  18. inline cpln ad(cpln c,double x){c.r+=x;return c;}//adding real number
  19. inline cpln adc(cpln c,cpln d){c.r+=d.r;c.i+=d.i;return c;}//cplx addition
  20. inline cpln subc(cpln c,cpln d){c.r-=d.r;c.i-=d.i;return c;}
  21. inline cpln mt(cpln c,cpln d){cpln r={c.r*d.r-c.i*d.i,c.r*d.i+c.i*d.r};return r;}//multiplication
  22. inline cpln inv(cpln c){double u=c.r*c.r+c.i*c.i;cpln r={c.r/u,-c.i/u}; return r;}//1/z
  23. inline cpln neg(cpln c){cpln r={-c.r,-c.i}; return r;}
  24. inline cpln i2c(int i){cpln r={(double)i,0}; return r;}
  25. int f(cpln c,cpln d,int i,long sym,int m){
  26. if(i<N && ab(ad(c,-1))*height*width<ab(d))
  27. return 1;
  28. else if(i==0)
  29. return 0;
  30. cpln nc=mt(z,c);
  31. cpln nd=adc(c,mt(z,d));
  32. int t=sym%m;
  33. if(signs[t]){nc=subc(i2c(t+1),nc);nd=neg(nd);}
  34. else nc=subc(nc, i2c(t));
  35. if(c.r>m/2)
  36. {
  37. for(int j=m-1;j>-1;j--)
  38. if(f(nc,nd,i-1,sym*m+j,m))return 1;
  39. }
  40. else{
  41. for(int j=0;j<m;j++)
  42. if(f(nc,nd,i-1,sym*m+j,m))return 1;
  43. }
  44. return 0;
  45. }
  46. int chk(double x,double y, int m){
  47. z.r=x;
  48. z.i=y;
  49. if(x*x+y*y>1||(m+1)*(m+1)*(x*x+y*y)<1)return 0;
  50. if(f(one,zero,N,m-1,m))
  51. return 1;
  52. return 0;
  53. }
  54. int main()
  55. { long time=clock();
  56. char* data=(char*)malloc(width*height);//pixels of the graph we are drawing
  57. for(int i=0;i<width*height;i++)data[i]=0;
  58. //check Gora's condition
  59. for(int m=2;m<4;m++){
  60. for(int s=0;s<para_s[m];s++){
  61. printf("m=%d, s=%d\n",m,s);
  62. long time2=clock();
  63. N=para_n[m];
  64. int s1=s;
  65. for(int i=0;i<m;i++){signs[i]=s1%2;s1/=2;}
  66. //find roots
  67. for(int i=0;i<width;i++){
  68. if(i%20==0)printf("%d\n",i);
  69. for(int j=0;j<height;j++)
  70. if(data[i+width*j]);
  71. else if(j>height-j)data[i+width*j]=data[i+width*(height-j)];
  72. else data[i+width*j]=m*chk(((double)i)*2/width-1,((double)j)*2/height-1, m);
  73. }
  74. free(tested);
  75. printf("%ld\n",clock()-time2);
  76. }
  77. }
  78. //output
  79. int datas[]={0,255,210,170,130,90,50};
  80. FILE* out=fopen("out.pgm","wb");
  81. fprintf(out, "P2\n%d %d\n255\n",width, height);
  82. for(int i=0;i<width*height;i++){
  83. fprintf(out,"%d",datas[data[i]]);
  84. if(i==width*height-1); else if(i%64==63)fputc('\n',out);else fputc(' ',out);}
  85. fputc('\n',out);
  86. fclose(out);
  87. printf("%ld\n",clock()-time);
  88. }

comments powered by Disqus