troubleshooting Question

# Oriented bounding rectangle for a line asked on
Game ProgrammingMath / ScienceAlgorithms
6 Comments1 Solution486 ViewsLast Modified:
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;
Double len = Math.Sqrt((Double)((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y)));// +(offset * 2);
rect.X = (Int32)(p1.X - (p1.Y - p2.Y) * offset / len);
rect.Y = (Int32)(p1.Y + (p1.X - p2.X) * offset / len);
rect.X = (Int32)(p1.X + (p1.Y - p2.Y) * offset / len);
rect.Y = (Int32)(p1.Y - (p1.X - p2.X) * offset / len);
rect.X = (Int32)(p2.X + (p1.Y - p2.Y) * offset / len);
rect.Y = (Int32)(p2.Y - (p1.X - p2.X) * offset / len);
rect.X = (Int32)(p2.X - (p1.Y - p2.Y) * offset / len);
rect.Y = (Int32)(p2.Y + (p1.X - p2.X) * offset / len);
return rect;
}
``````
Rectangles.jpg
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
###### 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.
Unlock 1 Answer and 6 Comments.

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.