Home > Back-end >  This I use double cycle time, give a optimization Suggestions
This I use double cycle time, give a optimization Suggestions

Time:10-24

CodePudding user response:

X1, y1, x2, y2 linear

Y=y1 + (y2 - y1)/(x2 - x1) * (x - x1) including x1 & lt; X & lt; X2
(y2 - y1)/(x2 - x1) to make the simplest fraction a/b, (x2 - x1-1)/b is the answer

Consider that may line is a vertical line, so to distinguish, if it is with another one set of formula of vertical
(x=x1 + x2 - x1)/(y2 - y1) * (y - y1), including y1 & lt; X & lt; Y2

CodePudding user response:

Int dx=x2 - x1, dy=y2 - y1;
If (dx==0) return dy - 1;
If (dy==0) return dx - 1;

At this point was not to 0 dx and dy
Calculation of dx and dy the greatest common factor of c
Return (dx - 1)/(dx/c);
Or
Return (dy - 1)/(dy/c);

CodePudding user response:

Int dx=x2 - x1, dy=y2 - y1;
If (dx==0) return dy - 1;
If (dy==0) return dx - 1;

At this point was not to 0 dx and dy
Calculation of dx and dy the greatest common factor of c
Return (dx - 1)/(dx/c);
Or
Return (dy - 1)/(dy/c);
  • Related