Advertisement

05.22.2008 at 10:04AM PDT, ID: 23425138
[x]
Attachment Details

Oriented bounding rectangle for a line

Asked by Bill-Hanson in Graphics & Game Programming, Math & Science Questions, Algorithms

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:  http://www.experts-exchange.com/Q_22951696.html#20261839Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
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;
        }
Attachments:
 
 
[+][-]05.22.2008 at 12:00PM PDT, ID: 21626618

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2008 at 12:14PM PDT, ID: 21626741

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2008 at 12:19PM PDT, ID: 21626784

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.22.2008 at 12:30PM PDT, ID: 21626864

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Graphics & Game Programming, Math & Science Questions, Algorithms
Sign Up Now!
Solution Provided By: ozo
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.23.2008 at 12:55PM PDT, ID: 21635325

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628