fork(4) download
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6.  
  7. class Gradient
  8. {
  9. int height;
  10. int width;
  11. float x_center;
  12. float y_center;
  13. string chars;
  14. float radius;
  15. int ratio;
  16. public:
  17. void Init(int height, int width, float x_center, float y_center, string chars);
  18. Gradient(int height, int width, float x_center, float y_center, string chars);
  19. Gradient(int height, int width, float x_center, float y_center, string chars, float radius);
  20. void Print();
  21. };
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25. int height=0,width=0;
  26. float x_center=0,y_center=0,radius=0;
  27. string chars;
  28. cout<<"Enter height:"<<endl;
  29. while (height<=0) cin>>height;
  30. cout<<"Enter width:"<<endl;
  31. while (width<=0) cin>>width;
  32. cout<<"Enter x_center"<<endl;
  33. cin>>x_center;
  34. cout<<"Enter y_center"<<endl;
  35. cin>>y_center;
  36. cout<<"Enter radius(optional, 0 to ommit)"<<endl;
  37. cin>>radius;
  38. cout<<"Enter char string"<<endl;
  39. cin>>chars;
  40. Gradient *grad;
  41. if (radius)
  42. grad=new Gradient(height, width, x_center, y_center, chars, radius);
  43. else
  44. grad=new Gradient(height, width, x_center, y_center, chars);
  45. grad->Print();
  46. return EXIT_SUCCESS;
  47. }
  48. void Gradient::Print()
  49. {
  50. float distForEach=(radius)/chars.size();
  51. for (int i=0;i<height;i++)
  52. {
  53. for (int j=0;j<width;j++)
  54. {
  55. char c;
  56. float dist=sqrt((j-x_center)*(j-x_center)+(i*ratio-y_center*ratio)*(i*ratio-y_center*ratio));
  57. dist=abs(dist);
  58. int charN=dist/distForEach;
  59. charN;
  60. if (chars.size()>charN)
  61. c=chars[charN];
  62. else
  63. c=' ';
  64. cout<<c;
  65. }
  66. cout<<endl;
  67. }
  68. }
  69. Gradient::Gradient(int height, int width, float x_center, float y_center, string chars)
  70. {
  71. Init(height,width,x_center,y_center,chars);
  72. float point_x=x_center<=width/2?(width-x_center):(0+x_center);//Calculate distance from center to farthest point in x
  73. float point_y=y_center<=height/2?(height-y_center):(0+y_center);//Calculate distance from center to farthest point in y
  74. point_y*=ratio;//Convert to LU (length units), multiplies by ratio, that indicates how many LUs in height is each character
  75. cout<<"Point x:"<<point_x<<" Point y:"<<point_y<<endl;
  76. this->radius=sqrt(point_x*point_x+point_y*point_y);//Calculate radius
  77. }
  78.  
  79. Gradient::Gradient(int height, int width, float x_center, float y_center, string chars, float radius)
  80. {
  81. Init(height,width,x_center,y_center,chars);
  82. this->radius=radius;
  83. }
  84. void Gradient::Init(int height, int width, float x_center, float y_center, string chars)
  85. {
  86. this->height=height;
  87. this->width=width;
  88. this->x_center=x_center;
  89. this->y_center=y_center;
  90. this->chars=chars;
  91. this->ratio=2;
  92. }
Success #stdin #stdout 0s 3036KB
stdin
35
75
0
0
0
.,-
stdout
Enter height:
Enter width:
Enter x_center
Enter y_center
Enter radius(optional, 0 to ommit)
Enter char string
Point x:75 Point y:70
...................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------
...................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------
..................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------
..................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------
..................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------
.................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------
.................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------
................................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------
...............................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------
..............................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------
............................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------
...........................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------
.........................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------
.......................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-----------
....................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------
.................,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------
.............,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------
....,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,---------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,-----------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,--------------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,------------------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,----------------------------------------------
,,,,,,,,,,,,,,,,,,,,,,,,,--------------------------------------------------
,,,,,,,,,,,,,,,,,,---------------------------------------------------------
,,,,,,,,-------------------------------------------------------------------