Category : C Source Code
Archive   : C_ROTATE.ZIP
Filename : EXAMPLE2.C

 
Output of file : EXAMPLE2.C contained in archive : C_ROTATE.ZIP
/* Rotate.C Rotate images...
Graphics programming in Turbo C++ p248
*/

#ifdef __TINY__
#error Graphics demos will not run in the tiny modle
#endif

#include
#include
#include
#include
#include
#include
#include "gprint.i"

const double PI = 3.14159254;

int GraphDriver;
int GraphMode;
double AspR;
int xasp, yasp;
int MaxColors;
int ErrorCode = 0;

void Initialize()
{
GraphDriver = DETECT;
initgraph( &GraphDriver, &GraphMode, "C:\\APP\\TC\\BGI");
ErrorCode = graphresult();
if( ErrorCode != grOk )
{
printf(" Graphics System Error: %s\n",
grapherrormsg( ErrorCode) );
exit( 1 );
}
MaxColors = getmaxcolor() + 1;
getaspectratio( &xasp, &yasp );
AspR = (double) xasp / (double) yasp;
}

void Pause()
{
setcolor(WHITE);
settextjustify( CENTER_TEXT, BOTTOM_TEXT );
outtextxy( getmaxx() / 2, getmaxy(), "press any key...");
while ( kbhit() ) getch();
getch();
cleardevice();
}

void Rotate_Point( int *X_Off, int *Y_Off, int degrees)
{
double HypLen, R_Angle, O_Angle = 0;
int Sign_X = 1, Sign_Y = 1;
double X0 = (double) *X_Off + 0.5, Y0 = (double) *Y_Off + 0.5;

degrees = abs(degrees);
HypLen = hypot( X0, Y0);
if( abs(X0) > 0 )
O_Angle = atan2( (-1 * Y0), X0);
else if( Y0 < 0 )
O_Angle = PI / 2;
else
O_Angle = 3 * PI / 2;

if( O_Angle < 0 )
O_Angle += 2 * PI;

R_Angle = (double) degrees / 180 * PI;
R_Angle += O_Angle;
if( R_Angle > 2 * PI )
R_Angle -= 2 * PI;

if( ( R_Angle > PI / 2 ) & ( R_Angle <= 3 * PI / 2 ) )
Sign_X = -1;
if( ( R_Angle > 0 ) & ( R_Angle <= PI ) )
Sign_Y = -1;
if( ( ( R_Angle >= PI / 4 ) & ( R_Angle <= 3 * PI / 4 ) ) |
( ( R_Angle >= 5 * PI / 4 ) & ( R_Angle <= 7 * PI / 4 ) ) )
{
X0 = HypLen * cos( R_Angle );
Y0 = sqrt( pow( HypLen, 2 ) - pow( X0, 2 ) );
}
else
{
Y0 = HypLen * sin( R_Angle );
X0 = sqrt( pow( HypLen, 2 ) - pow( Y0, 2 ) );
}
*X_Off = abs( X0 ) * Sign_X;
*Y_Off = abs( Y0 ) * Sign_Y;
}

void Adj_Rotate_Point( int *X_Off, int *Y_Off, int degrees )
{
int X0 = *X_Off;
int Y0 = *Y_Off / AspR;

Rotate_Point( &X0, &Y0, degrees );
*X_Off = X0;
*Y_Off = Y0 * AspR;
}

void Rotate_Line( int x, int y, int degrees, int x1, int y1, int x2, int y2 )
{
x1 -= x;
y1 -= y;
x2 -= x;
y2 -= y;
Rotate_Point( &x1, &y1, degrees );
Rotate_Point( &x2, &y2, degrees );
line( x+x1, y+y1, x+x2, y+y2 );
}

void Adj_Rotate_Line( int x, int y, int degrees, int x1, int y1, int x2, int y2 )
{
x1 -= x;
y1 -= y;
x2 -= x;
y2 -= y;
Adj_Rotate_Point( &x1, &y1, degrees );
Adj_Rotate_Point( &x2, &y2, degrees );
line( x+x1, y+y1, x+x2, y+y2 );
}

void Show_Rotation( int degrees )
{
int i, j, x, y, Point, radius = 50;

settextjustify( CENTER_TEXT, TOP_TEXT );
outtextxy( 320, 10, "Original");
x = 320;
y = 100;
circle( x, y, radius );
line( x-radius, y+radius * AspR, x+radius, y-radius * AspR );
line( x+radius, y-radius * AspR, x+radius, y-(radius-20) * AspR );
line( x+radius, y-radius * AspR, x+radius-20, y-radius * AspR );
setcolor(RED);
x = 160;
circle( x, y, radius );
setcolor(GREEN);
x = 480;
circle( x, y, radius );
setcolor(WHITE);
x = 160;
y = 30;
gprintf( &x, &y, "Figure rotated %d degrees", degrees );
gprintf( &x, &y, "without aspect correction" );
x = 480;
y = 30;
gprintf( &x, &y, "Figure rotated %d degrees", degrees );
gprintf( &x, &y, "using aspect correction" );

for(i = -50; i <= 50; i++)
for(j = -50; j <= 50; j++)
{
Point = getpixel( 320+i, 100+j );
if( Point > 0 )
{
x = i;
y = j;
Rotate_Point( &x, &y, degrees );
putpixel( 160+x, 100+y, Point );
x = i;
y = j;
Adj_Rotate_Point( &x, &y, degrees );
putpixel( 480+x, 100+y, Point );
}
}
x = 160;
y = 180;
gprintf( &x, &y, "Coordinates rotated %d degrees", degrees );
gprintf( &x, &y, "without aspect correction");
x = 480; y = 180;
gprintf( &x, &y, "Coordinates rotated %d degrees", degrees );
gprintf( &x, &y, "using aspect correction");
setcolor(RED);
x = 160;
y = 250;
circle( x, y, radius );
Rotate_Line( x, y, degrees, x-radius, y+radius * AspR, x+radius, y-radius * AspR );
Rotate_Line( x, y, degrees, x+radius, y-(radius-20) * AspR, x+radius-20, y-radius * AspR );
Rotate_Line( x, y, degrees, x+radius, y-radius * AspR, x+radius-20, y-radius * AspR );
setcolor(GREEN);
x = 480;
y = 250;
circle( x, y, radius);
Adj_Rotate_Line( x, y, degrees, x-radius, y+radius * AspR, x+radius, y-radius * AspR );
Adj_Rotate_Line( x, y, degrees, x+radius, y-(radius-20) * AspR, x+radius-20, y-radius * AspR );
Adj_Rotate_Line( x, y, degrees, x+radius, y-radius * AspR, x+radius-20, y-radius * AspR );
}

main()
{
Initialize();
Show_Rotation( 90 );
Pause();
closegraph();
}



  3 Responses to “Category : C Source Code
Archive   : C_ROTATE.ZIP
Filename : EXAMPLE2.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/