Go Down

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

AWOL

...and why is "color" never mentioned again?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

galantida

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

Chaul

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

Nick Gammon


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?

Code: [Select]

// 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:

Code: [Select]

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

Nick Gammon

If we change "calculate the length" like this:

Code: [Select]

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


We get:

Code: [Select]

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.

Go Up