Itutorial
Legendary OT User
- Joined
- Dec 23, 2014
- Messages
- 2,329
- Solutions
- 68
- Reaction score
- 1,007
I can't figure it out. Anyone able to make a tutorial or explain it here? I have some fixes I want to introduce to TFS but I just cannot figure it out.
If someone has the time this is a fix for the sightline code by @zbizu
If someone has the time this is a fix for the sightline code by @zbizu
Code:
//get a line between two points
//based on Xiaolin Wu algorithm
std::vector< std::pair<int, int> > get2DLine(int x0, int y0, int x1, int y1)
{
std::vector< std::pair<int, int> > line;
if (x0 == x1 && y0 == y1) {
return line;
}
bool isSteep = std::abs(y1 - y0) > std::abs(x1 - x0);
if (isSteep) {
std::swap(x0, y0);
std::swap(x1, y1);
}
if (x0 > x1) {
std::swap(x0, x1);
if (y0 < y1) {
std::swap(y0, y1);
}
}
float dx = x1 - x0;
float dy = y1 - y0;
double grad = 1.0;
if (dx != 0) {
grad = dy * 1.0 / dx;
}
if (grad < 0) {
grad = grad * -1;
}
double interY = y0 + grad; //first y - intersection for the main loop
if (isSteep) {
for (int y = x0 + 1; y < x1; y++) {
if (interY < 0) {
interY = interY * -1;
}
int newX = std::floor(interY);
int newY = y;
line.push_back({ newX, newY });
interY += grad;
}
}
else {
for (int x = x0 + 1; x < x1; x++) {
if (interY < 0) {
interY = interY * -1;
}
int newX = x;
int newY = std::floor(interY);
line.push_back({ newX, newY });
interY += grad;
}
}
// now we need to perform a jump between floors to see if everything is clear (literally)
return line;
}