fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<string>
  4. #include<cstdio>
  5. #include<cstdlib>
  6. #include<cmath>
  7. #include<cstring>
  8. //#include"Point.h"
  9.  
  10. using namespace std;
  11.  
  12. struct Point{
  13. char label;//for gnuplot.
  14. int index;//rank. sort by heading.
  15. double x, y;//coordinate.
  16.  
  17. double heading(Point& other){
  18. return atan2(y - this->y, x - this->x);
  19. }
  20. //friend ostream& operator<<(const Point& self, const ostream& os);
  21. friend ostream& operator<<(ostream& os, const Point& self);
  22. };
  23. //ostream& operator<<(const ostream& os, const Point& self)
  24. //ostream& operator<<(const Point& self, const ostream& os)
  25. ostream& operator<<(ostream& os, const Point& self)
  26. {
  27. os << self.label << ":" << "(" << self.x << ", " << self.y << ")";
  28. return os;
  29. }
  30.  
  31. double makerand(){
  32. return rand() / (double)RAND_MAX;
  33. }
  34. void makedata(vector<Point>& ps, int size){
  35. char ch = 'A';
  36. for(int i=0; i<size; i++){
  37. Point temp = {ch, -1, makerand(), makerand()};
  38. ps.push_back(temp);
  39. ch++;
  40. }
  41. }
  42. void output_gnuplot(vector<Point>& ps, char base[]){
  43. char fn[256];
  44. sprintf(fn, "%s_gnuplot.dat", base);
  45. FILE* fp = fopen(fn, "w");
  46.  
  47. fprintf(fp, "# %d\n", ps.size());
  48.  
  49. for(int i=0; i<ps.size(); i++){
  50. fprintf(fp, "%lf %lf\n", ps[i].x, ps[i].y);
  51. }
  52.  
  53. fclose(fp);
  54. }
  55. void output_exe(vector<Point>& ps, char base[]){
  56. char fn[256];
  57. sprintf(fn, "%s_exe.dat", base);
  58. FILE* fp = fopen(fn, "w");
  59.  
  60. fprintf(fp, "%d\n", ps.size());
  61.  
  62. for(int i=0; i<ps.size(); i++){
  63. fprintf(fp, "%c %lf %lf\n", ps[i].label, ps[i].x, ps[i].y);
  64. }
  65.  
  66. fclose(fp);
  67. }
  68. void output_gp_script(vector<Point>& ps, char base[], bool is_png){
  69. char fn[256];
  70. sprintf(fn, "%s.gp", base);
  71. FILE* fp = fopen(fn, "w");
  72.  
  73. if(is_png){
  74. fprintf(fp, "set terminal png\n");
  75. fprintf(fp, "set output \"%s.png\"\n", base);
  76. }
  77.  
  78. fprintf(fp, "set size square\n");
  79. fprintf(fp, "set nokey\n");//凡例を消す
  80.  
  81. for(int i=0; i<ps.size(); i++){
  82. fprintf(fp, "set label %d at %lf,%lf \"%c\"\n", i+1, ps[i].x, ps[i].y, ps[i].label);
  83. }
  84.  
  85. char fn_dat[256];
  86. sprintf(fn_dat, "%s_gnuplot.dat", base);
  87. fprintf(fp, "plot \"%s\"\n", fn_dat);
  88. fclose(fp);
  89. }
  90. int main(int argc, char* args[]){
  91. unsigned int seed = 7729;
  92. srand(seed);
  93.  
  94. if(argc != 3){
  95. fprintf(stderr, "arg error, argc=%d\n", argc);
  96. exit(1);
  97. }
  98. //makedata [N] [basename]
  99.  
  100. int N;//size.
  101. sscanf(args[1], "%d", &N);
  102. //printf("N=%d\n", N);//check
  103. if(N > 26){
  104. N = 26;
  105. }
  106.  
  107. char base[256];//base name.
  108. strcpy(base, args[2]);
  109.  
  110. vector<Point> ps;
  111.  
  112. makedata(ps, N);
  113. output_gnuplot(ps, base);
  114. output_gp_script(ps, base, false);
  115. output_exe(ps, base);
  116.  
  117.  
  118.  
  119.  
  120. //convex hull//check
  121. /*
  122. char fn_txt[256];
  123. sprintf(fn_txt, "%s.txt", args[2]);
  124. FILE* txt = fopen(fn_txt, "w");
  125. //gnuplotで線を引くチェックのため、前半の半分だけを選んで別ファイルにする。
  126. for(int i=0; i<ps.size()/2; i++){//check
  127. fprintf(txt, "%lf %lf\n", ps[i].x, ps[i].y);
  128. }
  129. fclose(txt);
  130.  
  131. fprintf(gp, "plot \"%s\" with lines\n", fn_txt);
  132. */
  133.  
  134.  
  135. //fclose(gp);
  136.  
  137. return 0;
  138. }
  139.  
Runtime error #stdin #stdout #stderr 0.01s 5520KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
arg error, argc=1