Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created January 8, 2021 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/90f0f1896e38a13d98e3d0c84ea45789 to your computer and use it in GitHub Desktop.
Save unitycoder/90f0f1896e38a13d98e3d0c84ea45789 to your computer and use it in GitHub Desktop.
Bresenham Lines
// old unityscript
function drawLine(x0:int, y0:int, x1:int, y1:int)
{
dx= Mathf.Abs(x1-x0);
dy= Mathf.Abs(y1-y0);
if (x0 < x1) {sx=1;}else{sx=-1;}
if (y0 < y1) {sy=1;}else{sy=-1;}
err=dx-dy;
loop=true;
while (loop)
{
texture.SetPixel (x0,y0,Color(1,1,1,1));
if ((x0 == x1) && (y0 == y1)) loop=false;
e2 = 2*err;
if (e2 > -dy)
{
err = err - dy;
x0 = x0 + sx;
}
if (e2 < dx)
{
err = err + dx;
y0 = y0 + sy;
}
}
texture.Apply();
}
@unitycoder
Copy link
Author

simpler version?
int dx = x1-x0;
int dy = y1-y0;
int step = dx/dy;
int y = x0-(x0%1)*step;
int x = Floor(x0);
int xmax = Floor(x1);
while(x<xmax)
{
pset(x,y,1);
x+=1;
y+=step;
}
https://twitter.com/Enichan/status/1445294504177700864/photo/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment