Example #1
0
File: Form1.cs Project: j4ra/grcis
        private void numColors_ValueChanged(object sender, EventArgs e)
        {
            numCol = Arith.Clamp((int)numColors.Value, 3, 10);

            if (inputImage != null)
            {
                Colormap.Generate(inputImage, numCol, out colors);
                pictureBox1.Invalidate();
            }
        }
Example #2
0
        public void DoNonBoolCtorTest()
        {
            var variable  = new Id(new Word("x", Tag.ID), VarType.INT, 0);
            var constant  = new Constant(new Num(12), VarType.INT);
            var expresion = new Arith(Word.PLUS, variable, constant);

            var donode = new Do();

            donode.Init(expresion, new Stmt());
        }
Example #3
0
 /// <summary>
 /// Mouse wheel event.
 /// </summary>
 private void glControl1_MouseWheel(object sender, MouseEventArgs e)
 {
     if (e.Delta != 0)
     {
         double notches = e.Delta / 120.0f;
         fov          *= (float)Math.Pow(fovInc, notches);
         fov           = Arith.Clamp(fov, 0.05f, 1.6f);
         setupViewport = true;
     }
 }
Example #4
0
        private Expr Expr()
        {
            Expr  x   = Term();
            Token cur = look;

            while (Except('+') || Except('-'))
            {
                x = new Arith(cur, x, Expr());
            }
            return(x);
        }
Example #5
0
        private Expr Term()
        {
            Expr  x   = Unary();
            Token cur = look;

            while (Except('*') || Except('/') || Except('%'))
            {
                x = new Arith(cur, x, Term());
            }
            return(x);
        }
Example #6
0
        private Expr AddExpr()
        {
            var x = MultExpr();

            while (_look.Tag == '+' || _look.Tag == '-')
            {
                var tok = _look;
                Move();
                x = new Arith(tok, x, MultExpr());
            }
            return(x);
        }
Example #7
0
        private Expr MultExpr()
        {
            var x = ExponentExpr();

            while (_look.Tag == '*' || _look.Tag == '/')
            {
                var tok = _look;
                Move();
                x = new Arith(tok, x, UnaryExpr());
            }
            return(x);
        }
Example #8
0
        internal virtual Expr term()
        {
            Expr x = unary();

            while (look.tag == '*' || look.tag == '/')
            {
                Token tok = look;
                move();
                x = new Arith(tok, x, unary());
            }
            return(x);
        }
Example #9
0
        internal virtual Expr expr()
        {
            Expr x = term();

            while (look.tag == '+' || look.tag == '-')
            {
                Token tok = look;
                move();
                x = new Arith(tok, x, term());
            }
            return(x);
        }
Example #10
0
        Expr expr()
        {
            Expr x = term();

            while (look.tag == '+' || look.tag == '-')
            {
                Token tok = look;
                move();
                x = new Arith(tok, x, term(), compileRows);
            }
            return(x);
        }
Example #11
0
        Expr term()
        {
            Expr x = unary();

            while (look.tag == '*' || look.tag == '/')
            {
                Token tok = look;
                move();
                x = new Arith(tok, x, unary(), compileRows);
            }
            return(x);
        }
Example #12
0
        /// <summary>
        /// Renders the single pixel of an image (using required super-sampling).
        /// </summary>
        /// <param name="x">Horizontal coordinate.</param>
        /// <param name="y">Vertical coordinate.</param>
        /// <param name="color">Computed pixel color.</param>
        public override void RenderPixel(int x, int y, double[] color)
        {
            Debug.Assert(color != null);
            Debug.Assert(MT.rnd != null);

            // !!!{{ TODO: this is exactly the code inherited from static sampling - make it adaptive!

            int bands = color.Length;
            int b;

            Array.Clear(color, 0, bands);
            double[] tmp = new double[bands];

            int    i, j;
            double step = 1.0 / superXY;
            double amplitude = Jittering * step;
            double origin = 0.5 * (step - amplitude);
            double x0, y0;

            MT.StartPixel(x, y, Supersampling);

            for (j = 0, y0 = y + origin; j++ < superXY; y0 += step)
            {
                for (i = 0, x0 = x + origin; i++ < superXY; x0 += step)
                {
                    MT.imageFunction.GetSample(x0 + amplitude * MT.rnd.UniformNumber(),
                                               y0 + amplitude * MT.rnd.UniformNumber(),
                                               tmp);
                    MT.NextSample();
                    Util.ColorAdd(tmp, color);
                }
            }

            double mul = step / superXY;

            // Gamma-encoding?
            if (Gamma > 0.001)
            {
                // Gamma-encoding and clamping.
                double g = 1.0 / Gamma;
                for (b = 0; b < bands; b++)
                {
                    color[b] = Arith.Clamp(Math.Pow(color[b] * mul, g), 0.0, 1.0);
                }
            }
            else
            {
                // No gamma, no clamping (for HDRI).
                Util.ColorMul(mul, color);
            }

            // !!!}}
        }
