I'm working in C++ with Visual Studio.
I got a strange case. There is my code (simplified version) :
Point GetPoint(/*parameters*/)
{
float Ax = 149; //Value depending on parameters
float Ay = 20;
float Bx = 29;
float By = 19;
double pAB = (Ay - By) / (Ax - Bx);
double Sx = Cx;
double Sy = pAB * (Cx - Ax) + Ay;
// Sy = (20 - 19) / (149 - 29) * (29 - 149) + 20.0
// => Sy = 1 / 120 * -120 + 20
// => Sy = -1 + 20
return new Point(static_cast<int>(Sx), static_cast<int>(Sy));
}
I expected that Sy = 19 but instead my point value is 18. I started the debugger to understand and I found this :
(Ay - By) / (Ax - Bx) = 0.00833333284
(20.0 - 19.0) / (149.0 - 29.0) = 0.0083333333333333332
pAB = 0.0083333337679505348
And :
(20.0 - 19.0) / (149.0 - 29.0) * (29.0 - 149.0) + 20.0 = 19
Sy = 18.999999947845936
Final cast set my point to 18 (sy < 19). But the same code in C# give me a 19. Why theses difference ? And how can I get a 19 ?