Advertisement
Guest User

GIK Complex

a guest
Jan 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Fractals
  8. {
  9. public struct Complex
  10. {
  11. public double Real;
  12. public double Imag;
  13. public double Magnitude
  14. {
  15. get { return Math.Sqrt(Real * Real + Imag * Imag); }
  16. set
  17. {
  18. var phase = Phase;
  19. Real = value * Math.Cos(phase);
  20. Imag = value * Math.Sin(phase);
  21. }
  22. }
  23. public double Phase
  24. {
  25. get { return Math.Atan2(Imag, Real); }
  26. set
  27. {
  28. var mag = Magnitude;
  29. Real = mag * Math.Cos(value);
  30. Imag = mag * Math.Sin(value);
  31. }
  32. }
  33.  
  34. public Complex(double real, double imag)
  35. {
  36. Real = real;
  37. Imag = imag;
  38. }
  39.  
  40. public static Complex operator +(Complex left, Complex right)
  41. {
  42. var result = new Complex(left.Real, left.Imag);
  43. result.Real += right.Real;
  44. result.Imag += right.Imag;
  45. return result;
  46. }
  47.  
  48. public static Complex operator -(Complex left, Complex right)
  49. {
  50. var result = new Complex(left.Real, left.Imag);
  51. result.Real -= right.Real;
  52. result.Imag -= right.Imag;
  53. return result;
  54. }
  55.  
  56. public static Complex operator *(Complex left, Complex right)
  57. {
  58. var result = new Complex(left.Real, left.Imag);
  59. result.Magnitude *= right.Magnitude;
  60. result.Phase += right.Phase;
  61. return result;
  62. }
  63.  
  64. public static Complex operator /(Complex left, Complex right)
  65. {
  66. var result = new Complex(left.Real, left.Imag);
  67. result.Magnitude /= right.Magnitude;
  68. result.Phase -= right.Phase;
  69. return result;
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement