Description
This article demonstrates how to rotate a polygon about its
center. The polygon is a square, which is drawn using the Polygon()
Win32 API function. All components on the form are created at runtime.
PolygonRotation.cpp
PolygonRotation.h
Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.
#include <vcl.h>
#pragma hdrstop
#include "PolygonRotate.h"
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int X_old;
int Y_old;
POINT Poly1a[5] = {{100, 100}, {300, 100}, {300, 300}, {100, 300}, {100,100}};
POINT *pPoly1a = Poly1a;
POINT Poly1b[5] = {{500, 100}, {700, 100}, {700, 300}, {500, 300}, {500,100}};
POINT *pPoly1b = Poly1b;
POINT Poly2a[5];
POINT* pPoly2a = Poly2a;
POINT Poly2b[5];
POINT* pPoly2b = Poly2b;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Position = poScreenCenter;
Width = 800;
Height = 600;
Color = (TColor)0xabcdef; // Cream
}
//---------------------------------------------------------------------------
void Translate(POINT* pin, POINT* pout, long DX, long DY)
{
for (int i = 0; i < 5; i++)
{
(pout+i)->x = (pin+i)->x + DX;
(pout+i)->y = (pin+i)->y + DY;
}
}
void Rotate(POINT* pin, POINT* pout, float Alpha, long xr, long yr,
int NumberOfPoints)
{
//Rotate Polygon about a given point P whose coordinates are (xr, yr)
// Degrees to radians
float const K = -0.01745329; // 2*3.141592653/360
for (int i = 0; i < NumberOfPoints; i++)
{
(pout+i)->x = (long)(xr + (((pin+i)->x - xr) * cos(K*Alpha))
- (((pin+i)->y - yr) * sin(K*Alpha)));
(pout+i)->y = (long)(yr + (((pin+i)->x - xr) * sin(K*Alpha))
+ (((pin+i)->y - yr) * cos(K*Alpha)));
}
}
//---------------------------------------------------------------------------
void Translate(POINT* pin, POINT* pout, long xt, long yt,
int NumberOfPoints)
{
//Tanslate Polygon
for (int i = 0; i< NumberOfPoints; i++)
{
(pout+i)->x = (long)((pin+i)->x + xt);
(pout+i)->y = (long)((pin+i)->y + yt);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
X_old = X;
Y_old = Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if (X_old != X)
{
// Convert X mouse movement into polygon rotation
Rotate(pPoly1a, pPoly2a, X, 200, 200, 5);
Polygon(Canvas->Handle, pPoly2a, 5); //Win32 API
}
if (Y_old != Y)
{
// Convert Y mouse movement into polygon rotation
Rotate(pPoly1b, pPoly2b, Y, 600, 200, 5);
Polygon(Canvas->Handle, pPoly2b, 5); //Win32 API
}
X_old = X;
Y_old = Y;
}
//---------------------------------------------------------------------------
#ifndef PolygonRotateH
#define PolygonRotateH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif