Ejemplo n.º 1
0
        private void surfaceButton1_Click(object sender, RoutedEventArgs e)
        {
            bool error = false;
            try
            {
                double x = Convert.ToDouble(surfaceTextBox1.Text);
                double y = Convert.ToDouble(surfaceTextBox2.Text);
                double x_1 = Convert.ToDouble(surfaceTextBox3.Text);
                double y_1 = Convert.ToDouble(surfaceTextBox4.Text);

                Point p = Helper.adjustPoint(new Point(x, y));
                Point p_1 = Helper.adjustPoint(new Point(x_1, y_1));

                HapticShape rect = new HapticLine(p, p_1);
                rect.color(Helper.getBrush(_currentColor));
                _grid.Children.Add(rect);
            }
            catch (FormatException fe)
            {
                error = true;
                surfaceButton1.Background = Brushes.Red;
            }

            if (!error)
                this.Close();
        }
Ejemplo n.º 2
0
        private bool displayFunction(bool useInt)
        {
            String function = textBox1.Text;
            List<Point> functionValues = new List<Point>();
            try
            {
                var regex = new Regex(Regex.Escape("x"));
                var max = double.MinValue;
                var min = double.MaxValue;
                for (int i = 0; i < XY_RANGE; i++)
                {
                    var funct = regex.Replace(function, i.ToString(), 1);
                    NCalc.Expression expr = new NCalc.Expression(funct);
                    var y = 0.0;
                    if (useInt)
                    {
                        y = (int)expr.Evaluate();
                    }
                    else
                    {
                        y = (double)expr.Evaluate();
                    }
                    if (y < min)
                        min = y;
                    if (y > max)
                        max = y;
                    functionValues.Add(new Point(i, y));
                }
                button1.Background = Brushes.Green;

                // Remove all
                this.container.Children.RemoveRange(initialCount, this.container.Children.Count);

                if (hapticObjects.Count != 0)
                {
                    foreach (HapticShape obj in hapticObjects)
                    {
                        HaptiQsManager.Instance.removeObserver(obj);
                    }
                }

                // Scale values
                double scaleFactor = this.Height / XY_RANGE;
                for (int i = 0; i < functionValues.Count; i++)
                {
                    functionValues[i] = new Point(functionValues[i].X * (this.Width / XY_RANGE), this.Height - functionValues[i].Y * scaleFactor + (min < 0 ? min * scaleFactor : 0));
                }

                HapticPolyline polyline = new HapticPolyline(functionValues);
                polyline.color(Brushes.White);
                this.container.Children.Add(polyline);

                hapticObjects.Add(polyline);

                // Create y-axis
                if (line1 != null)
                {
                    HaptiQsManager.Instance.removeObserver(line1);
                }

                line1 = new HapticLine(new Point(0, 0), new Point(0, this.Height));
                line1.color(Brushes.Blue);
                this.container.Children.Add(line1);

                // x-axis
                double mid = this.Height + (min < 0 ? min * scaleFactor : 0);
                if (line != null)
                {
                    HaptiQsManager.Instance.removeObserver(line);
                }
                line = new HapticLine(new Point(0, mid), new Point(this.Width, mid));
                line.color(Brushes.Blue);
                line.thickness(5);
                this.container.Children.Add(line);
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine("Argument exception - function not valid " + ae.Message);
                button1.Background = Brushes.Red;
                return false;
            }
            catch (InvalidCastException ice)
            {
                Console.WriteLine("Invalid Cast Exception - function not valid " + ice.Message);
                button1.Background = Brushes.Red;
                return false;
            }
            catch (EvaluationException ee)
            {
                Console.WriteLine("Evaluation Exception- function not valid " + ee.Message);
                button1.Background = Brushes.Red;
                return false;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception- function not valid " + e.Message);
                button1.Background = Brushes.Red;
                return false;
            }

            return true;
        }