# Data type for coordinates

I am trying optimize routines for drawing primitive shapes for my custom 3D renderer.

What is the best data type for representing a coordinate in 3D space? I thought that integer would be the best choice since calculations involving integer are fast, compared to float or double. Also, Bresenham's algorithm loves integers. But the drawback for using integer is the loss of precision which leads to z fighting. Here is the attempt to draw overlapping 3D triangles if I use integer coordinates.

You can see the rough line on the area where triangles overlap. It is supposed to be straight. On the other hand, if I use float for coordinates,

It works as expected.

I wonder if it is possible to fix the z fighting issue when I use integer coordinates. Maybe there's no other way than using float for coordinates?

Edit:

Here is my code for rendering a single pixel. I thought it might be relevant.

void Renderer::PutPixel(int x, int y, int z, const Colour& col)
{
int offset = y * width + x;
float depth = 1.0 / z;

if (zdepth[offset] < depth)
{
zdepth[offset] = depth;
pixels[offset] = col.argb;
}
}


The z buffer array zdepth is a 1D array of floating point values and pixels is an another 1D array of 32-bit unsigned RGB values.

• 32 bit floats are pretty standard and their performance is comparable to integer on a modern machine. Commented Jun 9, 2022 at 11:25
• @pmw1234 Yeah that's what I ended up but I am still wondering if it is still possible to use integers. Commented Jun 9, 2022 at 15:39
• doing everything with integers is possible, the easiest approach is to make the each step size very small. So a smallish model might be 1000 units across, while the same object when done with floating point might only be 1 or 2 units across. Commented Jun 9, 2022 at 18:07