public MovementVector()
 {
     _startPoint = new Point3D();
     _endPoint = new Point3D();
     _angle = 0;
     _distance = 0;
 }
        public MovementVector(Point3D startPoint, Point3D endPoint)
        {
            _startPoint = startPoint;
            _endPoint = endPoint;

            _distance = DistanceBetwenTwoPoints();
            _angle = AngleBetweenTwoPointsInDegrees();
        }
        // Linear Regression Function
        // Returns the new generated y' values
        public Point3D SmoothingLine(Dictionary<JointType, List<Point3D>> smoothingBuffer, Point3D lastCoordinates, JointType joint)
        {
            double meanX = 0;
            double meanY = 0;
            double sumXY = 0;
            double sqX = 0;
            double sumSqX = 0;
            double sqMeanX = 0;
            double meanXY = 0;
            double sumX = 0;
            double sumY = 0;
            double alfa = 0;
            double betha = 0;

            //for (var counter = 0; counter < smoothingBuffer[joint].Count; counter++)
            // {
            foreach (var listItem in smoothingBuffer[joint])
            {
                sumX += listItem.X;
                sumY += listItem.Y;
                sumXY += listItem.X * listItem.Y;
                sumSqX += listItem.X * listItem.X;

            }

            meanX = sumX / smoothingBuffer[joint].Count;
            meanY = sumY / smoothingBuffer[joint].Count;
            meanXY = sumXY / smoothingBuffer[joint].Count;
            sqMeanX = meanX * meanX;

            betha = (sumXY - (smoothingBuffer[joint].Count * meanXY)) / (sumSqX - (smoothingBuffer[joint].Count * sqMeanX));
            alfa = meanY - (betha * meanX);

            // }

            lastCoordinates.Y = alfa + (betha * lastCoordinates.X);

            return lastCoordinates;
        }
 public static double DistanceBetwenTwoPoints(Point3D startPoint, Point3D endPoint)
 {
     return DistanceBetwenTwoPoints(startPoint.X - endPoint.X, startPoint.Y - endPoint.Y);
 }
 public static double AngleBetweenTwoPointsInDegrees(Point3D startPoint, Point3D endPoint)
 {
     return AngleBetweenTwoPointsInDegrees(startPoint.X - endPoint.X, startPoint.Y - endPoint.Y);
 }
        public void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            var reference = e.FrameReference.AcquireFrame();

            // Color
            colorFrame = reference.ColorFrameReference.AcquireFrame();
            {
                if (colorFrame != null)
                {

                    if (_mode == CameraMode.Color)
                    {
                        camera.Source = colorFrame.ToBitmap();
                    }
                }
            }

            // Depth
            depthFrame = reference.DepthFrameReference.AcquireFrame();
            {
                if (depthFrame != null)
                {
                    if (_mode == CameraMode.Depth)
                    {
                        cameraDepth.Source = depthFrame.ToBitmap();
                    }
                }
            }

            Ellipse ellipse1 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse2 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            Ellipse ellipse3 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse4 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            Ellipse ellipse5 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse6 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            Ellipse ellipse7 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse8 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            Ellipse ellipse9 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse10 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            Ellipse ellipse11 = new Ellipse
            {
                Fill = Brushes.Green,
                Width = 30,
                Height = 30
            };
            Ellipse ellipse12 = new Ellipse
            {
                Fill = Brushes.Red,
                Width = 30,
                Height = 30
            };

            if (finishedCountdown)
            {
                if (_mode == CameraMode.Color)
                {
                    camera.Visibility = Visibility.Visible;
                    canvas.Visibility = Visibility.Visible;
                    cameraDepth.Visibility = Visibility.Hidden;
                    canvasDepth.Visibility = Visibility.Hidden;
                    finishedCountdown = false;
                }

                if (_mode == CameraMode.Depth)
                {
                    camera.Visibility = Visibility.Hidden;
                    canvas.Visibility = Visibility.Hidden;
                    cameraDepth.Visibility = Visibility.Visible;
                    canvasDepth.Visibility = Visibility.Visible;
                    finishedCountdown = false;
                }
            }

            // Body
            using (var frame = reference.BodyFrameReference.AcquireFrame())
            {
                if (frame != null)
                {

                    this.canvas.Children.Clear();
                    this.canvasDepth.Children.Clear();

                    _bodies = new Body[frame.BodyFrameSource.BodyCount];

                    frame.GetAndRefreshBodyData(_bodies);

                    //int bodyIndex = 0;
                    foreach (var body in _bodies)
                    {
                        //if (bodyIndex != 0) continue;
                        //bodyIndex++;
                        var cameraSpacePoint = new CameraSpacePoint();

                        DepthSpacePoint depthPointWristLeft = new DepthSpacePoint();
                        DepthSpacePoint depthPointWristRight = new DepthSpacePoint();
                        DepthSpacePoint depthPointHead = new DepthSpacePoint();
                        DepthSpacePoint depthPointSpineMid = new DepthSpacePoint();
                        DepthSpacePoint depthPointKneeLeft = new DepthSpacePoint();
                        DepthSpacePoint depthPointKneeRight = new DepthSpacePoint();

                        ColorSpacePoint colorPointWristLeft = new ColorSpacePoint();
                        ColorSpacePoint colorPointWristRight = new ColorSpacePoint();
                        ColorSpacePoint colorPointHead = new ColorSpacePoint();
                        ColorSpacePoint colorPointSpineMid = new ColorSpacePoint();
                        ColorSpacePoint colorPointKneeLeft = new ColorSpacePoint();
                        ColorSpacePoint colorPointKneeRight = new ColorSpacePoint();

                        if (body.IsTracked)
                        {
                            BodyStructure currentFrameBody = new BodyStructure();

                            var lines = new List<string>();

                            foreach (var joint in body.Joints.Values)
                            {
                                cameraSpacePoint.X = joint.Position.X;//(float)currentFrameBody.currentFrameBody[joint.JointType].X;
                                cameraSpacePoint.Y = joint.Position.Y; //(float)currentFrameBody.currentFrameBody[joint.JointType].Y;
                                cameraSpacePoint.Z = joint.Position.Z; //(float)currentFrameBody.currentFrameBody[joint.JointType].Z;

                                if (PressSpaced && !escape && jointsOfInterest.Contains(joint.JointType))
                                {
                                    curNumOfFrames++;
                                    distanceSum[joint.JointType] += cameraSpacePoint.Z;
                                    meanDistanceToSensor[joint.JointType] = distanceSum[joint.JointType] / ( (curNumOfFrames/6) + 1);
                                }
                                else if(escape && movementVector != null && jointsOfInterest.Contains(joint.JointType))
                                {
                                    distanceScale[joint.JointType] = cameraSpacePoint.Z / meanDistanceToSensor[joint.JointType];
                                }

                                var colorPoint = _sensor.CoordinateMapper.MapCameraPointToColorSpace(cameraSpacePoint);
                                var depthPoint = _sensor.CoordinateMapper.MapCameraPointToDepthSpace(cameraSpacePoint);

                                if (_mode == CameraMode.Depth)
                                {
                                    /*  Ellipse ellipseBlue = new Ellipse
                                      {
                                          Fill = Brushes.Blue,
                                          Width = 10,
                                          Height = 10
                                      };
                                      var xPos = depthPoint.X - ellipseBlue.Width / 2;
                                      var yPos = depthPoint.Y - ellipseBlue.Height / 2;

                                      if (xPos >= 0 && xPos < this.canvasDepth.ActualWidth &&
                                          yPos >= 0 && yPos < this.canvasDepth.ActualHeight)
                                      {
                                          Canvas.SetLeft(ellipseBlue, xPos);
                                          Canvas.SetTop(ellipseBlue, yPos);

                                          canvasDepth.Children.Add(ellipseBlue);
                                      } */

                                    Extensions.DrawSkeleton(canvas, body, jointsOfInterest, _sensor);

                                    //currentFrameBody.currentFrameBody.Add(joint.JointType, new Point3D());
                                    if (jointsOfInterest.Contains(joint.JointType))
                                    {
                                        if (joint.JointType == JointType.HandLeft)
                                            depthPointWristLeft = depthPoint;

                                        else if (joint.JointType == JointType.HandRight)
                                            depthPointWristRight = depthPoint;

                                        else if (joint.JointType == JointType.Head)
                                            depthPointHead = depthPoint;

                                        else if (joint.JointType == JointType.SpineMid)
                                            depthPointSpineMid = depthPoint;

                                        else if (joint.JointType == JointType.KneeRight)
                                            depthPointKneeRight = depthPoint;

                                        else if (joint.JointType == JointType.KneeLeft)
                                            depthPointKneeLeft = depthPoint;

                                        currentFrameBody.currentFrameBody.Add(joint.JointType, new Point3D(depthPoint.X, depthPoint.Y, 0));
                                    }
                                }
                                else
                                {
                                      Ellipse ellipseBlue = new Ellipse
                                      {
                                          Fill = Brushes.Blue,
                                          Width = 10,
                                          Height = 10
                                      };
                                      var xPos = colorPoint.X - ellipseBlue.Width / 2;
                                      var yPos = colorPoint.Y - ellipseBlue.Height / 2;

                                      if (xPos >= 0 && xPos < this.canvas.ActualWidth &&
                                          yPos >= 0 && yPos < this.canvas.ActualHeight)
                                      {
                                          Canvas.SetLeft(ellipseBlue, xPos);
                                          Canvas.SetTop(ellipseBlue, yPos);

                                          canvas.Children.Add(ellipseBlue);
                                      }

                                    Extensions.DrawSkeleton(canvas, body, jointsOfInterest, _sensor);

                                    //currentFrameBody.currentFrameBody.Add(joint.JointType, new Point3D());
                                    if (jointsOfInterest.Contains(joint.JointType))
                                    {
                                        if (joint.JointType == JointType.HandLeft)
                                            colorPointWristLeft = colorPoint;

                                        else if (joint.JointType == JointType.HandRight)
                                            colorPointWristRight = colorPoint;

                                        else if (joint.JointType == JointType.Head)
                                            colorPointHead = colorPoint;

                                        else if (joint.JointType == JointType.SpineMid)
                                            colorPointSpineMid = colorPoint;

                                        else if (joint.JointType == JointType.KneeRight)
                                            colorPointKneeRight = colorPoint;

                                        else if (joint.JointType == JointType.KneeLeft)
                                            colorPointKneeLeft = colorPoint;

                                        var pointCurJoint = new Point3D(colorPoint.X, colorPoint.Y, 0);
                                        this.dictAllMovementPositions[joint.JointType].Add(pointCurJoint);
                                        currentFrameBody.currentFrameBody.Add(joint.JointType, pointCurJoint);
                                    }
                                }
                            }

                            /*TODO - null or empty Dictionary  (.Count == 0)*/
                            //  if (movementVector == null)
                            if (PressSpaced && !escape)
                            {
                                if (movementVector != null && jointsChange)
                                {
                                    movementVector.Clear();
                                }

                                jointsChange = false;
                                movementVector = record.GetMovementVectors(currentFrameBody);

                            }
                            //else if(movementVector != null)
                            else if (escape && movementVector != null)
                            {
                                var curFrameResult = benchPress.ValidateCurrentSkeletonFrame(movementVector, currentFrameBody, distanceScale);
                                var resultWristLeft = curFrameResult.BoneStates[JointType.HandLeft];
                                var resultWristRight = curFrameResult.BoneStates[JointType.HandRight];

                                Extensions.DrawSkeleton(canvas, body, jointsOfInterest, _sensor);

                                if (recordVisualization)
                                {
                                    recordVisualization = false;

                                    Color colorHandLeft = Color.FromArgb(255, 35, 63, 147);
                                    Color colorHandRight = Color.FromArgb(255, 0, 136, 170);
                                    Color colorHead = Color.FromArgb(255, 193, 84, 151);
                                    Color colorSpineMid = Color.FromArgb(255, 238, 198, 20);
                                    Color colorKneeLeft = Color.FromArgb(255, 116, 81, 67);
                                    Color colorKneeRight = Color.FromArgb(255, 94, 58, 106);

                                    foreach (var joy in this.dictAllMovementPositions)
                                    {
                                        Color fillColor = Color.FromArgb(255, 255, 255, 255);
                                        switch (joy.Key)
                                        {
                                            case JointType.HandLeft:
                                                fillColor = colorHandLeft;
                                                break;

                                            case JointType.HandRight:
                                                fillColor = colorHandRight;
                                                break;

                                            case JointType.Head:
                                                fillColor = colorHead;
                                                break;

                                            case JointType.SpineMid:
                                                fillColor = colorSpineMid;
                                                break;

                                            case JointType.KneeLeft:
                                                fillColor = colorKneeLeft;
                                                break;

                                            case JointType.KneeRight:
                                                fillColor = colorKneeRight;
                                                break;
                                        }

                                        foreach (var point in joy.Value)
                                        {

                                           float opacityFactor =  joy.Value.Count / 255  ;
                                            if (opacityFactor == 0.0f)
                                                opacityFactor = 1.0f;

                                            int alpha = (int)opacityFactor * (joy.Value.IndexOf(point) + 1);

                                            fillColor = SetTransparency(alpha, fillColor);

                                            Brush fill = new SolidColorBrush(fillColor);

                                            Ellipse newEllipse = new Ellipse
                                            {
                                                Fill = fill,
                                                Width = 20,
                                                Height = 20
                                            };

                                            var xPosRecord = point.X - newEllipse.Width / 2;
                                            var yPosRecord = point.Y - newEllipse.Height / 2;

                                            if (xPosRecord >= 0 && xPosRecord < this.Record.ActualWidth &&
                                                yPosRecord >= 0 && yPosRecord < this.Record.ActualHeight)
                                            {
                                                Canvas.SetLeft(newEllipse, xPosRecord);
                                                Canvas.SetTop(newEllipse, yPosRecord);

                                                Record.Children.Add(newEllipse);
                                            }
                                        }
                                    }
                                }

                                if (sixJoints.IsChecked == true)
                                {
                                    var resultHead = curFrameResult.BoneStates[JointType.Head];
                                    var resultSpineMid = curFrameResult.BoneStates[JointType.SpineMid];
                                    var resultKneeLeft = curFrameResult.BoneStates[JointType.KneeLeft];
                                    var resultKneeRight = curFrameResult.BoneStates[JointType.KneeRight];

                                    if (_mode == CameraMode.Depth)
                                    {
                                        if (resultSpineMid == SkeletonBoneState.Matched)
                                        {
                                            var xPosMid = depthPointSpineMid.X - ellipse7.Width / 2;
                                            var yPosMid = depthPointSpineMid.Y - ellipse7.Height / 2;

                                            if (xPosMid >= 0 && xPosMid < this.canvasDepth.ActualWidth &&
                                                yPosMid >= 0 && yPosMid < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse7, xPosMid);
                                                Canvas.SetTop(ellipse7, yPosMid);

                                                canvasDepth.Children.Add(ellipse7);
                                            }
                                        }
                                        else
                                        {
                                            var xPosMid = depthPointSpineMid.X - ellipse8.Width / 2;
                                            var yPosMid = depthPointSpineMid.Y - ellipse8.Height / 2;

                                            if (xPosMid >= 0 && xPosMid < this.canvasDepth.ActualWidth &&
                                                yPosMid >= 0 && yPosMid < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse8, xPosMid);
                                                Canvas.SetTop(ellipse8, yPosMid);

                                                canvasDepth.Children.Add(ellipse8);
                                            }
                                        }

                                        if (resultKneeLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosKneeLeft = depthPointKneeLeft.X - ellipse9.Width / 2;
                                            var yPosKneeLeft = depthPointKneeLeft.Y - ellipse9.Height / 2;

                                            if (xPosKneeLeft >= 0 && xPosKneeLeft < this.canvasDepth.ActualWidth &&
                                                yPosKneeLeft >= 0 && yPosKneeLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse9, xPosKneeLeft);
                                                Canvas.SetTop(ellipse9, yPosKneeLeft);

                                                canvasDepth.Children.Add(ellipse9);
                                            }
                                        }
                                        else
                                        {
                                            var xPosKneeLeft = depthPointKneeLeft.X - ellipse10.Width / 2;
                                            var yPosKneeLeft = depthPointKneeLeft.Y - ellipse10.Height / 2;

                                            if (xPosKneeLeft >= 0 && xPosKneeLeft < this.canvasDepth.ActualWidth &&
                                                yPosKneeLeft >= 0 && yPosKneeLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse10, xPosKneeLeft);
                                                Canvas.SetTop(ellipse10, yPosKneeLeft);

                                                canvasDepth.Children.Add(ellipse10);
                                            }
                                        }

                                        if (resultKneeRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosKneeRight = depthPointKneeRight.X - ellipse11.Width / 2;
                                            var yPosKneeRight = depthPointKneeRight.Y - ellipse11.Height / 2;

                                            if (xPosKneeRight >= 0 && xPosKneeRight < this.canvasDepth.ActualWidth &&
                                                yPosKneeRight >= 0 && yPosKneeRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse11, xPosKneeRight);
                                                Canvas.SetTop(ellipse11, yPosKneeRight);

                                                canvasDepth.Children.Add(ellipse11);
                                            }
                                        }
                                        else
                                        {
                                            var xPosKneeRight = depthPointKneeRight.X - ellipse12.Width / 2;
                                            var yPosKneeRight = depthPointKneeRight.Y - ellipse12.Height / 2;

                                            if (xPosKneeRight >= 0 && xPosKneeRight < this.canvasDepth.ActualWidth &&
                                                yPosKneeRight >= 0 && yPosKneeRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse12, xPosKneeRight);
                                                Canvas.SetTop(ellipse12, yPosKneeRight);

                                                canvasDepth.Children.Add(ellipse12);
                                            }
                                        }

                                        if (resultHead == SkeletonBoneState.Matched)
                                        {
                                            var xPosHead = depthPointHead.X - ellipse5.Width / 2;
                                            var yPosHead = depthPointHead.Y - ellipse5.Height / 2;

                                            if (xPosHead >= 0 && xPosHead < this.canvasDepth.ActualWidth &&
                                                yPosHead >= 0 && yPosHead < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse5, xPosHead);
                                                Canvas.SetTop(ellipse5, yPosHead);

                                                canvasDepth.Children.Add(ellipse5);
                                            }

                                        }
                                        else
                                        {
                                            var xPosHead = depthPointHead.X - ellipse6.Width / 2;
                                            var yPosHead = depthPointHead.Y - ellipse6.Height / 2;

                                            if (xPosHead >= 0 && xPosHead < this.canvasDepth.ActualWidth &&
                                                yPosHead >= 0 && yPosHead < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse6, xPosHead);
                                                Canvas.SetTop(ellipse6, yPosHead);

                                                canvasDepth.Children.Add(ellipse6);
                                            }
                                        }

                                        if (resultWristLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosLeft = depthPointWristLeft.X - ellipse1.Width / 2;
                                            var yPosLeft = depthPointWristLeft.Y - ellipse1.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvasDepth.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse1, xPosLeft);
                                                Canvas.SetTop(ellipse1, yPosLeft);

                                                canvasDepth.Children.Add(ellipse1);
                                            }

                                        }
                                        else
                                        {
                                            var xPosLeft = depthPointWristLeft.X - ellipse2.Width / 2;
                                            var yPosLeft = depthPointWristLeft.Y - ellipse2.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvasDepth.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse2, xPosLeft);
                                                Canvas.SetTop(ellipse2, yPosLeft);

                                                canvasDepth.Children.Add(ellipse2);
                                            }
                                        }

                                        if (resultWristRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosRight = depthPointWristRight.X - ellipse3.Width / 2;
                                            var yPosRight = depthPointWristRight.Y - ellipse3.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvasDepth.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse3, xPosRight);
                                                Canvas.SetTop(ellipse3, yPosRight);

                                                canvasDepth.Children.Add(ellipse3);
                                            }

                                        }
                                        else
                                        {
                                            var xPosRight = depthPointWristRight.X - ellipse4.Width / 2;
                                            var yPosRight = depthPointWristRight.Y - ellipse4.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvasDepth.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse4, xPosRight);
                                                Canvas.SetTop(ellipse4, yPosRight);

                                                canvasDepth.Children.Add(ellipse4);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (resultSpineMid == SkeletonBoneState.Matched)
                                        {
                                            var xPosMid = colorPointSpineMid.X - ellipse7.Width / 2;
                                            var yPosMid = colorPointSpineMid.Y - ellipse7.Height / 2;

                                            if (xPosMid >= 0 && xPosMid < this.canvas.ActualWidth &&
                                                yPosMid >= 0 && yPosMid < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse7, xPosMid);
                                                Canvas.SetTop(ellipse7, yPosMid);

                                                canvas.Children.Add(ellipse7);
                                            }
                                        }
                                        else
                                        {
                                            var xPosMid = colorPointSpineMid.X - ellipse8.Width / 2;
                                            var yPosMid = colorPointSpineMid.Y - ellipse8.Height / 2;

                                            if (xPosMid >= 0 && xPosMid < this.canvas.ActualWidth &&
                                                yPosMid >= 0 && yPosMid < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse8, xPosMid);
                                                Canvas.SetTop(ellipse8, yPosMid);

                                                canvas.Children.Add(ellipse8);
                                            }
                                        }

                                        if (resultKneeLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosKneeLeft = colorPointKneeLeft.X - ellipse9.Width / 2;
                                            var yPosKneeLeft = colorPointKneeLeft.Y - ellipse9.Height / 2;

                                            if (xPosKneeLeft >= 0 && xPosKneeLeft < this.canvas.ActualWidth &&
                                                yPosKneeLeft >= 0 && yPosKneeLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse9, xPosKneeLeft);
                                                Canvas.SetTop(ellipse9, yPosKneeLeft);

                                                canvas.Children.Add(ellipse9);
                                            }
                                        }
                                        else
                                        {
                                            var xPosKneeLeft = colorPointKneeLeft.X - ellipse10.Width / 2;
                                            var yPosKneeLeft = colorPointKneeLeft.Y - ellipse10.Height / 2;

                                            if (xPosKneeLeft >= 0 && xPosKneeLeft < this.canvas.ActualWidth &&
                                                yPosKneeLeft >= 0 && yPosKneeLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse10, xPosKneeLeft);
                                                Canvas.SetTop(ellipse10, yPosKneeLeft);

                                                canvas.Children.Add(ellipse10);
                                            }
                                        }

                                        if (resultKneeRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosKneeRight = colorPointKneeRight.X - ellipse11.Width / 2;
                                            var yPosKneeRight = colorPointKneeRight.Y - ellipse11.Height / 2;

                                            if (xPosKneeRight >= 0 && xPosKneeRight < this.canvas.ActualWidth &&
                                                yPosKneeRight >= 0 && yPosKneeRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse11, xPosKneeRight);
                                                Canvas.SetTop(ellipse11, yPosKneeRight);

                                                canvas.Children.Add(ellipse11);
                                            }
                                        }
                                        else
                                        {
                                            var xPosKneeRight = colorPointKneeRight.X - ellipse12.Width / 2;
                                            var yPosKneeRight = colorPointKneeRight.Y - ellipse12.Height / 2;

                                            if (xPosKneeRight >= 0 && xPosKneeRight < this.canvas.ActualWidth &&
                                                yPosKneeRight >= 0 && yPosKneeRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse12, xPosKneeRight);
                                                Canvas.SetTop(ellipse12, yPosKneeRight);

                                                canvas.Children.Add(ellipse12);
                                            }
                                        }

                                        if (resultHead == SkeletonBoneState.Matched)
                                        {
                                            var xPosHead = colorPointHead.X - ellipse5.Width / 2;
                                            var yPosHead = colorPointHead.Y - ellipse5.Height / 2;

                                            if (xPosHead >= 0 && xPosHead < this.canvas.ActualWidth &&
                                                yPosHead >= 0 && yPosHead < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse5, xPosHead);
                                                Canvas.SetTop(ellipse5, yPosHead);

                                                canvas.Children.Add(ellipse5);
                                            }

                                        }
                                        else
                                        {
                                            var xPosHead = colorPointHead.X - ellipse6.Width / 2;
                                            var yPosHead = colorPointHead.Y - ellipse6.Height / 2;

                                            if (xPosHead >= 0 && xPosHead < this.canvas.ActualWidth &&
                                                yPosHead >= 0 && yPosHead < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse6, xPosHead);
                                                Canvas.SetTop(ellipse6, yPosHead);

                                                canvas.Children.Add(ellipse6);
                                            }
                                        }

                                        if (resultWristLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosLeft = colorPointWristLeft.X - ellipse1.Width / 2;
                                            var yPosLeft = colorPointWristLeft.Y - ellipse1.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvas.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse1, xPosLeft);
                                                Canvas.SetTop(ellipse1, yPosLeft);

                                                canvas.Children.Add(ellipse1);
                                            }

                                        }
                                        else
                                        {
                                            var xPosLeft = colorPointWristLeft.X - ellipse2.Width / 2;
                                            var yPosLeft = colorPointWristLeft.Y - ellipse2.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvas.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse2, xPosLeft);
                                                Canvas.SetTop(ellipse2, yPosLeft);

                                                canvas.Children.Add(ellipse2);
                                            }
                                        }

                                        if (resultWristRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosRight = colorPointWristRight.X - ellipse3.Width / 2;
                                            var yPosRight = colorPointWristRight.Y - ellipse3.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvas.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse3, xPosRight);
                                                Canvas.SetTop(ellipse3, yPosRight);

                                                canvas.Children.Add(ellipse3);
                                            }

                                        }
                                        else
                                        {
                                            var xPosRight = colorPointWristRight.X - ellipse4.Width / 2;
                                            var yPosRight = colorPointWristRight.Y - ellipse4.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvas.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse4, xPosRight);
                                                Canvas.SetTop(ellipse4, yPosRight);

                                                canvas.Children.Add(ellipse4);
                                            }
                                        }
                                    }
                                }

                                else
                                {
                                    if (_mode == CameraMode.Depth)
                                    {
                                        if (resultWristLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosLeft = depthPointWristLeft.X - ellipse1.Width / 2;
                                            var yPosLeft = depthPointWristLeft.Y - ellipse1.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvasDepth.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse1, xPosLeft);
                                                Canvas.SetTop(ellipse1, yPosLeft);

                                                canvasDepth.Children.Add(ellipse1);
                                            }

                                        }
                                        else
                                        {
                                            var xPosLeft = depthPointWristLeft.X - ellipse2.Width / 2;
                                            var yPosLeft = depthPointWristLeft.Y - ellipse2.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvasDepth.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse2, xPosLeft);
                                                Canvas.SetTop(ellipse2, yPosLeft);

                                                canvasDepth.Children.Add(ellipse2);
                                            }
                                        }

                                        if (resultWristRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosRight = depthPointWristRight.X - ellipse3.Width / 2;
                                            var yPosRight = depthPointWristRight.Y - ellipse3.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvasDepth.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse3, xPosRight);
                                                Canvas.SetTop(ellipse3, yPosRight);

                                                canvasDepth.Children.Add(ellipse3);
                                            }

                                        }
                                        else
                                        {
                                            var xPosRight = depthPointWristRight.X - ellipse4.Width / 2;
                                            var yPosRight = depthPointWristRight.Y - ellipse4.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvasDepth.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvasDepth.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse4, xPosRight);
                                                Canvas.SetTop(ellipse4, yPosRight);

                                                canvasDepth.Children.Add(ellipse4);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (resultWristLeft == SkeletonBoneState.Matched)
                                        {
                                            var xPosLeft = colorPointWristLeft.X - ellipse1.Width / 2;
                                            var yPosLeft = colorPointWristLeft.Y - ellipse1.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvas.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse1, xPosLeft);
                                                Canvas.SetTop(ellipse1, yPosLeft);

                                                canvas.Children.Add(ellipse1);
                                            }

                                        }
                                        else
                                        {
                                            var xPosLeft = colorPointWristLeft.X - ellipse2.Width / 2;
                                            var yPosLeft = colorPointWristLeft.Y - ellipse2.Height / 2;

                                            if (xPosLeft >= 0 && xPosLeft < this.canvas.ActualWidth &&
                                                yPosLeft >= 0 && yPosLeft < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse2, xPosLeft);
                                                Canvas.SetTop(ellipse2, yPosLeft);

                                                canvas.Children.Add(ellipse2);
                                            }
                                        }

                                        if (resultWristRight == SkeletonBoneState.Matched)
                                        {
                                            var xPosRight = colorPointWristRight.X - ellipse3.Width / 2;
                                            var yPosRight = colorPointWristRight.Y - ellipse3.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvas.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse3, xPosRight);
                                                Canvas.SetTop(ellipse3, yPosRight);

                                                canvas.Children.Add(ellipse3);
                                            }

                                        }
                                        else
                                        {
                                            var xPosRight = colorPointWristRight.X - ellipse4.Width / 2;
                                            var yPosRight = colorPointWristRight.Y - ellipse4.Height / 2;

                                            if (xPosRight >= 0 && xPosRight < this.canvas.ActualWidth &&
                                                yPosRight >= 0 && yPosRight < this.canvas.ActualHeight)
                                            {
                                                Canvas.SetLeft(ellipse4, xPosRight);
                                                Canvas.SetTop(ellipse4, yPosRight);

                                                canvas.Children.Add(ellipse4);
                                            }
                                        }
                                    }
                                }

                            }
                            else
                            {
                                escape = false;
                            }
                        }
                    }
                }
            }

            if (depthFrame != null)
                depthFrame.Dispose();
            depthFrame = null;
            if (colorFrame != null)
                colorFrame.Dispose();
            colorFrame = null;
        }