Compare items from the same list

1

I am working with Visual Studio 2015, WPF app. I need to compare elements of the same list.

The scenario is the following I am obtaining positions of the hands with the Kinect device which does it in three dimensions X = valor , Y = valor , Z = valor . But when a movement is registered those values change for that I have implemented the following code, which is always taking data since the device cameras are Time Of Fly.

foreach (var data in skeletons)
        {
            if (SkeletonTrackingState.Tracked == data.TrackingState)
            {
                Joint jointManoDerecha = data.Joints[JointType.HandRight];
                Joint jointManoIzquierda = data.Joints[JointType.HandLeft];

                SkeletonPoint posicionManoDerecha = jointManoDerecha.Position;
                SkeletonPoint posicionManoIzquierda = jointManoIzquierda.Position;

                manoDerecha = string.Format("Mano derecha: X:{0:0.0} Y:{1:0.0} Z{2:0.0}", posicionManoDerecha.X, posicionManoDerecha.Y, 
                    posicionManoDerecha.Z);

                var _manoDerecha = new ManoDerecha()
                {
                    X = posicionManoDerecha.X,
                    Y = posicionManoDerecha.Y,
                    Z = posicionManoDerecha.Z
                };
                listMaDerecha.Add(_manoDerecha);

                manoIzquierda = string.Format("Mano izquierda: X:{0:0.0} Y:{1:0.0} Z{2:0.0}", posicionManoIzquierda.X, posicionManoIzquierda.Y,
                    posicionManoIzquierda.Z);

                var _manoIzquierda = new ManoIzquierda()
                {
                    X = posicionManoIzquierda.X,
                    Y = posicionManoIzquierda.Y,
                    Z = posicionManoIzquierda.Z
                };
                listMaIzquierda.Add(_manoIzquierda);

                // Dibujar huesos
                Brush brush = brushes[iSkeleton % brushes.Length];
                skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.Spine, JointType.ShoulderCenter, JointType.Head));
                skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft));
                skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.ShoulderCenter, JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight));
                skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft));
                skeletonCanvas.Children.Add(GetBodySegment(data.Joints, brush, JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight));

                // Dibujar articulaciones
                foreach (Joint joint in data.Joints)
                {
                    Point jointPos = GetDisplayPosition(joint);
                    var jointLine = new Line();
                    jointLine.X1 = jointPos.X - 3;
                    jointLine.X2 = jointLine.X1 + 6;
                    jointLine.Y1 = jointLine.Y2 = jointPos.Y;
                    jointLine.Stroke = _jointColors[joint.JointType];
                    jointLine.StrokeThickness = 6;
                    skeletonCanvas.Children.Add(jointLine);
                }
            }

            iSkeleton++;
        } // para cada esqueleto

I am stored for each interaction of the ForEach the values of X, Y, Z in a list.

public class ManoIzquierda
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}

I need to compare the values of the same list one position is the data of X, Y, Z if a movement is made the variables take another value, I need to make a comparison.

    
asked by Pedro Ávila 07.11.2017 в 20:39
source

1 answer

3

I believe that the best solution for your problem is to implement in your class ManoIzquierda the interface IEquatable<T> . That way, you can compare two objects by the 3 properties you have, and also use the Contains method to check if an object with the same properties already exists in your list.

The implementation of your class would be like this:

public class ManoIzquierda:IEquatable<ManoIzquierda>
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }

    public bool Equals(ManoIzquierda other)
    {
        return this.X == other.X && this.Y == other.Y && this.Z == other.Z;
    }
}

Now, to compare two objects of type ManoIzquierda you can simply do:

maIzq.Equals(maIzq2)

Also as I said you can use Contains , so in your code you can do:

var _manoIzquierda = new ManoIzquierda()
{
    X = posicionManoIzquierda.X,
    Y = posicionManoIzquierda.Y,
    Z = posicionManoIzquierda.Z
};
if (listaMaIzquierda.Contains(_manoIzquierda)
{
    //Ya existe un objeto con las mismas propiedades, no se ha producido movimiento
}
else
{
   //no existe,lo añadimos a la lista 
   listMaIzquierda.Add(_manoIzquierda);
}
    
answered by 08.11.2017 в 11:15