private void surfaceButton2_Click(object sender, RoutedEventArgs e)
        {
            bool error = false;
            try
            {
                if (_points.Count > 1)
                {
                    HapticShape rect = new HapticPolyline(_points);
                    rect.color(Helper.getBrush(_currentColor));
                    _grid.Children.Add(rect);
                }
                else
                {
                    error = true;
                    surfaceButton1.Background = Brushes.Red;
                }
            }
            catch (FormatException fe)
            {
                error = true;
                surfaceButton1.Background = Brushes.Red;
            }

            if (!error)
                this.Close();
        }
Example #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;
        }