2

Possible Duplicate:
Drawing rectangle between two points with arbitrary width

I have the following

RectangleF GetRectangleFrom2PointsAndWidth(Point p1, Point p2, int width)

enter image description here

How to implement it? (points are located to the end of the middle line from the picture.)

Community
  • 1
  • 1
serhio
  • 28,010
  • 62
  • 221
  • 374
  • 1
    Please see this very similar question: http://stackoverflow.com/questions/7854043/drawing-rectangle-between-two-points-with-arbitrary-width – Kromster Oct 21 '11 at 20:33
  • @Krom: I suppose using sqrt is not very optimal...? – serhio Oct 21 '11 at 20:40
  • is not duplicate! maybe Krom answered, but the question was an other one! and the answer was not even in .NET... – serhio Oct 21 '11 at 20:42
  • 2
    Two Pythagoras c^2 = a^2 + b^2 calculations and a few additions to "construct" new coordinates of remaining two points. Try to use pencil and paper first. – mloskot Oct 21 '11 at 20:47
  • 2
    So you have Point1, Point2 and Width. You need to make a rectangle. How come it is not a very similar question? Also how come you can't write the math code in .Net ? – Kromster Oct 21 '11 at 20:48
  • You ned to be more specific. What 2 points do you have? If they are opposing corner points you don't even need the width, assuming the rect lies along the plane axes. We really need more information. – Sam Axe Oct 21 '11 at 20:52
  • @mloskot I tried. But I am not you ) – serhio Oct 21 '11 at 20:52
  • Well first off then the built-in Rectangle class will not work for you because your rectangle (as shown in the illustration) is not aligned to the planar axes. – Sam Axe Oct 21 '11 at 21:00
  • Seems to me that the width you have is defined by the two points. Basically, you don't have anything here to bound the height of the rectange, therefore it could be of any size. If you had the two red points plus the hieght of the rectangle there might be something to work with. – NealB Oct 21 '11 at 21:04
  • @serhio The updated picture is actually a significant improvement and makes the question answerable. I was on the verge of voting to close before you fixed it. – Chris Cunningham Oct 21 '11 at 21:23

2 Answers2

3

First, find the slope of your middle line. Call it m. Problem: The middle line might be vertical. This could cause problems. So instead calculate deltaX (the change in X) and deltaY (the change in Y) between the points P1 and P2. Thinking of these two numbers as giving you a "direction" is the key to solving this problem.

To make the corners of your rectangle, you want to move from Point 1 in the perpendicular direction. The perpendicular direction is given by -1/m. To avoid the danger of dividing by 0, a better way to think about it is that you want to move in the X direction by -deltaY each time you move in the Y direction by deltaX.

You will want to normalize your "direction" given by -deltaY and deltaX. To do this, find the distance between P1 and P2. I will call this distance D. Now you want to do the following:

  • For the first corner, start at P1 and move in the x-direction by -deltaY / D times width/2. Move in the y-direction by deltaX / D times width/2.

  • For the second corner, start at P1 and move in the x-direction by -deltaY / D times -width/2. Move in the y-direction by deltaX / D times -width/2.

  • For the third corner, start at P2 and move in the x-direction by -deltaY / D times width/2. Move in the y-direction by deltaX / D times width/2.

  • For the fourth corner, start at P2 and move in the x-direction by -deltaY / D times -width/2. Move in the y-direction by deltaX / D times -width/2.

Good luck! What we are working with here is called a vector, but my language in the above answer is a little awkward because I've avoided using almost all of the language of vectors while writing it. The word "normalize" slipped in. Look forward to a class in college called "linear algebra," which will make you an expert at this question.

Chris Cunningham
  • 1,875
  • 1
  • 15
  • 27
1

You can't. Rectangle class can hold only rectangles that are parallel to the x and y axes.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99