fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. // This is a simplified text-based representation. Graphical libraries
  5. // like OpenGL, SDL, or plotting libraries like matplotlib (via a C++ wrapper)
  6. // would be required for a true graphical output.
  7.  
  8. int main() {
  9. // Define the bounds of the imaginary axis to display.
  10. double minY = -5.0;
  11. double maxY = 5.0;
  12.  
  13. // Define the resolution (number of lines to print).
  14. int resolution = 21; // Higher number for finer detail
  15.  
  16. // Calculate the step size between each line.
  17. double stepSize = (maxY - minY) / (resolution - 1);
  18.  
  19. // Calculate the values of the roots
  20. double z1_imag = sqrt(3) + sqrt(5);
  21. double z2_imag = sqrt(3) - sqrt(5);
  22.  
  23.  
  24. std::cout << "Text-based Complex Plane (Imaginary Axis)" << std::endl;
  25. std::cout << "-------------------------------------------" << std::endl;
  26.  
  27. for (int i = 0; i < resolution; ++i) {
  28. double y = minY + i * stepSize;
  29.  
  30. // Print the imaginary axis
  31. std::cout << "|";
  32.  
  33. //Mark the location of root 1
  34. if (std::abs(y - z1_imag) < stepSize / 2) {
  35. std::cout << " z1 ";
  36. } else if (std::abs(y - z2_imag) < stepSize / 2) {
  37. std::cout << " z2 ";
  38. } else {
  39. std::cout << " "; // 4 spaces to align with z1 and z2
  40. }
  41.  
  42.  
  43.  
  44. std::cout << " " << y << "i" << std::endl;
  45. }
  46.  
  47. std::cout << "-------------------------------------------" << std::endl;
  48. std::cout << "z1 = " << z1_imag << "i" << std::endl;
  49. std::cout << "z2 = " << z2_imag << "i" << std::endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Text-based Complex Plane (Imaginary Axis)
-------------------------------------------
|     -5i
|     -4.5i
|     -4i
|     -3.5i
|     -3i
|     -2.5i
|     -2i
|     -1.5i
|     -1i
| z2  -0.5i
|     0i
|     0.5i
|     1i
|     1.5i
|     2i
|     2.5i
|     3i
|     3.5i
| z1  4i
|     4.5i
|     5i
-------------------------------------------
z1 = 3.96812i
z2 = -0.504017i