Example #13
0
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show(@"Do you want to return to the Main Menu?", "Close Game", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                myMain = new MainWindow();
                myMain.Show();

                Arith.Hide();
            }
        }
Example #14
0
        public void TestArith()
        {
            var add = new Arith(new Token('+'), new Constant(42), new Constant(99));

            Assert.AreEqual("42 + 99", add.ToString());
            Assert.IsTrue(add.Reduce() is Sara.Temp);

            var mult = new Arith(new Token('*'), new Constant(142), new Constant(0));
            var poly = new Arith(new Token('-'), add, mult);

            Assert.AreEqual("42 + 99 - 142 * 0", poly.ToString());
        }
Example #15
0
        /// <summary>
        /// Draw one trace point (bullet-shaped).
        /// </summary>
        /// <param name="interior">True for object interior, false for exterior.</param>
        public void DrawTrace(int x, int y, bool interior)
        {
            Debug.Assert(mask != null);

            int xmin = Arith.Clamp(x - MAX_RADIUS, 0, wid - 1);
            int xmax = Arith.Clamp(x + MAX_RADIUS + 1, 0, wid);
            int ymin = Arith.Clamp(y - MAX_RADIUS, 0, hei - 1);
            int ymax = Arith.Clamp(y + MAX_RADIUS + 1, 0, hei);

            if (xmin >= xmax ||
                ymin >= ymax)
            {
                return;
            }

            byte col   = (byte)(interior ? COL_INTERIOR : COL_EXTERIOR);
            int  wWid  = xmax - xmin;
            int  wHei  = ymax - ymin;
            int  x0    = x - xmin;
            int  y0    = y - ymin;
            int  depth = Image.GetPixelFormatSize(PixelFormat.Format8bppIndexed) / 8;

            BitmapData dataOut = mask.LockBits(new Rectangle(xmin, ymin, wWid, wHei), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            unsafe
            {
                // SetPixel( x0, y0, col ):
                if (x0 >= 0 && x0 < wWid &&
                    y0 >= 0 && y0 < wHei)
                {
                    *((byte *)dataOut.Scan0 + y0 * dataOut.Stride + x0 * depth) = col;
                }

                if (traceSize > 1.0)
                {
                    for (int pi = 1; pi < Draw.squares.Length && Draw.squares[pi] <= traceSize; pi++)
                    {
                        int pix = x0 + Draw.penPixels[pi].Item1;
                        int piy = y0 + Draw.penPixels[pi].Item2;
                        if (pix < 0 || pix >= wWid ||
                            piy < 0 || piy >= wHei)
                        {
                            continue;
                        }

                        // SetPixel( pix, piy, col ):
                        *((byte *)dataOut.Scan0 + piy * dataOut.Stride + pix * depth) = col;
                    }
                }
            }
            mask.UnlockBits(dataOut);
        }
Example #16
0
        public void ArithGenTest()
        {
            var token    = new Token('+');
            var variable = new Id(new Word("x", Tag.ID), VarType.INT, 0);
            var constant = new Constant(new Num(12), VarType.INT);
            var arith    = new Arith(token, variable, constant);

            var expresion = arith.Gen();

            Assert.AreEqual(VarType.INT, expresion.Type);
            Assert.AreEqual(token, expresion.Op);
            Assert.AreEqual("x + 12", expresion.ToString());
        }
Example #17
0
        public void ArithCtorTest()
        {
            var token    = new Token('+');
            var variable = new Id(new Word("x", Tag.ID), VarType.INT, 0);
            var constant = new Constant(new Num(12), VarType.INT);
            var arith    = new Arith(token, variable, constant);

            Assert.AreEqual(variable, arith.Expr1);
            Assert.AreEqual(constant, arith.Expr2);
            Assert.AreEqual(token, arith.Op);
            Assert.AreEqual(VarType.INT, arith.Type);
            Assert.AreEqual("x + 12", arith.ToString());
        }
Example #18
0
        static void Main(string[] args)
        {
            Console.Write("첫 번째 숫자 입력 : ");
            double num0 = Double.Parse(Console.ReadLine());

            Console.Write("두 번째 숫자 입력 : ");
            double num1 = Double.Parse(Console.ReadLine());

            Console.WriteLine("+ 결과 " + Arith.add(num0, num1));
            Console.WriteLine("- 결과 " + Arith.sub(num0, num1));
            Console.WriteLine("* 결과 " + Arith.mul(num0, num1));
            Console.WriteLine("/ 결과 " + Arith.div(num0, num1));
        }
Example #19
0
        /// <summary>
        /// Returns color based on range
        /// Returned color is either dark blue (close to minValue) or red (close to maxValue)
        /// Between that color is lineary transited, changing value in HSV model
        /// Dark blue -> light blue -> turquoise -> green -> yellow -> orange -> red (does not go to purple - reason for value 240 instead of 255)
        /// </summary>
        /// <param name="minValue">Start of range (dark blue color)</param>
        /// <param name="maxValue">End of range (red color)</param>
        /// <param name="newValue">Value for which we want color</param>
        /// <returns>Appropriate color</returns>
        private static Color GetAppropriateColorLinear(double minValue, double maxValue, double newValue)
        {
            double colorValue = (newValue - minValue) / (maxValue - minValue) * 240;

            if (double.IsNaN(colorValue)) // this happens in case minValue and maxValue are same
            {
                return(Color.Red);
            }
            else
            {
                return(Arith.HSVToColor(240 - colorValue, 1, 1));
            }
        }
Example #20
0
        /// <summary>
        /// Do one step of simulation.
        /// </summary>
        /// <param name="time">Required target time in seconds.</param>
        /// <returns>Data for rendering or <code>null</code> if nothing has changed.</returns>
        public MarblesRenderData Simulate(double time)
        {
            if (!Running)
            {
                return(null);
            }

            // {{ TODO: put your own simulation code here

            if (time > Time)
            {
                Frames++;
                float dTime = (float)(time - Time);

                // Simulate (update) the world.
                for (int i = 0; i < centers.Count; i++)
                {
                    Vector3 vel = velocities[i];
                    Vector3 pos = centers[i] + vel * dTime;

                    if (Math.Abs(pos.X) >= 10.0f)
                    {
                        vel.X = -vel.X;
                        pos.X = Arith.Clamp(pos.X, -10.0f, 10.0f);
                    }

                    if (Math.Abs(pos.Y) >= 10.0f)
                    {
                        vel.Y = -vel.Y;
                        pos.Y = Arith.Clamp(pos.Y, -10.0f, 10.0f);
                    }

                    if (Math.Abs(pos.Z) >= 10.0f)
                    {
                        vel.Z = -vel.Z;
                        pos.Z = Arith.Clamp(pos.Z, -10.0f, 10.0f);
                    }

                    velocities[i] = vel;
                    centers[i]    = pos;
                }

                Time = time;
            }

            // Return the current data in a new MarblesRenderData instance.
            return(new MarblesRenderData(centers, radii, colors));

            // }}
        }
Example #21
0
            public void SingleWhirlwind()
            {
                double i = 1;

                for (double t = 0; t <= length; t += interval(i))
                {
                    i += 1;
                    (double x, double y) = Whirl(m, t, rotation, xMid, yMid);
                    double r, g, b;
                    Arith.HSVtoRGB(hue, saturation, t / length, out r, out g, out b);
                    canvas.SetColor(Color.FromArgb((int)(r * 255), (int)(g * 255), (int)(b * 255)));
                    canvas.FillDisc((float)x, (float)y, (float)(radius * (t / length)));
                }
            }
Example #22
0
        public static Color[] GetAll(int Size)
        {
            if (Size < (1 << 24))
            {
                throw new ArgumentException($"Size is not enough to create all colors");
            }
            Color[] all     = new Color[Size];
            int     Counter = 0;

            int fullcount = Size / (1 << 24);

            for (int ser = 0; ser < fullcount; ser++)
            {
                Parallel.For(0, 256, (r) =>
                {
                    for (int g = 0; g < 256; g++)
                    {
                        for (int b = 0; b < 256; b++)
                        {
                            //Flat[x + WIDTH * (y + DEPTH * z)] = Original[x, y, z]
                            all[(ser * (1 << 24)) + b + 256 * (g + 256 * r)] = Color.FromArgb(r, g, b);
                        }
                    }
                });
            }


            Counter += (1 << 24);

            while (true)
            {
                for (int hue = 0; hue < 360; hue++)
                {
                    for (float sa = 0.9f; sa < 1.0f; sa += 0.0002f)
                    {
                        if (Counter >= Size)
                        {
                            return(all);
                        }

                        Arith.HSVToRGB(hue, sa, 1, out double r, out double g, out double b);
                        all[Counter] = Color.FromArgb((int)r, (int)g, (int)b);
                        Counter++;
                        Arith.HSVToRGB(hue, 1 - sa, 1, out r, out g, out b);
                        all[Counter] = Color.FromArgb((int)r, (int)g, (int)b);
                        Counter++;
                    }
                }
            }
        }
Example #23
0
        /// <summary>
        /// Renders the single pixel of an image.
        /// </summary>
        /// <param name="x">Horizontal coordinate.</param>
        /// <param name="y">Vertical coordinate.</param>
        /// <param name="color">Computed pixel color.</param>
        /// <param name="rnd">Shared random generator.</param>
        public override void RenderPixel(int x, int y, double[] color, RandomJames rnd)
        {
            Debug.Assert(color != null);
            Debug.Assert(rnd != null);

            int bands = color.Length;
            int b;

            Array.Clear(color, 0, bands);
            double[] tmp = new double[bands];

            int    i, j, ord;
            double step = 1.0 / superXY;
            double amplitude = Jittering * step;
            double origin = 0.5 * (step - amplitude);
            double x0, y0;

            for (j = ord = 0, y0 = y + origin; j++ < superXY; y0 += step)
            {
                for (i = 0, x0 = x + origin; i++ < superXY; x0 += step)
                {
                    ImageFunction.GetSample(x0 + amplitude * rnd.UniformNumber(),
                                            y0 + amplitude * rnd.UniformNumber(),
                                            ord++, Supersampling, rnd, tmp);
                    for (b = 0; b < bands; b++)
                    {
                        color[b] += tmp[b];
                    }
                }
            }

            double mul = step / superXY;

            if (Gamma > 0.001)
            {                               // gamma-encoding and clamping
                double g = 1.0 / Gamma;
                for (b = 0; b < bands; b++)
                {
                    color[b] = Arith.Clamp(Math.Pow(color[b] * mul, g), 0.0, 1.0);
                }
            }
            else                            // no gamma, no clamping (for HDRI)
            {
                for (b = 0; b < bands; b++)
                {
                    color[b] *= mul;
                }
            }
        }
Example #24
0
 private void glControl1_MouseWheel(object sender, MouseEventArgs e)
 {
     if (e.Delta != 0)
     {
         if (hovercraft)
         {
             // !!! TODO: hovercraft
         }
         else
         {
             float change = e.Delta / 120.0f;
             zoom = Arith.Clamp(zoom * Math.Pow(1.05, change), 0.5, 100.0);
         }
     }
 }
Example #25
0
        /// <summary>
        /// Renders the single pixel of an image.
        /// </summary>
        /// <param name="x">Horizontal coordinate.</param>
        /// <param name="y">Vertical coordinate.</param>
        /// <param name="color">Computed pixel color.</param>
        /// <param name="rnd">Shared random generator.</param>
        public virtual void RenderPixel(int x, int y, double[] color, RandomJames rnd)
        {
            ImageFunction.GetSample(x + 0.5, y + 0.5, color);

            // gamma-encoding:
            if (Gamma > 0.001)
            {                               // gamma-encoding and clamping
                double g = 1.0 / Gamma;
                for (int b = 0; b < color.Length; b++)
                {
                    color[b] = Arith.Clamp(Math.Pow(color[b], g), 0.0, 1.0);
                }
            }
            // else: no gamma, no clamping (for HDRI)
        }
Example #26
0
            private Color GetAppropriateColorSimple(Vector3d normalVector, Vector3d intersectionVector)
            {
                double angle = Vector3d.CalculateAngle(normalVector, rayOrigin - intersectionVector) * 180 / Math.PI;

                double colorValue = angle / 90 * 240;

                if (double.IsNaN(colorValue))
                {
                    return(Color.FromArgb(1, 1, 1, 1));
                }
                else
                {
                    return(Arith.HSVToColor(240 - colorValue, 1, 1));
                }
            }
Example #27
0
        public void ArithJumpingTest()
        {
            using (var cout = new StringWriter())
            {
                Node.Cout = cout;

                var token    = new Token('+');
                var variable = new Id(new Word("x", Tag.ID), VarType.INT, 0);
                var constant = new Constant(new Num(12), VarType.INT);
                var arith    = new Arith(token, variable, constant);
                arith.Jumping(22, 33);

                var actual = cout.ToString();
                Assert.AreEqual("\tif x + 12 goto L22\r\n\tgoto L33\r\n", actual);
            }
        }
Example #28
0
        static void TestBackend()
        {
            Device.PrintInfo();
            Console.WriteLine(Device.BackendCount);
            var arr1 = Data.RandNormal <float>(3, 3);
            var arr2 = Data.RandNormal <float>(3, 3);
            var arr3 = arr1 + arr2;
            var arr4 = Matrix.Multiply(arr1, arr2);
            var arr5 = Arith.Sin(arr1) + Arith.Cos(arr2);

            Util.Print(arr1, "arr1");
            Util.Print(arr2, "arr2");
            Util.Print(arr3, "arr1 + arr2");
            Util.Print(arr4, "arr1 * arr2 (matrix product)");
            Util.Print(arr5, "Sin(arr1) + Cos(arr2)");
        }
Example #29
0
        protected void TiltedRectangle(float cx, float cy, float w, float h, float angle)
        {
            double da   = Arith.DegreeToRadian(angle);
            double sina = Math.Sin(da);
            double cosa = Math.Cos(da);
            double dw   = 0.5 * w;
            double dh   = 0.5 * h;
            double dax  = dw * cosa + dh * sina;
            double day  = -dw * sina + dh * cosa;
            double dbx  = -dw * cosa + dh * sina;
            double dby  = dw * sina + dh * cosa; // A, B, -A, -B

            path.Reset();
            path.AddLine((float)(cx + dax), (float)(cy + day), (float)(cx + dbx), (float)(cy + dby));
            path.AddLine((float)(cx - dax), (float)(cy - day), (float)(cx - dbx), (float)(cy - dby));
            path.AddLine((float)(cx + dax), (float)(cy + day), (float)(cx + dbx), (float)(cy + dby));
        }
Example #30
0
        static void Main(string[] args)
        {
            Arith arith = new Arith();
            int   back1 = arith.AddCli(1, 2);

            Console.WriteLine(back1.ToString());
            int back2 = arith.SubCli(3, 4);

            Console.WriteLine(back2.ToString());
            int back3 = arith.MulCli(4, 5);

            Console.WriteLine(back3.ToString());
            int back4 = arith.DivCli(8, 4);

            Console.WriteLine(back4.ToString());
            Console.ReadLine();
        }