Go Down

Topic: Programming ??? Math Question (Read 1 time) previous topic - next topic

Nick Gammon


I could have limited my use of floats to the number I am incrementing but I have found in some languages that if you mix in even one integer the answer could be rounded to integer. Not sure how Arduino does it...


Arduino uses C++ which has very well-defined handling of arithmetic.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

galantida

The debug functionality using serial.print has been making my life a lot easier. It does slow the program down a whole lot but it is easy enough to turn on and off.

I did get my line function working so I though I would post my result in case anyone stumbles on this post looking for the same thing. It works pretty fast but if anyone see a way to improve on it please post.

  // set all the points on a line to a color
    void Line(byte x1, byte y1, byte z1, byte x2, byte y2, byte z2, byte Color)
    {
      int x,y,z;
      float xSlope, ySlope, zSlope;
     
      // Lengths
      int xLength = x1 - x2;
      int yLength = y1 - y2;
      int zLength = z1 - z2;
     
      // absolute lengths
      int xAbsolute = abs(xLength);
      int yAbsolute = abs(yLength);
      int zAbsolute = abs(zLength);
     
      // determine longest line
      int MaxLength;
      if ((xAbsolute > yAbsolute) & (xAbsolute > zAbsolute)) MaxLength = xAbsolute;
      else if (yAbsolute > zAbsolute) MaxLength = yAbsolute;
      else MaxLength = zAbsolute;
     
      // calculate the slope change each cycle
      xSlope = xLength/-MaxLength; 
      ySlope = yLength/-MaxLength;
      zSlope = zLength/-MaxLength;;
     
      for (int t=0; t<MaxLength+1; t++)
      {
        x = x1 + (int)(xSlope * t);
        y = y1 + (int)(ySlope * t);
        z = z1 + (int)(zSlope * t);
        Pixel(x,y,z,Color); 
      }
    }

michinyon

Your problem is that a bunch of points in a cube don't actually lie along a line.

Suggest you google Bresenham  algorithm   and follow on from there.

Go Up