示例#1
0
        public Cplx Eval(Cplx x)
        {
            Cplx s = Cplx.Zero;

            for (int i = 0; i < Coe.Count; i++)
            {
                Cplx coef  = Coe[i];
                Cplx bx    = x;
                int  power = i;

                if (i > 0)
                {
                    for (int j = 0; j < power - 1; j++)
                    {
                        bx = bx.Multiply(x);
                    }

                    coef = coef.Multiply(bx);
                }

                s = s.Add(coef);
            }

            return(s);
        }
示例#2
0
        public Cplx Subtract(Cplx b)
        {
            Cplx a = this;

            return(new Cplx()
            {
                Re = a.Re - b.Re,
                Im = a.Im - b.Im
            });
        }
示例#3
0
        public Cplx Add(Cplx b)
        {
            Cplx a = this;

            return(new Cplx()
            {
                Re = a.Re + b.Re,
                Im = a.Im + b.Im
            });
        }
示例#4
0
        public Cplx Multiply(Cplx b)
        {
            Cplx a = this;

            // aRe*bRe + aRe*bIm*i + aIm*bRe*i + aIm*bIm*i*i
            return(new Cplx()
            {
                Re = a.Re * b.Re - a.Im * b.Im,
                Im = (float)(a.Re * b.Im + a.Im * b.Re)
            });
        }
示例#5
0
        internal Cplx Divide(Cplx b)
        {
            // (aRe + aIm*i) / (bRe + bIm*i)
            // ((aRe + aIm*i) * (bRe - bIm*i)) / ((bRe + bIm*i) * (bRe - bIm*i))
            //  bRe*bRe - bIm*bIm*i*i
            var tmp = this.Multiply(new Cplx()
            {
                Re = b.Re, Im = -b.Im
            });
            var tmp2 = b.Re * b.Re + b.Im * b.Im;

            return(new Cplx()
            {
                Re = tmp.Re / tmp2,
                Im = (float)(tmp.Im / tmp2)
            });
        }
示例#6
0
        static void Main(string[] args)
        {
            // TODO: add parameters from args?
            // No to fakt ne
            Bitmap bmp  = new Bitmap(300, 300);
            double xmin = -1.5;
            double xmax = 1.5;
            double ymin = -1.5;
            double ymax = 1.5;

            double xstep = (xmax - xmin) / 300;
            double ystep = (ymax - ymin) / 300;

            List <Cplx> koreny = new List <Cplx>();
            // TODO: poly should be parameterised?
            Poly p = new Poly();

            p.Coe.Add(new Cplx()
            {
                Re = 1
            });
            p.Coe.Add(Cplx.Zero);
            p.Coe.Add(Cplx.Zero);
            //p.Coe.Add(Cplx.Zero);
            p.Coe.Add(new Cplx()
            {
                Re = 1
            });
            Poly pd = p.Derive();

            Console.WriteLine(p);
            Console.WriteLine(pd);

            var clrs = new Color[]
            {
                Color.Red, Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Fuchsia, Color.Gold, Color.Cyan, Color.Magenta
            };

            var maxid = 0;

            // TODO: cleanup!!!
            // for every pixel in image...
            for (int i = 0; i < 300; i++)
            {
                for (int j = 0; j < 300; j++)
                {
                    // find "world" coordinates of pixel
                    double x = xmin + j * xstep;
                    double y = ymin + i * ystep;

                    Cplx ox = new Cplx()
                    {
                        Re = x,
                        Im = (float)(y)
                    };

                    if (ox.Re == 0)
                    {
                        ox.Re = 0.0001;
                    }
                    if (ox.Im == 0)
                    {
                        ox.Im = 0.0001f;
                    }

                    //Console.WriteLine(ox);

                    // find solution of equation using newton's iteration
                    float it = 0;
                    for (int q = 0; q < 30; q++)
                    {
                        var diff = p.Eval(ox).Divide(pd.Eval(ox));
                        ox = ox.Subtract(diff);

                        //Console.WriteLine($"{q} {ox} -({diff})");
                        if (Math.Pow(diff.Re, 2) + Math.Pow(diff.Im, 2) >= 0.5)
                        {
                            q--;
                        }
                        it++;
                    }

                    //Console.ReadKey();

                    // find solution root number
                    var known = false;
                    var id    = 0;
                    for (int w = 0; w < koreny.Count; w++)
                    {
                        if (Math.Pow(ox.Re - koreny[w].Re, 2) + Math.Pow(ox.Im - koreny[w].Im, 2) <= 0.01)
                        {
                            known = true;
                            id    = w;
                        }
                    }
                    if (!known)
                    {
                        koreny.Add(ox);
                        id    = koreny.Count;
                        maxid = id + 1;
                    }

                    // colorize pixel according to root number
                    //int vv = id;
                    //int vv = id * 50 + (int)it*5;
                    var vv = clrs[id % clrs.Length];
                    vv = Color.FromArgb(vv.R, vv.G, vv.B);
                    vv = Color.FromArgb(Math.Min(Math.Max(0, vv.R - (int)it * 2), 255), Math.Min(Math.Max(0, vv.G - (int)it * 2), 255), Math.Min(Math.Max(0, vv.B - (int)it * 2), 255));
                    //vv = Math.Min(Math.Max(0, vv), 255);
                    bmp.SetPixel(j, i, vv);
                    //bmp.SetPixel(j, i, Color.FromArgb(vv, vv, vv));
                }
            }

            // TODO: delete I suppose...
            //for (int i = 0; i < 300; i++)
            //{
            //    for (int j = 0; j < 300; j++)
            //    {
            //        Color c = bmp.GetPixel(j, i);
            //        int nv = (int)Math.Floor(c.R * (255.0 / maxid));
            //        bmp.SetPixel(j, i, Color.FromArgb(nv, nv, nv));
            //    }
            //}

            bmp.Save("../../../out.png");
            //Console.ReadKey();
        }