一条直线通过端点绕一个半径为200的圆的圆心进行360度的旋转,在旋转的期间直线的端点一直在圆环上
#define _crt_secure_no_warnings
#include "graphics.h"
#define _use_math_defines
#include
struct point
{
double x;
double y;
point( void ) : x(), y()
{
}
point( double x, double y ) : x(x), y(y)
{
}
point translation( const point& offset ) const // 平移
{
return point( x offset.x, y offset.y );
}
point rotation( const point& anchor, double angle ) const // 旋转
{
double radian = fmod(angle,360) * m_pi/180;
return point( cos(radian)*(x-anchor.x) - sin(radian)*(y-anchor.y) anchor.x
, cos(radian)*(y-anchor.y) sin(radian)*(x-anchor.x) anchor.y );
}
point scale( const point& anchor, double ratio ) const // 缩放
{
return point( (x-anchor.x)*ratio anchor.x, (y-anchor.y)*ratio anchor.y );
}
};
struct line
{
point a;
point b;
line( void )
{
}
line( const point& a, const point& b ) : a(a), b(b)
{
}
line translation( const point& offset ) const // 平移
{
return line( a.translation(offset), b.translation(offset) );
}
line rotation( const point& anchor, double angle ) const // 旋转
{
return line( a.rotation(anchor,angle), b.rotation(anchor,angle) );
}
line scale( const point& anchor, double ratio ) const // 缩放
{
return line( a.scale(anchor,ratio), b.scale(anchor,ratio) );
}
void draw( colorref color=white, int style=ps_dot, int thickness=1 ) const
{
setlinecolor( color );
setlinestyle( style, thickness );
line( a.x, a.y, b.x, b.y );
}
};
struct circle
{
point center;
double radius;
circle( void ) : radius()
{
}
circle( const point& center, double radius ) : center(center), radius(radius)
{
}
void draw( colorref color=white, int style=ps_dot, int thickness=1 ) const
{
setlinecolor( color );
setlinestyle( style, thickness );
circle( center.x, center.y, radius );
}
};
#include
int main( void )
{
initgraph( 640, 480 );
circle( point(320,240), 200 ).draw( white, ps_dot, 2 );
line segment = line( point(320,40), point(320,35) ).scale( point(320,40), 10 );
for( double angle=0; angle<360; angle =1 )
{
segment.rotation( point(320,240), angle ).draw( white, ps_solid, 1 );
}
_getch();
closegraph();
}
阅读(5528) | 评论(0) | 转发(0) |