Pages: 1 [2]   Go Down
Author Topic: Programming ??? Math Question  (Read 899 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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); 
      }
    }
Logged

Offline Offline
Edison Member
*
Karma: 28
Posts: 2037
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: