# Programming ??? Math Question

Hello All,

This is my first post and I am happy to be using Arduino!!!

I recently built an 8x8x8 rgb led cube. Well almost done, I have two 8x8 towers going and I am having some fun turning independent LEDs on and off. Now I am trying to write some more advanced graphic routines to turn on more then single LEDs. For example lines and circles. I just can’t seem to get the line rountine to work. Any help would be appreciated.

// set all the points on a line to a color
void Line(float xa, float ya, float za, float xb, float yb, float zb, int color)
{
float x,y,z;

// calculate the legnth
float xl = xa-xb;
float yl = ya-yb;
float zl = za-zb;

for (float t=0; t<8.0f; t++)
{
x = xa + ((xl/8.0f) * t);
y = ya + ((yl/8.0f) * t);
z = za + ((zl/8.0f) * t);
Point(x,y,z,6);
}
}

Okay. But what does happen? What exactly goes wrong?

I would multiply xl by 8 and then divide by t. You would be dealing with bigger numbers and less rounding errors.

As mentioned you need to be specific with what is not working for you.

You want to be able to set the LEDs along a line from (1.2, 5.6, 92.7) to (-184,5, 993.02, -15.392) to a color? Does that really make sense? Your LEDs are at integer positions, are they not? There are some bounds on the reasonable extent of the values, are there not?

What does the Point() function do, Mr. Snippet?

galantida: This is my first post and I am happy to be using Arduino!!!

http://arduino.cc/forum/index.php/topic,148850.0.html

Point 7 plus the other ones.

...and why is "color" never mentioned again?

Thanks for all the replies and sorry for the missing information. I have working with this cube so long that what I think is basic is really not.

The Point function lights the LED at x,y,z at a specified color. My cubes originating point is one of its corners so you would pass any number from zero to seven in the x,y,z. My Cube is also capable of 6 colors so that is also zero to seven. The reason color variable is never used again was I hard coded a six in the point function for color when I was testing.

What the code does as is…
I pass the coordinates to draw a line from 4,1,4 to 4,6,4 at color 6. [e.g. Line(4,1,4,4,6,4,6) ]

I would think this would light the six LEDs between those two points

4,1,4
4,2,4
4,3,4
4,4,4
4,5,4
4,6,4

what it does is light 4,1,4 and 4,1,4.

@PaulS: Are the numbers you list really the ones you came up with. When I walk through the code I does get decimal values that need to be rounded but none are as high as 92? I might be miss calculating?

Thanks for all the replies
Dave

Now that I think about, I've never actually tried to do an increment operation on a floating point number. I would have tried something like t+=1.0f myself not knowing would the post increment operation also work. Then again, you could implement a line drawing algorithm using integers for efficiency, but I suppose I'm only nitpicking here.. Also, ya-yb is a negative, is that what you intended in your design? Maybe that's the key problem here..

galantida:
I pass the coordinates to draw a line from 4,1,4 to 4,6,4 at color 6. [e.g. Line(4,1,4,4,6,4,6) ]

I would think this would light the six LEDs between those two points

4,1,4
4,2,4
4,3,4
4,4,4
4,5,4
4,6,4

what it does is light 4,1,4 and 4,1,4.

Well, let’s test it shall we?

``````// set all the points on a line to a color
void Line(float xa, float ya, float za, float xb, float yb, float zb, int color)
{
float x,y,z;

// calculate the legnth
float xl = xa-xb;
float yl = ya-yb;
float zl = za-zb;

for (float t=0; t<8.0f; t++)
{
x = xa + ((xl/8.0f) * t);
y = ya + ((yl/8.0f) * t);
z = za + ((zl/8.0f) * t);
Point(x,y,z,6);
}
}

void Point (int x, int y, int z, int color)
{
Serial.print (x);
Serial.print (", ");
Serial.print (y);
Serial.print (", ");
Serial.print (z);
Serial.println ();
}

void setup ()
{
Serial.begin (115200);
Serial.println ();
Line(4,1,4,4,6,4,6);
}  // end of setup

void loop () { }
``````

Output:

``````4, 1, 4
4, 0, 4
4, 0, 4
4, 0, 4
4, -1, 4
4, -2, 4
4, -2, 4
4, -3, 4
``````

If we change "calculate the length" like this:

``````    // calculate the legnth
float xl = xb - xa;
float yl = yb - ya;
float zl = zb - za;
``````

We get:

``````4, 1, 4
4, 1, 4
4, 2, 4
4, 2, 4
4, 3, 4
4, 4, 4
4, 4, 4
4, 5, 4
``````

Do you see how adding debugging displays helps? You don't need to speculate, add some Serial.print and see for yourself what is happening.

@PaulS: Are the numbers you list really the ones you came up with. When I walk through the code I does get decimal values that need to be rounded but none are as high as 92? I might be miss calculating?

No, I was trying to make two points. First, I don't see any sanity checking of the input data. That's usually a good idea. Second, I don't understand why the inputs are floats. Don't the line ends correspond to positions where the LEDs are located in the matrix? Aren't the LEDs uniformly spaced? That is, at 0, 1, 2, etc., rather than 0.1, 1.3, 2.7, etc.

Hello thanks again or the replies.

I am an experienced developer but I am new to Arduino style programming. I see now from the replies that I really have to look in to better ways to debug. I assumed that since the code runs on the Arduino I could not use the debug area at the bottom of the development environment. I'll look in to Serial.Print as suggested.

Also there were some questions about why I used floats. Depending on the angle of the line I my be incrementing it by very small values each cycle. For example a line that starts on one row and ends on the next will increment about 0.2 each time. The .2 is not really important but if you throw it out you will never get the next row.

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 but I was trying to get the right answer before I started improving the code. I hope to get the hang of these Arduino C++ nuances.

Thanks again everyone!! Dave G

Are you using the Arduino IDE ?
If so, then Serial.print sends it output to the IDE’s Serial monitor or another serial device listening on the COM port that the USB interface creates.

@UKHeliBob: I am use the IDE. For some reason I didn't think it would be that easy. I thought I would have to write sending and receiving code. I'll give it a shot when I get home tonight.

The more I learn about my Arduino the more I am pleasantly surprised. :)

galantida: Hello All,

This is my first post and I am happy to be using Arduino!!!

I recently built an 8x8x8 rgb led cube. Well almost done, I have two 8x8 towers going and I am having some fun turning independent LEDs on and off. Now I am trying to write some more advanced graphic routines to turn on more then single LEDs. For example lines and circles. I just can't seem to get the line rountine to work. Any help would be appreciated.

That's Bresenham's line draw algorithm. It's used in any graphics code that draws a line from one point to another.

Working with a cube, you have a THREE dimensional space to work with. You may wish to divide the cube into two parallel square planes, one located at the line start and the other at the line end. Then distance between the planes is the line length and the X/Y coordinates in each plane locate the start and end point. Then, use Bresenham to calculate all the points in between.

galantida:
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.

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

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