troubleshooting Question

# Oriented bounding rectangle for a line

Game ProgrammingMath / ScienceAlgorithms
I'm working on a flowchart application, and need to define the click-able region for a shape connector (line-segment).

To do this, I a need to generate the vertexes for an oriented rectangle that fully encloses an arbitrary line.  In addition, I need to be able to specify some padding (in pixels) such that the resulting rectangle will be slightly larger than the line.

The code below is the closest solution I have found, but the resulting rectangle does not include the padding at the end of the line, only at the sides.

The image below illustrates the difference between what the GetSegmentBounds function does and what I need it to do.

For reference, I found the code below here:  https://www.experts-exchange.com/Q_22951696.html#20261839
``````protected Point[] GetSegmentBounds(Point p1, Point p2, Double offset)
{
Point[] rect = new Point[4];
Double len = Math.Sqrt((Double)((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y)));// +(offset * 2);
rect[0].X = (Int32)(p1.X - (p1.Y - p2.Y) * offset / len);
rect[0].Y = (Int32)(p1.Y + (p1.X - p2.X) * offset / len);
rect[1].X = (Int32)(p1.X + (p1.Y - p2.Y) * offset / len);
rect[1].Y = (Int32)(p1.Y - (p1.X - p2.X) * offset / len);
rect[2].X = (Int32)(p2.X + (p1.Y - p2.Y) * offset / len);
rect[2].Y = (Int32)(p2.Y - (p1.X - p2.X) * offset / len);
rect[3].X = (Int32)(p2.X - (p1.Y - p2.Y) * offset / len);
rect[3].Y = (Int32)(p2.Y + (p1.X - p2.X) * offset / len);
return rect;
}
``````
Rectangles.jpg
Join the community to see this answer!
###### Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.