Search code examples
c#xamlgraphicskinectdrawingcontext

Drawing Parallel Lines in C#


Sorry if this has been asked before, but I couldn't find a valid response or a response I could understand

Currently I have a code that draws a line from a Joint to Joint in the Kinect, this forms the Bone:

  drawingContext.DrawLine(drawPen, jointPoints[jointType0], jointPoints[jointType1]);

enter image description here

In the picture aboe it shows Parallel lines joining from circle to cirlce, Can someone please explain to me or show me to create these lines?


Solution

  • If you have a line from point p0 to point p1, and want to create a parallel line / offset line, you need to use either trigonometry or vector math.

    To do it using vector math:

    • Find the direction vector of the line
    • Find a vector perpendicular to that
    • Use the perpendicular vector to offset p0 and p1

    Pseudo code:

    Vector vLine = ( p1 - p0 ).Normalized();
    Vector vPerp = new Vector( -vLine.Y, vLine.X );
    Point newp0 = p0 + vPerp * offset distance
    Point newp1 = p1 + vPerp * offset distance
    DrawLine( newp0, newp1 );
    

    Reverse the offset or negate vPerp to get a line on the other side. How you do this depends on what you have available. System.Windows.Vector and System.Windows.Point should work fine since you're using WPF.

    If you are interested in what's going on here in more detail, get your googling on and search for vector math or linear algebra.