Skip to content

Instantly share code, notes, and snippets.

@rubenvanassche
Created May 20, 2013 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenvanassche/5615261 to your computer and use it in GitHub Desktop.
Save rubenvanassche/5615261 to your computer and use it in GitHub Desktop.
//project those points
this->projectPoint(point1);
this->projectPoint(point2);
this->projectPoint(point3);
int yMin = roundToInt(std::min(std::min(point1.y, point2.y), point3.y) + 0.5);
int yMax = roundToInt(std::max(std::max(point1.y, point2.y), point3.y) - 0.5);
std::cout << "yMin " << yMin << " yMax" << yMax << std::endl;
if(yMin == yMax){
// No calculation possible, stop at this moment
return;
}
double posInfinity = std::numeric_limits<double>::infinity();
double negInfinity = -std::numeric_limits<double>::infinity();
//AB -> point1 - point2
//BC -> point2 - point3
//CA -> point3 - point1
// our lines
// Now start looping between YMin and YMax
for(int i = yMin;i < yMax;i++){
double XlAB = posInfinity;
double XlCA = posInfinity;
double XlBC = posInfinity;
double XrAB = negInfinity;
double XrCA = negInfinity;
double XrBC = negInfinity;
if((i-point1.y)*(i-point2.y) <= 0 and point1.y != point2.y){
XlAB = point1.x + (point2.x - point1.x)*( (i-point1.y) / (point2.y-point1.y));
XrAB = point1.x + (point2.x - point1.x)*( (i-point1.y) / (point2.y-point1.y));
}
if((i-point2.y)*(i-point3.y) <= 0 and point2.y != point3.y){
XlBC = point2.x + (point3.x - point2.x)*( (i-point2.y) / (point3.y-point2.y));
XrBC = point2.x + (point3.x - point2.x)*( (i-point2.y) / (point3.y-point2.y));
}
if((i-point1.y)*(i-point3.y) <= 0 and point1.y != point3.y){
XlCA = point3.x + (point1.x - point3.x)*( (i-point3.y) / (point1.y-point3.y));
XrCA = point3.x + (point1.x - point3.x)*( (i-point3.y) / (point1.y-point3.y));
}
int Xl = roundToInt(std::min(std::min(XlAB, XlBC), XlCA) + 0.5);
int Xr = roundToInt(std::max(std::max(XrAB, XrBC), XrCA) - 0.5);
Point2D left;
left.x = Xl;
left.y = i;
Point2D right;
right.x = Xr;
right.y = i;
Line2D line;
line.p1 = left;
line.p2 = right;
line.color = color;
linesToDraw.push_back(line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment