c++快速开发例子--Canvas

这篇博客介绍了如何在图形界面中创建一个动态旋转的多边形,并挑战将旋转图形变成能碰撞墙壁并反弹的球体。通过使用旋转角度、极坐标和椭圆绘制,作者展示了如何实现图形的平滑旋转,并探讨了碰撞检测的实现思路,为图形编程爱好者提供了有趣的实践项目。

 

//---------------------------------------------------------------------------
#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.
    }
}
//---------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moneytree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值