Search code examples
c#graphicslineregion

How to expand a line to make a rectangle or region


I am working on .net2 so dont have access to the Line class in .net3, though im not sure if that will work.

But I have a line (2 points)

and i want to expand it to width of 4 i.e. like the drawLine Does on Graphics, but i cannot find an easy way to get the region/ graphics path or rectangle for this.

anyone know? and it the line can be in any direction.


Solution

  • I have found the way to do this,

    GraphicsPath gfxPath = new GraphicsPath();
    gfxPath.AddLine(line.x1, line.y1, line.x2, line.y2);
    gfxPath.Widen(new Pen(Color.Blue, lineThickness));//lineThinkness is all that matters
    Region reg = new Region(gfxPath);
    
    if (reg.IsVisible(mousePoint)) // return true if the mousePoint is within the Region.
    

    this widens the line by lineThickness, and you can then use it to check if a point or rect etc is within it.