c# 4.0 - linear increasing algorithm in c# -


from start point 3 variables, x,y,z point; best linear algorithm.

example: x:0, y=0, z=0 x:10, y=20, z=50.

the way looking equal steps on each homogenised increase block.

edit ambiguity: asked question motor control. motors controlling x , y coordinates, can't perform steps @ same time. motion has ladder or squares. so, looking smallest ladder steps 3 (or more) coordinates, least squares logic, in c#.

using integer coordinates, can divide differences greatest common divisor:

var gcd = gcd(x2 - x1, y2 - y1, z2 - z1); var stepx = (x2 - x1) / gcd; var stepy = (y2 - y1) / gcd; var stepz = (z2 - z1) / gcd; 

some lines have 2 steps, since can't divided evenly in 3 dimensions. (e.g. (0,0,0)-(17,19,23))


alternativly, use bresenham's line algorithm

public static ienumerable<point> getpointsonline(int x0, int y0, int z0, int x1, int y1, int z1) {     bool swapxy = math.abs(y1 - y0) > math.abs(x1 - x0);     bool swapxz = math.abs(z1 - z0) > math.abs(x1 - x0);      if (swapxy)     {         int tmp;         tmp = x0; x0 = y0; y0 = tmp;         tmp = x1; x1 = y1; y1 = tmp;     }      if (swapxz)     {         int tmp;         tmp = x0; x0 = z0; z0 = tmp;         tmp = x1; x1 = z1; z1 = tmp;     }      int deltax = math.abs(x1 - x0);     int deltay = math.abs(y1 - y0);     int deltaz = math.abs(z1 - z0);      int driftxy = deltax / 2;     int driftxz = deltax / 2;      int stepx = x0 <= x1 ? 1 : -1;     int stepy = y0 <= y1 ? 1 : -1;     int stepz = z0 <= z1 ? 1 : -1;      int y = y0;     int z = z0;      (int x = x0; x*stepx <= x1*stepx; x += stepx)     {         int xc = swapxy ? y : swapxz ? z : x;         int yc = swapxy ? swapxz ? z : x : y;         int zc = swapxz ? x : z;          yield return new point(xc, yc, zc);          driftxy -= deltay;         if (driftxy < 0)         {             y += stepy;             driftxy += deltax;         }          driftxz -= deltaz;         if (driftxz < 0)         {             z += stepz;             driftxz += deltax;         }     } } 

demonstration


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -