I am trying to draw lines on canvas using my KINECT v2 right Handtip positions. I am getting my right hand tip positions from this line of code.
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper.MapCameraPointToColorSpace(handtipPosition);
And this is my code snippet for drawing the lines, I have defined another point to feed X1 and Y1 coordinates to my line as so
ColorSpacePoint PreviousPoint;
line.X1 = PreviousPoint.X; // ERROR 'Use of possibly Unassigned field X'
line.Y1 = PreviousPoint.Y; // ERROR 'Use of possibly Unassigned field Y'
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
PreviousPoint = handtipPoint;
canvas.Children.Add(line);
but when i use PreviousPoint for assigning coordinates to my X1 and Y1 parameters i get the error 'Use of possibly Unassigned field X' (Which i am guessing occurs cause PreviousPoint has no value at the beginning) and if i fix previousPoint values for X and Y to 0, on my canvas i draw lines from a fixed point (0,0) as only X2 and Y2 follow my hand positions.
Please help me rectify this situation, Appreciate any suggestions and help.
It's still not clear to me what the exact issue is here. However, based on your comment on the other answer, it seems that my guess from my comment an hour ago might be on the mark. In that event, something like this should work for you:
class Form1 : Form // making assumption your code is in a Form
{
ColorSpacePoint? PreviousPoint;
// I have no idea what the actual event handler is supposed to look like.
// This is just a wild guess based on the little bit of code you posted.
void KinectEventHandler(object sender, KinectEventArgs handtip)
{
CameraSpacePoint handtipPosition = handtip.Position;
ColorSpacePoint handtipPoint = _sensor.CoordinateMapper
.MapCameraPointToColorSpace(handtipPosition);
if (PreviousPoint != null)
{
ColorSpacePoint previousPointValue = (ColorSpacePoint)PreviousPoint;
line.X1 = previousPointValue.X;
line.Y1 = previousPointValue.Y;
line.X2 = handtipPoint.X;
line.Y2 = handtipPoint.Y;
canvas.Children.Add(line);
}
PreviousPoint = handtipPoint;
}
}
The above declares a "nullable" instance of ColorSpacePoint
, outside the event handler method. It is initialized to null
by default. In the event handler, a line is added only if this value is not still null, i.e. an initial point has already been received. In either case, the current point replaces the current value of PreviousPoint
, so that that value can be used the next time the event handler is called.
Note that I am assuming ColorSpacePoint
is in fact a value type (i.e. a struct
). I infer this from the error message you show, which should only come from value types. But if it's a reference type (i.e. a class
), then you do not need to declare the field as a "nullable" type, as reference types are already nullable. In that case, you can declare the field as ColorSpacePoint PreviousPoint
, and then use that field directly (e.g. line.X1 = PreviousPoint.X;
) instead of copying it to a local first.
(Actually, you don't strictly need to copy it to a local first anyway...you could just use e.g. PreviousPoint.Value.X
, but because you are accessing multiple fields, I like to move it to a local variable for clarity and to reduce access to the nullable type).