Drawing rectangle using x and y co-ordinates

Discussion in 'Math' started by killer6008, Feb 9, 2010.

  1. killer6008

    Thread Starter New Member

    Jan 26, 2010
    Hi guys

    I have two co-ordinates (x1,y1) and (x2,y2) and gradient,m1 (y2-y1/x2-x1)

    A straight line is drawn between these two points.

    3 more lines to be drawn each rotated by 90 degrees so when they are joined together it gives a shape very similar to a rectangle.

    These lines will be represented by equations similar to y=mx+c.

    A variable width, which is the width of the rectangle is going to be the length of two of the lines. This needs to be adjusted in the equations as well.

    I am a bit stuck with this. Can someone please help me with this??? I need equations for these lines.

  2. thyristor

    Active Member

    Dec 27, 2009
    Well, have a think about this. One of the lines will have the same gradient as the one you have drawn already. The other two will also have the same gradient but shifted by 90 degrees. What would we need to do to a straight line equation to represent that 90 degree shift?
  3. Papabravo


    Feb 24, 2006
    The gradient(slope) of a perpendicular line is the negative reciprocal. For example if the slope of a line is 1/3 then the slope of a perpendicular line is -3.
  4. Miroslav Obradović

    New Member

    Jul 10, 2009
    Sorry for the long post, but I didn't want to post a short but incomplete solution. Text included below for forum searching, but also attached for easier download.

    There are only three things from geometry you need to know to be able to solve the problem.

    The first is the equation of a line through two given points (x1,y1) and (x2,y2):
    Should be easy to remember, because it looks the same on both sides.
    Note: Your problem should be fairly easy to handle without any equations in two
    special cases of horizontal and vertical lines (i.e. when x1=x2 or y1=y2), so
    I'll assume that both x1!=x2 and y1!=y2 is true (here != means "not equal").

    The second is the vector of a line which is always perpendicular to the line.
    If you have a line y=mx+c (in its explicit form), first you move everything to
    one side and leave the zero on the other side to get its implicit form: mx-y+c=0.
    In general, this implicit form looks like: Ax+By+C=0, and the vector of that line
    is simply (A,B). Note that vectors start at (0,0) and this point is just where
    the vector ends. So, in our case, the vector of the line is (m,-1).

    The third is how to calculate the distance between two given points (x1,y1) and (x2,y2):
    (here d stands for distance and the operator ^2 means the square).

    Now that we know the required math, let's see how to solve the problem:
    a) The first of your lines is through the given points (x1,y1) and (x2,y2).
    After transforming the equation into its explicit form, we get:
    m=(y2-y1)/(x2-x1), c=(y1*x2-x1*y2)/(x2-x1)

    To find the other two points on perpendicular lines, you actually need to
    "move" the points you already have: (x1,y1) and (x2,y2), along the vector of
    the line y=mx+c, by the given distance 'width' ('w' for short).

    b) Calculate the vector of the line: (m,-1) where m is the calculated in step a).

    c) Find an arbitrary point on each of the perpendicular lines by simply adding
    the perpendicular vecotr (m,-1) to points (x1,y1) and (x2,y2). Now you have:
    (x1+m,y1-1) and (x2+m,y2-1)

    d) Now come up with equations of perpendicular lines. The first one goes through:
    (x1,y1) and (x1+m,y1-1)
    while the second one goes through:
    (x2,y2) and (x2+m,y2-1).

    The first perpendicular line is:
    y=y1-(x-x1)/m (*)
    and converted to explicit form looks like:
    We keep simpler form (*) of the equation for easier calculations later on.

    The same way, you get the second perpendicular line:
    y=y2-(x-x2)/m (**)

    e) Finally, find the two points on perpendicular lines which have the desired
    distance from points (x1,y1) and (x2,y2) respectively. Here you get a quadratic
    equation having two solutions, because you can construct the rectangle on both
    sides of the initially given line.

    To find the point of the first perpendicular line, start with the equation for
    distance (putting 'w' for width, instead of 'd'):

    Now, one of the points is (x1,y1) and the other is somewhere on the first
    perpendicular line, i.e. satisfies the equation (*), so it's coordinates are
    (x, y1-(x-x1)/m). In other words, for a given x we calculate y using formula (*).
    In the formula for distance, we now substitute the first point (x1,y1) with
    (x1,y1) and the second point (x2,y2) with (x, y1-(x-x1)/m):
    So now we have:
    From here, it's easy to calculate the two possible 'x' coordinates for the point
    on the first perpendicular line. These two points are from opposite sides of the
    initial line (function sqrt means square root and gives a positive result):

    You choose the side of the initial line on which you want the rectangle by
    picking one of the results. Then calculate the corresponding y using (*).
    This gives you the point (x,y) on the first perpendicular line.

    The same way, you get two x values on the second perpendicular line:
    pick the point from the same side of the initial line and calculate the
    corresponding y. This gives you point (x,y) on the second perpendicular line.

    f) The fourth line goes through the two points on perpendicular lines you've
    found in the previous step.