Hi,
I have problem with creating ‘for’ loops to generate tile based road from point to point. It should looks like on the pictures with examples - road should ‘break’ symmetrically depending on the start and end coordinates in all directions.
Thanks for help in advance!
mgear
January 27, 2020, 7:42am
2
can use this to walk through those coordinates,
Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen), as it uses only integer addition, subtraction, and bit shifting, all of which are very cheap operations in historically common computer architectures. It is an incremental error algorithm, and one of...
Thank you.
But how to ‘translate’ this to c#?
mgear
January 27, 2020, 8:14am
4
i have one version here (need to remove some of those extras there though)
{
pixels[pixel] = paintColor.r;
pixels[pixel + 1] = paintColor.g;
pixels[pixel + 2] = paintColor.b;
pixels[pixel + 3] = paintColor.a;
}
// draw line between 2 points (if moved too far/fast)
// http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
public void DrawLine(int startX, int startY, int endX, int endY)
{
int x1 = endX;
int y1 = endY;
int tempVal = x1 - startX;
int dx = (tempVal + (tempVal >> 31)) ^ (tempVal >> 31); // http://stackoverflow.com/questions/6114099/fast-integer-abs-function
tempVal = y1 - startY;
int dy = (tempVal + (tempVal >> 31)) ^ (tempVal >> 31);
int sx = startX < x1 ? 1 : -1;
here’s unityscript version (without those extras)
Thank you very much.
It works very well.
Can I use your implementation for my project? Is it cc0?
mgear
January 27, 2020, 9:39am
6
yes go ahead, i’ve converted it from that wiki article example