Created
January 8, 2021 17:05
-
-
Save unitycoder/90f0f1896e38a13d98e3d0c84ea45789 to your computer and use it in GitHub Desktop.
Bresenham Lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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