Mistři v programování na #hovnokod

C/C++ #4247

Grid snapping (přichytávání kurzoru k mřížce). Kód dělající to samé, po refaktoru: GridRelative[0] = round(MousePos_GridRelative[0]); GridRelative[1] = round(MousePos_GridRelative[1]);

GridRelative[0] = MousePos_GridRelative[0];
GridRelative[1] = MousePos_GridRelative[1];

  if (AppSettings->Snapping)
  {
    MousePos_ModulusPerMeter[0] = fmod( (double)MousePos_AxisRelative[0], PixelsPerGrid ) / PixelsPerMeter;
    MousePos_ModulusPerMeter[1] = fmod( (double)MousePos_AxisRelative[1], -PixelsPerGrid ) / PixelsPerMeter;
    MousePos_ModulusPerMeter[0] = abs(MousePos_ModulusPerMeter[0]);
    MousePos_ModulusPerMeter[1] = abs(MousePos_ModulusPerMeter[1]);

    if (GridRelative[0] < 0 && GridRelative[1] > 0)
    {
      //top-left quadrant, -x +y
      if ( abs(MousePos_ModulusPerMeter[0]) < (0.5 * Scale) )
        GridRelative[0] += MousePos_ModulusPerMeter[0];
      else
        GridRelative[0] -= ( (1.0 * Scale) - MousePos_ModulusPerMeter[0] );

      if ( abs(MousePos_ModulusPerMeter[1]) < (0.5 * Scale) )
        GridRelative[1] -= MousePos_ModulusPerMeter[1];
      else
        GridRelative[1] += ( (1.0 * Scale) - MousePos_ModulusPerMeter[1] );
    }
    else if (GridRelative[0] >= 0 && GridRelative[1] > 0)
    {
      //top-right quadrant, +x +y
      if ( abs(MousePos_ModulusPerMeter[0]) < (0.5 * Scale) )
        GridRelative[0] -= MousePos_ModulusPerMeter[0];
      else
        GridRelative[0] += ( (1.0 * Scale) - MousePos_ModulusPerMeter[0] );

      if ( abs(MousePos_ModulusPerMeter[1]) < (0.5 * Scale) )
        GridRelative[1] -= MousePos_ModulusPerMeter[1];
      else
        GridRelative[1] += ( (1.0 * Scale) - MousePos_ModulusPerMeter[1] );
    }
    else if (GridRelative[0] >= 0 && GridRelative[1] <= 0)
    {
      //bottom-right quadrant, +x -y
      if ( abs(MousePos_ModulusPerMeter[0]) < (0.5 * Scale) )
        GridRelative[0] -= MousePos_ModulusPerMeter[0];
      else
        GridRelative[0] += ( (1.0 * Scale) - MousePos_ModulusPerMeter[0] );

      if ( abs(MousePos_ModulusPerMeter[1]) < (0.5 * Scale) )
        GridRelative[1] += MousePos_ModulusPerMeter[1];
      else
        GridRelative[1] -= ( (1.0 * Scale) - MousePos_ModulusPerMeter[1] );
    }
    else if (GridRelative[0] < 0 && GridRelative[1] <= 0)
    {
      //bottom-left quadrant, -x -y
      if ( abs(MousePos_ModulusPerMeter[0]) < (0.5 * Scale) )
        GridRelative[0] += MousePos_ModulusPerMeter[0];
      else
        GridRelative[0] -= ( (1.0 * Scale) - MousePos_ModulusPerMeter[0] );

      if ( abs(MousePos_ModulusPerMeter[1]) < (0.5 * Scale) )
        GridRelative[1] += MousePos_ModulusPerMeter[1];
      else
        GridRelative[1] -= ( (1.0 * Scale) - MousePos_ModulusPerMeter[1] );
    }
  }

Anonymous,