Thread: Understanding bresenham's line algorithm to move stepper motors

  1. #1
    Registered User
    Join Date
    Jun 2018

    Understanding bresenham's line algorithm to move stepper motors

    I have been trying to understand the code used in GcodeCNCDemo4AxisRAMPS, especially the algorithm used to draw a line. I have been reading several articles on Bresenham's line algorithm.

    code example

    I understand above code and derivation

    But they only explain about slope less than 1 and code used is a lot different than the one implemented here.
    Can anyone help me understand it? Here's the code:
     * Uses bresenham's line algorithm to move both motors
     * @input newx the destination x position
     * @input newy the destination y position
    void line(float newx,float newy,float newz,float newe) {
      a[0].delta = newx-px;
      a[1].delta = newy-py;
      a[2].delta = newz-pz;
      a[3].delta = newe-pe;
      long i,j,maxsteps=0;
      for(i=0; i<NUM_AXIES; ++i) {
        a[i].absdelta = abs(a[i].delta);
        a[i].dir = a[i].delta > 0 ? 1:-1;
        if( maxsteps < a[i].absdelta ) maxsteps = a[i].absdelta;
      for(i=0; i<NUM_AXIES; ++i) {
    #ifdef VERBOSE
      Serial.println(F("Start >"));
      for(i=0; i<maxsteps; ++i) {
        for(j=0; j<NUM_AXIES; ++j) {
          a[j].over += a[j].absdelta;
          if(a[j].over >= maxsteps) {
            a[j].over -= maxsteps;
    #ifdef VERBOSE
      Serial.println(F("< Done."));
    Above code is for moving stepper motors in CNC machine using arduino..

  2. #2
    Registered User
    Join Date
    Feb 2019
    Sorry, but this explanation isn't enough?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Move to specified line in CSV file
    By strokebow in forum C++ Programming
    Replies: 26
    Last Post: 07-03-2012, 09:35 AM
  2. Move pointer to next line in .txt file
    By motarded in forum C++ Programming
    Replies: 6
    Last Post: 03-01-2006, 10:18 AM
  3. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  4. How do you move up a line?
    By epb613 in forum C Programming
    Replies: 3
    Last Post: 05-31-2005, 01:44 PM
  5. Bresenham Circle Algorithm
    By cozman in forum Game Programming
    Replies: 4
    Last Post: 04-30-2002, 09:21 PM

Tags for this Thread