Thread: Equation to find endpoints on a line

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    202

    Equation to find endpoints on a line

    What is the equation to find points on a line? My problem is that I need to draw a line with a variable slope and y intercept. Does anyone have any idea how to figure out the co-ordinates to begin and end the line on. I know the equation y=mx+b (where x and y are any real numbers, m is the slope, and b is the Y intercept) but how can I use that to fine the points to start and end my line on if Im only given m and b?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Umm, a line HAS no endpoints...

    If you wanted to draw a line segment between, say, x = -5 and x = +5, then plug those values in for x to obtain y, and those are the points.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Let me clarify...I want to know how I can calculate the coordinates to put into MoveToEx and LineTo functions.How would I calculate where the line goes through the edge of the client area?

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    It depends on the position of your axes in the client area and what scaling you are using, (i.e. how many pixels per axis unit). More info required I'm afraid.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    The client area is 510 pixel by 420 picels (fixed). this is devided by 30 to give the grid. The line will extended from one end to the other. I dont need the code tho I just need the formula.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Figure out the x-value and y-value of each side of the screen.

    use the line equation to calculate the x,y pairs which correxpond to:

    (xmin, y)
    (xmax, y)
    (x, ymin)
    (x, ymax)

    There will only be 2 unique points out of these 4 which are on the screen, these are the points to draw between.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    so basicaly I should put in into y=mx+b with X being the max or min respectivly?

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Isometric
    so basicaly I should put in into y=mx+b with X being the max or min respectivly?
    that gets you two of the 4 points, the min x and min y.

    The other 2 points, you plug into x = (y-b)/m with y = ymin and y = ymax.

    Now, of those 4 points, only 2 unique points will be "on the screen" -- these are the 2 points that the line meets the edge of the screen.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If Width/2 == center point and 0,0 (Width in pixcels)
    Scale == spacing of marks on x axis (in pixcels)

    then
    xMax= Width/(Scale*2)
    xMin= -xMax

    so

    yStart=m*xMin+b
    yEnd =m*xMax+b

    Giving

    MoveToEx(hdc,xMin,yStart,(LPPOINT)NULL);
    LineTo(hdc,xMax,yEnd);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Can't Find this! Dotted line with scissors
    By Stan100 in forum Tech Board
    Replies: 4
    Last Post: 10-17-2005, 08:24 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM