p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

c_plus_plus_programming thread: Canvas (fwd)


Message #1 by 520059544664-0001@t... on 14 Feb 2002 13:11 GMT



---Urspr=FCngliche Nachricht---

From: "Peace" <520059544664-0001@t...>

To: "C++_Programming" <c_plus_plus_programming@p...>

Subject: [c_plus_plus_programming] Canvas





Please can anybody explain to me what the following C++ program does?





//----------------------------------------------------------------

//Borland C++Builder

//Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.



//----------------------------------------------------------------

//----------------------------------------------------------------

#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 =3D clTeal;

Rotation =3D 0;

PointCount =3D MaxPoints;

RotatePoints();

}

//----------------------------------------------------------------

void __fastcall TFormMain::FormPaint(TObject *Sender)

{

int centerX =3D ClientWidth / 2;

int centerY =3D ClientHeight / 2;

int radius =3D min(centerY, centerX);



Canvas->Ellipse(0, 0, radius*2, radius*2);

int i,j;

for (i =3D 0; i < PointCount; i++) {

for (j =3D 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 =3D 2 * M_PI; // 2 pi radians in a circle

float StepAngle =3D M_2PI / PointCount; // angular distance between points





Rotation +=3D M_PI / 32; // Increment the angle of rotation of figure

if (Rotation > StepAngle)

Rotation -=3D 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 =3D 0, j =3D Rotation; i < PointCount; i++, j +=3D StepAngle) {

Points[i].X =3D cos(j); // These values will be multiplied by the

Points[i].Y =3D sin(j); // current radius at display time.

 }

}

//----------------------------------------------------------------



Your detail explanation will be highly appreciated, thank you.



Horatio





------


  Return to Index