Remedial Math Question

Discussion in 'Technical Analysis' started by greenleaf, May 17, 2004.

  1. I am embarrassed that I don't know this, but it has been years since I took these types of math classes.

    I want to programmatically draw a line through two points, but these points are not the end points. I want the line to extend through these points by a certain length on either end, say 100 pixels. What would be the formula to calculate the end points if I only know two points in the middle of the line?

    Thanks in advance. I can mock up an illustration if this doesn't make sense.
     
  2. Kap

    Kap

    Eqn of a line : Y=MX+C
     
  3. vikana

    vikana Moderator

    if you want to draw the pixels (as in raster display), Bresenham's (sp?) algorithm is the simplest and fastest.
     
  4. doji

    doji

    Greenleaf,

    Kap's equation is the one you want,

    M is your slope, rise/run, dy/dx and C is your offset constant.
    So if you have two points on the line you can find the slope of the line and with that just solve the equation for the two extended points you want.

    -doji
     
  5. I saw this during my google searches I've been doing all day looking for the answer. Fortunately, there is a function I can use to draw a line so I don't have to go to this low-level. Unfortunately, I don't have the endpoints so I can't call the function.
     
  6. I got that part with Kap's equation. Thanks.

    The issue is that I don't know the x or y for the extended points so I can't solve the equation. I only know that the x and y should end up being 100 (or some number) units/pixels away.

    I am thinking I need to use some form of the Pythagorean Theorem to calculate the dx and dy such that they equal the desired distance from my points. The only problem is I don't know the angles of the triangle, but maybe I can calculate them from the slope of the line or something.
     
  7. vikana

    vikana Moderator

    one other appoach is to use the parametric description of a line:

    t*(p1-p0) + p0,

    p0 and p1 are the two points you know.
    t=0 gives you first point, t=1 gives you the 2nd endpoint.

    then simply normalize t by the length of the line segement and run t >1 and t <0 to get your extended line.
     

  8. Step 1: Figure for the angle of the line you wish to draw using the formula that was given. Angle = y/x (rise/run).

    Step 2: Insert the x value that you wish to solve for. You should know this value as it is how far in the future (or the past) you wish to project the line. Take into account bar spacing in pixels.

    your formula is really y/x = Angle. You know the angle after step 1. Insert that # into the formula and:

    Step 3: solve the equation for y: y = Angle * x

    with computers and pixels, the x values are going to be constant barspacing, the y values are what you are solving for.
     
  9. Yes - i think you need pythag to get the end pts

    If you know:

    - 2 points on line [x0,y0], [x1,y1]
    - distance (D) from endpoint to known pt [x1,y1]

    Assume end point is [x2,y2].

    D*D = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) [by pythag]

    but also:

    Gradient = (y1-y0)/(x1-x0) = (y2-y1)/(x2-x1)

    You now have 2 eqns & 2 unknowns (x2,y2) so you can solve it.

    Hope this helps.
    Hope this is correct !
     
  10. For two points (x1,y1), (x2,y2) the equation of the line is

    (y-y1)/(x-x1)=(y-y2)/(x-x2)
    After solving y, you may plot [supposing x1 < x2] from x1-100 till x2+100 .
    Some T/A software should have this functionality to draw lines.
     
    #10     May 18, 2004