Line Math?

This is a discussion on Line Math? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a line algorithim. I'm going crazy. I just can't seem to get the math strait. Heres ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    31

    Line Math?

    I'm trying to create a line algorithim. I'm going crazy.
    I just can't seem to get the math strait. Heres what i'm doing...

    First, i am getting two points from the user, then getting the slope.

    Then i do this...

    1. Check which percent is smaller.
    2. Moved on the axis with the smaller percent.
    3. Loop to 1

    Oddly enough(not really)
    i will end up with a line like this
    Code:
    KEY: 0's = BLANK, # = LINE
    
    # # 0 0 0
    0 # 0 0 0
    0 # 0 0 0
    0 # 0 0 0
    0 ##0 0 0
    instead of a line like this...
    Code:
    # 0 0 0 0
    0 # 0 0 0
    0 # 0 0 0
    0 # 0 0 0
    0 0 # 0 0
    In short, i can't think of a way to calculate when to move diagonly, insted of horizontaly or verticaly. I've thought about comparing the percents, but i don't know how close they should be for a diagonal move. (If you check the percent of both axis when they (should) move diagonal, they are usually pretty close.)

    Any info would be greatly appreciated.
    I never lie... except for right now.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,688
    Do a web search for "bresenham line algorithm"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    31
    Perfect, thanks salem. If anyone else is interested ...
    I never lie... except for right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  2. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  3. 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
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21