# Construct a regular grid in a rectangular box in 3 dimensional space

1. Dec 2, 2014

### DivergentSpectrum

Im trying to construct a regular grid in a rectangular box in 3 dimensional space
heres what i have to work with:
x has a range between xmin and xmax
y has a range beween ymin and ymax
z has a range between zmin and zmax
the total number of points is limited to a specific number n

i want the height,width,and length=pixelsize of each box in the grid to be equal.

i want to find the formula for pixelsize and a,b,c so that
xmin+pixelsize*(a-1)=xmax
ymin+pixelsize*(b-1)=ymax
zmin+pixelsize*(c-1)=zmax
where a,b,c is the number of x,y,z coordinates respectively

Last edited: Dec 2, 2014
2. Dec 2, 2014

### Staff: Mentor

What do you get when you try to do this with numbers, say a box that is 4" x 6" x 3", with xmin = ymin = zmin = 0, and pixelsize = 1/2"? If necessary, draw a sketch on a piece of paper.

3. Dec 2, 2014

### DivergentSpectrum

ok, i think i got it partially figured out:
pixelsize is given by the cubic equation
a*pixelsize^3+b*pixelsize^2+c*pixelsize+d=0
where
a=xmax*ymax*zmax+ymax*zmax+xmax*zmax+zmax+xmax*ymax+ymax+xmax-n+1;
b = -xmax * ymax * zmin - ymax * zmin - xmax * zmin - zmin - xmax * ymin * zmax - ymin * zmax - xmin * ymax * zmax - xmin * zmax - xmax * ymin - ymin - xmin * ymax - xmin;
c=xmax*ymin*zmin+ymin*zmin+xmin*ymax*zmin+xmin*zmin+xmin*ymin*zmax+xmin*ymin;
d = -xmin * ymin * zmin;

so before i start programming the cubic fomula (ughh), i have a concern as to where this may be right.
as i understand cubics have 3 answers, so which one do i chose? im guessing if im right so far then an equation of this form will only have 1 real root. im kinda hesitant to start i really dont want to do any unneeded work.

4. Dec 2, 2014

### Staff: Mentor

This is pretty ugly, and pretty much unreadable.

Before continuing on, please clarify for me what you're trying to do. You said you're trying to
I think of a grid as a two-dimensional framework. Are you thinking of a three-dimensional lattice of boxes inside the larger box?
What does this mean?
Pixels are normally two-dimensional regions on a (usually) flat surface. Also, pixels emit light. What do you mean by this term (pixel)?

5. Dec 2, 2014

### DivergentSpectrum

yes, a 3 dimensional lattice.
im trying to write a program that evaluates a function of 3 varriables at the places where the lattice bars intersect.

6. Dec 2, 2014

### Staff: Mentor

Why do you need n? If all you're trying to do is to evaluate some function at the lattice points, why not specify what the size of the lattice mesh is?

IMO, this unnecessarily complicates things, as does specifying minimum values for x, y, and z. For starters you can assume that the minimum values are all zero.

For example, let's say you had a box whose dimensions are 3' x 4' x 3' (x, y, and z, respectively), and that the lattice mesh is 6" or 1/2 '. I am assuming that we count all lattice points, including those at the 8 corners and along all 12 edges.

On one of the 3' x 4' faces, there are (3/(1/2) + 1)(4/(1/2) + 1) = (6 + 1)(8 + 1) = 7 * 9 = 63 lattice points. Since there are 3/(1/2) + 1 = 7 vertical rectangles that are parallel to the face I'm working with, then there are 7 * 63 = 441 lattice points in all.

The easiest thing to do is to arbitrarily define the lattice mesh size as "1" so that the box is now 7 x 9 x 7 "units" in size. To get to an individual lattice point, just count off the number of units in each direction and represent it as an ordered triple such as (3, 4, 2).

7. Dec 2, 2014

### DivergentSpectrum

Thanks, i think i was overcomplicating it, the minus 1's dont go there and its easier to deal with array indices this way:
pixelsize=((xmax-xmin)(ymax-ymin)(zmax-zmin)/n)1/3
then
a=(xmax-xmin)/pixelsize
b=(ymax-ymin)/pixelsize
c=(zmax-zmin)/pixelsize
then the lattice points are
xmin+pixelsize*(i)=x
ymin+pixelsize*(j)=y
zmin+pixelsize*(k)=z
where i j and k are integers greater than zero and less than or equal to a,b, or c.

8. Dec 3, 2014

### DivergentSpectrum

i was wrong with my last post.

basically i want to plot a vector field function to see what it looks like but if i render too many arrows at once i get lag problems
i guess my current solution works for limiting the number of arrows, but its not a very pretty/rigorous/accurate solution.