# How to rotate a wheel image continuously in C ++ using C ++ Builder?

I am trying to constantly rotate the wheel image. I could only spin the wheel. But I want this wheel to spin continuously. Help is needed. Here is the code:

``````void __fastcall TForm1::Button1Click(TObject *Sender)
{
int angle = 45; //45° for example
Graphics::TBitmap *SrcBitmap = new Graphics::TBitmap;
Graphics::TBitmap *DestBitmap = new Graphics::TBitmap;

float Point1x = (-SrcBitmap->Height*sine);
float Point1y = (SrcBitmap->Height*cosine);
float Point2x = (SrcBitmap->Width*cosine-SrcBitmap->Height*sine);
float Point2y = (SrcBitmap->Height*cosine+SrcBitmap->Width*sine);
float Point3x = (SrcBitmap->Width*cosine);
float Point3y = (SrcBitmap->Width*sine);

float minx = min(0,min(Point1x,min(Point2x,Point3x)));
float miny = min(0,min(Point1y,min(Point2y,Point3y)));
float maxx = max(Point1x,max(Point2x,Point3x));
float maxy = max(Point1y,max(Point2y,Point3y));

int DestBitmapWidth  = (int)ceil(fabs(maxx)-minx);
int DestBitmapHeight = (int)ceil(fabs(maxy)-miny);

DestBitmap->Height = DestBitmapHeight;
DestBitmap->Width  = DestBitmapWidth;
Form1->Refresh();

for (int x=0; x < DestBitmapWidth; x++)
{
for (int y=0; y < DestBitmapHeight; y++)
{
int SrcBitmapx = (int)((x+minx)*cosine+(y+miny)*sine);
int SrcBitmapy = (int)((y+miny)*cosine-(x+minx)*sine);

if (SrcBitmapx >=0 && SrcBitmapx < SrcBitmap->Width
&& SrcBitmapy >=0 && SrcBitmapy < SrcBitmap->Height)
{
DestBitmap->Canvas->Pixels[x][y] =
SrcBitmap->Canvas->Pixels[SrcBitmapx][SrcBitmapy];
}
}
}

//Show the rotated bitmap
Image1->Picture->Bitmap=DestBitmap;
delete DestBitmap;
delete SrcBitmap;
}
```

```
+3

source to share

If I understand correctly, you have a wheel image in your image1.bmp file and you want to rotate it constantly. First of all, it seems like you've put your rotation code on the click event! Events are user initiated events and are not suitable for continuous behavior. Usually they are used as triggers for some action. In this case, for example, it is possible to start a continuous rotation and stop it.

If we come to you, create input and output bitmaps first.

``````Graphics::TBitmap *SrcBitmap = new Graphics::TBitmap;
Graphics::TBitmap *DestBitmap = new Graphics::TBitmap;
```

```

and load the input from an external source file (which stands intact !!)

``````SrcBitmap->LoadFromFile("image1.bmp");
```

```

then your code rotates the TImage object and then rotates every pixel ... Then sets the results to the output bitmap, which is a dynamic Timage object ... Since the original pixels don't change continuously, you always start with the same data and endup with the same result!

My suggestion is to load the image object before starting continuous rotation (for example, during Form.Show) and then revisit the code starting with the bitmap generated from the TImage object (which was loaded on Form.Show) and output the stream back to that bitmap. How to sum an array of integers to a sum variable:

``````int sum = 0
for(int i=0;i<ListSize;i++)
sum = sum+List[i];
```

```

Perhaps, after your corrections, you cannot get the correct behavior while turning. You can use timers to handle the RPM so you end up with a hostile spinning wheel.

0

source

All Articles