
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "canmain.h"
#include "math.h"
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
Canvas->Pen->Color = clTeal;
Rotation = 0;
PointCount = MaxPoints;
RotatePoints();
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormPaint(TObject *Sender)
{
int centerX = ClientWidth / 2;
int centerY = ClientHeight / 2;
int radius = min(centerY, centerX);
Canvas->Ellipse(0, 0, radius*2, radius*2);
int i,j;
for (i = 0; i < PointCount; i++) {
for (j = i + 1; j < PointCount; j++) {
Canvas->MoveTo(radius + floor(Points[i].X * radius),
radius + floor(Points[i].Y * radius));
Canvas->LineTo(radius + floor(Points[j].X * radius),
radius + floor(Points[j].Y * radius));
}
}
// A challenge: Turn the rotating figure into a ball that bounces off the
// walls of the window. Don't forget the english (spin) on the ball should
// pick up when bouncing off the wall...
}
//---------------------------------------------------------------------
void __fastcall TFormMain::FormResize(TObject *Sender)
{
Invalidate();
}
//---------------------------------------------------------------------
void __fastcall TFormMain::Timer1Timer(TObject *Sender)
{
RotatePoints();
Invalidate();
}
//---------------------------------------------------------------------
void __fastcall TFormMain::RotatePoints()
{
// NOTE: all figures are in radians
const float M_2PI = 2 * M_PI; // 2 pi radians in a circle
float StepAngle = M_2PI / PointCount; // angular distance between points
Rotation += M_PI / 32; // Increment the angle of rotation of figure
if (Rotation > StepAngle)
Rotation -= StepAngle; // Keep rotation less than distance between points
// The loop below has i walking through the Points array, while j walks
// simultaneously through the angles to each point on the circle.
// Incrementing j by StepAngle moves j to the next point on the circle with
// no complicated arithmetic (everything has been set up in advance of the
// loop). Initializing j with Rotation causes the entire figure to shift
// clockwise a small amount.
//
int i;
float j;
for (i = 0, j = Rotation; i < PointCount; i++, j += StepAngle) {
Points[i].X = cos(j); // These values will be multiplied by the
Points[i].Y = sin(j); // current radius at display time.
}
}
//---------------------------------------------------------------------
这篇博客介绍了如何在图形界面中创建一个动态旋转的多边形,并挑战将旋转图形变成能碰撞墙壁并反弹的球体。通过使用旋转角度、极坐标和椭圆绘制,作者展示了如何实现图形的平滑旋转,并探讨了碰撞检测的实现思路,为图形编程爱好者提供了有趣的实践项目。
230

被折叠的 条评论
为什么被折叠?



