NO

Author Topic: Rotation 2D  (Read 2229 times)

Grincheux

  • Guest
Rotation 2D
« on: April 04, 2016, 10:26:24 AM »
Code: [Select]
LPIMAGEINFOS Effect_Rotate2(LPIMAGEINFOS __lpImageInfos)
{
LPIMAGEINFOS _lpNewImage ;
LPDWORD _lpOldBits, _lpNewBits ;
register int _iLigne ;
register int _iColonne ;
int _iWNew, _iHNew, _iXYNew, _iXYOld ;
double _dCosinus, _dSinus ;
double _dX, _dY, _dXY, _dLigne, _dColonne, _dCentreX, _dCentreY ;

if(!__lpImageInfos) return (NULL) ;

_lpNewImage = ImageCreateEmpty(__lpImageInfos->BitmapInfo.bmiHeader.biWidth * 4,__lpImageInfos->BitmapInfo.bmiHeader.biHeight * 4,0) ;
if(!_lpNewImage) return (__lpImageInfos) ;

_lpOldBits = (LPDWORD) __lpImageInfos->lpImageBits ;
_lpNewBits = (LPDWORD) _lpNewImage->lpImageBits ;

_lpNewBits += (_lpNewImage->BitmapInfo.bmiHeader.biWidth * _lpNewImage->BitmapInfo.bmiHeader.biHeight) ;

_dCosinus = cos(30.0 / 180.0) * 3.1415926535897932384626433832795 ;
_dSinus = sin(30.0 / 180.0) * 3.1415926535897932384626433832795 ;

_dCentreX = __lpImageInfos->BitmapInfo.bmiHeader.biWidth / 2 ;
_dCentreY = __lpImageInfos->BitmapInfo.bmiHeader.biHeight / 2 ;

for(_iLigne = 0 ; _iLigne < __lpImageInfos->BitmapInfo.bmiHeader.biHeight ; _iLigne++)
{
_dLigne = (double) _iLigne ;

for(_iColonne = 0 ; _iColonne < __lpImageInfos->BitmapInfo.bmiHeader.biWidth ; _iColonne++)
{
_dColonne = (double) _iColonne ;

_iXYOld = (__lpImageInfos->BitmapInfo.bmiHeader.biWidth * _iLigne) + _iColonne ;

// X' = (X * Cosinus(Alpha)) - (Y * Sinus(Alpha))
// Y' = (X * Sinus(Alpha)) + (Y * Cosinus(Alpha))

_dX = ((_dColonne - _dCentreX) * _dCosinus) - ((_dLigne - _dCentreY) * _dSinus) ;
_dY = ((_dColonne - _dCentreX) * _dSinus) + ((_dLigne - _dCentreY) * _dCosinus) ;

_dXY = (((double) _lpNewImage->BitmapInfo.bmiHeader.biWidth) * _dY) + _dX ;
_iXYNew = (int) _dXY ;

*(_lpNewBits + _iXYNew) = *(_lpOldBits + _iXYOld) ;
}
}

ImageFree(__lpImageInfos) ;

return (ImageToGflBitmap(_lpNewImage)) ;
}

I want to make a rotation and I think the formula :

      X' = (X * Cosinus(Alpha)) - (Y * Sinus(Alpha))
      Y' = (X * Sinus(Alpha)) + (Y * Cosinus(Alpha))

is good.

My problem is to convert for my image.
The image size is 4 * width and 4 * height of original image.


The crash is here
Quote
*(_lpNewBits + _iXYNew) = *(_lpOldBits + _iXYOld) ;

The variable _iXYNew is wrong, FALSE or too big.

Quote
         _dXY = (((double) _lpNewImage->BitmapInfo.bmiHeader.biWidth) * _dY) + _dX ;
         _iXYNew = (int) _dXY ;

I have the Z axis to zero that comes to this simple formula.
The center is half the size of the original image.

The Point (0,0) of the image is the point (0 - CentreX,0 - CentreY) because image is centered on it.
When I compute X and Y I have the real coordinates.

My problem, my crash problem, is when I compute the XY position into the bitmap.
I am obliged to create a bitmap larger and higher the original because of negative coordinates.

I need help.
« Last Edit: April 04, 2016, 02:01:24 PM by Grincheux »

Sam Steele

  • Guest
Re: Rotation 2D
« Reply #1 on: April 09, 2016, 04:20:10 AM »
This line:

   
Code: [Select]
_lpNewBits += (_lpNewImage->BitmapInfo.bmiHeader.biWidth * _lpNewImage->BitmapInfo.bmiHeader.biHeight) ;
seems wrong.

You point to  the end of your array.

You probably ought to point into the middle of the array:

   
Code: [Select]
_lpNewBits += (_lpNewImage->BitmapInfo.bmiHeader.biWidth * _lpNewImage->BitmapInfo.bmiHeader.biHeight) / 2;
Also, your new bitmap probably doesn’t need to be 4 × 4 = 16 times bigger.
I reckon 1½ × 1½ = 2¼ times bigger would cover any angle.

Nice code.

—Sam.
« Last Edit: April 09, 2016, 06:49:51 AM by Sam Steele »