![]() |
#2
C000000012021-09-07 14:19
完整的项目文件
#include<graphics.h> #include<cmath> namespace SPRITE { class Rect { private: double center_x; double center_y; double high; double width; double left_top_x; double left_top_y; double left_bottom_x; double left_bottom_y; double right_top_x; double right_top_y; double right_bottom_x; double right_bottom_y; double top_middle_x; double top_middle_y; double bottom_middle_x; double bottom_middle_y; double right_middle_x; double right_middle_y; double left_middle_x; double left_middle_y; double anchor_x; double anchor_y; void Left_top_x() { left_top_x = center_x - width / 2; } void Left_top_y() { left_top_y = center_y - high / 2; } void Left_bottom_x() { left_bottom_x = center_x - width / 2; } void Left_bottom_y() { left_bottom_y = center_y + high / 2; } void Right_top_x() { right_top_x = center_x + width / 2; } void Right_top_y() { right_top_y = center_y - high / 2; } void Right_bottom_x() { right_bottom_x = center_x + width / 2; } void Right_bottom_y() { right_bottom_y = center_y + high / 2; } void Top_middle_x() { top_middle_x = center_x; } void Top_middle_y() { top_middle_y = center_y - high / 2; } void Bottom_middle_x() { bottom_middle_x = center_x; } void Bottom_middle_y() { bottom_middle_y = center_y + high / 2; } void Right_middle_x() { right_middle_x = center_x + width / 2; } void Right_middle_y() { right_middle_y = center_y; } void Left_middle_x() { left_middle_x = center_x - width / 2; } void Left_middle_y() { left_middle_y = center_y; } void Update_reference(); static const double PI; public: Rect(); Rect(const double& ob_center_x, const double& ob_center_y, const double& ob_high, const double& ob_width); virtual void move_x(const double& value); virtual void move_y(const double& value); virtual void set_anchor_point(const double& ob_anchor_x, const double& ob_anchor_y); virtual void scale_baseon_anchorpoint(const double& value); virtual void rotate_baseon_anchorpoint(const double& angle); virtual const double& CR_center_x()const { return center_x; } virtual const double& CR_center_y()const { return center_y; } virtual const double& CR_high()const { return high; } virtual const double& CR_width()const { return width; } virtual const double& CR_left_bottom_x()const { return left_bottom_x; } virtual const double& CR_left_bottom_y()const { return left_bottom_y; } virtual const double& CR_left_top_x()const { return left_top_x; } virtual const double& CR_left_top_y()const { return left_top_y; } virtual const double& CR_right_bottom_x()const { return right_bottom_x; } virtual const double& CR_right_bottom_y()const { return right_bottom_y; } virtual const double& CR_right_top_x()const { return right_top_x; } virtual const double& CR_right_top_y()const { return right_top_y; } virtual const double& CR_top_middle_x()const { return top_middle_x; } virtual const double& CR_top_middle_y()const { return top_middle_y; } virtual const double& CR_bottom_middle_x()const { return bottom_middle_x; } virtual const double& CR_bottom_middle_y()const { return bottom_middle_y; } virtual const double& CR_right_middle_x()const { return right_middle_x; } virtual const double& CR_right_middle_y()const { return right_middle_y; } virtual const double& CR_left_middle_x()const { return left_middle_x; } virtual const double& CR_left_middle_y()const { return left_middle_y; } }; void Rect::Update_reference() { Left_top_x(); Left_top_y(); Left_bottom_x(); Left_bottom_y(); Right_top_x(); Right_top_y(); Right_bottom_x(); Right_bottom_y(); Top_middle_x(); Top_middle_y(); Bottom_middle_x(); Bottom_middle_y(); Right_middle_x(); Right_middle_y(); Left_middle_x(); Left_middle_y(); return; } Rect::Rect() { center_x = 0; center_y = 0; high = 0; width = 0; anchor_x = center_x; anchor_y = center_y; Update_reference(); return; } Rect::Rect(const double& ob_center_x, const double& ob_center_y, const double& ob_high, const double& ob_width) { center_x = ob_center_x; center_y = ob_center_y; high = ob_high; width = ob_width; anchor_x = center_x; anchor_y = center_y; Update_reference(); return; } void Rect::move_x(const double& value) { center_x += value; Update_reference(); return; } void Rect::move_y(const double& value) { center_y += value; Update_reference(); return; } void Rect::set_anchor_point(const double& ob_anchor_x, const double& ob_anchor_y) { anchor_x = ob_anchor_x; anchor_y = ob_anchor_y; return; } void Rect::scale_baseon_anchorpoint(const double& value) { center_x = (center_x - anchor_x) * value + anchor_x; center_y = (center_y - anchor_y) * value + anchor_y; left_bottom_x = (left_bottom_x - anchor_x) * value + anchor_x; left_bottom_y = (left_bottom_y - anchor_y) * value + anchor_y; left_top_x = (left_top_x - anchor_x) * value + anchor_x; left_top_y = (left_top_y - anchor_y) * value + anchor_y; right_bottom_x = (right_bottom_x - anchor_x) * value + anchor_x; right_bottom_y = (right_bottom_y - anchor_y) * value + anchor_y; right_top_x = (right_top_x - anchor_x) * value + anchor_x; right_top_y = (right_top_y - anchor_y) * value + anchor_y; top_middle_x = (top_middle_x - anchor_x) * value + anchor_x; top_middle_y = (top_middle_y - anchor_y) * value + anchor_y; bottom_middle_x = (bottom_middle_x - anchor_x) * value + anchor_x; bottom_middle_y = (bottom_middle_y - anchor_y) * value + anchor_y; right_middle_x = (right_middle_x - anchor_x) * value + anchor_x; right_middle_y = (right_middle_y - anchor_y) * value + anchor_y; left_middle_x = (left_middle_x - anchor_x) * value + anchor_x; left_middle_y = (left_middle_y - anchor_y) * value + anchor_y; return; } const double Rect::PI = acos(-1); void Rect::rotate_baseon_anchorpoint(const double& angle) { double distance_before, distance_after; double radian; radian = angle * PI / 180; distance_before = sqrt((center_x - anchor_x) * (center_x - anchor_x) + (center_y - anchor_y) * (center_y - anchor_y)); center_x = cos(radian) * (center_x - anchor_x) - sin(radian) * (center_y - anchor_y) + anchor_x; center_y = cos(radian) * (center_y - anchor_y) + sin(radian) * (center_x - anchor_x) + anchor_y; distance_after = sqrt((center_x - anchor_x) * (center_x - anchor_x) + (center_y - anchor_y) * (center_y - anchor_y)); if (distance_before != distance_after) { center_x += (distance_before - distance_after); center_y -= (distance_before - distance_after); } distance_before = sqrt((left_bottom_x - anchor_x) * (left_bottom_x - anchor_x) + (left_bottom_y - anchor_y) * (left_bottom_y - anchor_y)); left_bottom_x = cos(radian) * (left_bottom_x - anchor_x) - sin(radian) * (left_bottom_y - anchor_y) + anchor_x; left_bottom_y = cos(radian) * (left_bottom_y - anchor_y) + sin(radian) * (left_bottom_x - anchor_x) + anchor_y; distance_after = sqrt((left_bottom_x - anchor_x) * (left_bottom_x - anchor_x) + (left_bottom_y - anchor_y) * (left_bottom_y - anchor_y)); if (distance_before != distance_after) { left_bottom_x += (distance_before - distance_after); left_bottom_y -= (distance_before - distance_after); } distance_before = sqrt((left_top_x - anchor_x) * (left_top_x - anchor_x) + (left_top_y - anchor_y) * (left_top_y - anchor_y)); left_top_x = cos(radian) * (left_top_x - anchor_x) - sin(radian) * (left_top_y - anchor_y) + anchor_x; left_top_y = cos(radian) * (left_top_y - anchor_y) + sin(radian) * (left_top_x - anchor_x) + anchor_y; distance_after = sqrt((left_top_x - anchor_x) * (left_top_x - anchor_x) + (left_top_y - anchor_y) * (left_top_y - anchor_y)); if (distance_before != distance_after) { left_top_x += (distance_before - distance_after); left_top_y -= (distance_before - distance_after); } distance_before = sqrt((right_top_x - anchor_x) * (right_top_x - anchor_x) + (right_top_y - anchor_y) * (right_top_y - anchor_y)); right_top_x = cos(radian) * (right_top_x - anchor_x) - sin(radian) * (right_top_y - anchor_y) + anchor_x; right_top_y = cos(radian) * (right_top_y - anchor_y) + sin(radian) * (right_top_x - anchor_x) + anchor_y; distance_after = sqrt((right_top_x - anchor_x) * (right_top_x - anchor_x) + (right_top_y - anchor_y) * (right_top_y - anchor_y)); if (distance_before != distance_after) { right_top_x += (distance_before - distance_after); right_top_y -= (distance_before - distance_after); } distance_before = sqrt((right_bottom_x - anchor_x) * (right_bottom_x - anchor_x) + (right_bottom_y - anchor_y) * (right_bottom_y - anchor_y)); right_bottom_x = cos(radian) * (right_bottom_x - anchor_x) - sin(radian) * (right_bottom_y - anchor_y) + anchor_x; right_bottom_y = cos(radian) * (right_bottom_y - anchor_y) + sin(radian) * (right_bottom_x - anchor_x) + anchor_y; distance_after = sqrt((right_bottom_x - anchor_x) * (right_bottom_x - anchor_x) + (right_bottom_y - anchor_y) * (right_bottom_y - anchor_y)); if (distance_before != distance_after) { right_bottom_x += (distance_before - distance_after); right_bottom_y -= (distance_before - distance_after); } distance_before = sqrt((top_middle_x - anchor_x) * (top_middle_x - anchor_x) + (top_middle_y - anchor_y) * (top_middle_y - anchor_y)); top_middle_x = cos(radian) * (top_middle_x - anchor_x) - sin(radian) * (top_middle_y - anchor_y) + anchor_x; top_middle_y = cos(radian) * (top_middle_y - anchor_y) + sin(radian) * (top_middle_x - anchor_x) + anchor_y; distance_after = sqrt((top_middle_x - anchor_x) * (top_middle_x - anchor_x) + (top_middle_y - anchor_y) * (top_middle_y - anchor_y)); if (distance_before != distance_after) { top_middle_x += (distance_before - distance_after); top_middle_y -= (distance_before - distance_after); } distance_before = sqrt((bottom_middle_x - anchor_x) * (bottom_middle_x - anchor_x) + (bottom_middle_y - anchor_y) * (bottom_middle_y - anchor_y)); bottom_middle_x = cos(radian) * (bottom_middle_x - anchor_x) - sin(radian) * (bottom_middle_y - anchor_y) + anchor_x; bottom_middle_y = cos(radian) * (bottom_middle_y - anchor_y) + sin(radian) * (bottom_middle_x - anchor_x) + anchor_y; distance_after = sqrt((bottom_middle_x - anchor_x) * (bottom_middle_x - anchor_x) + (bottom_middle_y - anchor_y) * (bottom_middle_y - anchor_y)); if (distance_before != distance_after) { bottom_middle_x += (distance_before - distance_after); bottom_middle_y -= (distance_before - distance_after); } distance_before = sqrt((right_middle_x - anchor_x) * (right_middle_x - anchor_x) + (right_middle_y - anchor_y) * (right_middle_y - anchor_y)); right_middle_x = cos(radian) * (right_middle_x - anchor_x) - sin(radian) * (right_middle_y - anchor_y) + anchor_x; right_middle_y = cos(radian) * (right_middle_y - anchor_y) + sin(radian) * (right_middle_x - anchor_x) + anchor_y; distance_after = sqrt((right_middle_x - anchor_x) * (right_middle_x - anchor_x) + (right_middle_y - anchor_y) * (right_middle_y - anchor_y)); if (distance_before != distance_after) { right_middle_x += (distance_before - distance_after); right_middle_y -= (distance_before - distance_after); } distance_before = sqrt((left_middle_x - anchor_x) * (left_middle_x - anchor_x) + (left_middle_y - anchor_y) * (left_middle_y - anchor_y)); left_middle_x = cos(radian) * (left_middle_x - anchor_x) - sin(radian) * (left_middle_y - anchor_y) + anchor_x; left_middle_y = cos(radian) * (left_middle_y - anchor_y) + sin(radian) * (left_middle_x - anchor_x) + anchor_y; distance_after = sqrt((left_middle_x - anchor_x) * (left_middle_x - anchor_x) + (left_middle_y - anchor_y) * (left_middle_y - anchor_y)); if (distance_before != distance_after) { left_middle_x += (distance_before - distance_after); left_middle_y -= (distance_before - distance_after); } return; } class Line :private Rect { private: int linestyle; COLORREF linecolor; int thickness; public: Line(); Line(const const double& ob_center_x, const double& ob_center_y, const double& ob_high = 10, const double& ob_width = 0); virtual ~Line(); virtual void anchor_point(const double& ob_anchor_x, const double& ob_anchor_y); virtual void scalebyanchorpoint(const double& value); virtual void rotatebyanchorpoint(const double& angle); virtual void Setlinestyle(const int& line_style); virtual void Setlinecolor(const COLORREF& line_color); virtual void Setlinethickness(const int& line_thick); virtual void show()const; }; Line::Line() :Rect() { linestyle = PS_SOLID; linecolor = WHITE; thickness = 1; } Line::Line(const const double& ob_center_x, const double& ob_center_y, const double& ob_high, const double& ob_width) :Rect(ob_center_x, ob_center_y, ob_high, ob_width) { linestyle = PS_SOLID; linecolor = WHITE; thickness = 1; } Line::~Line() { } void Line::anchor_point(const double& ob_anchor_x, const double& ob_anchor_y) { set_anchor_point(ob_anchor_x, ob_anchor_y); return; } void Line::scalebyanchorpoint(const double& value) { scale_baseon_anchorpoint(value); return; } void Line::rotatebyanchorpoint(const double& angle) { rotate_baseon_anchorpoint(angle); return; } void Line::Setlinestyle(const int& line_style) { linestyle = line_style; return; } void Line::Setlinecolor(const COLORREF& line_color) { linecolor = line_color; return; } void Line::Setlinethickness(const int& line_thick) { thickness = line_thick; return; } void Line::show()const { setlinecolor(linecolor); setlinestyle(linestyle, thickness); line(CR_center_x(),CR_center_y(), CR_top_middle_x(), CR_top_middle_y()); return; } } int main() { initgraph(640, 480); SPRITE::Line first(320, 40); first.anchor_point(320, 40); first.scalebyanchorpoint(10); first.anchor_point(320, 240); BeginBatchDraw(); while (1) { setlinecolor(WHITE); setlinestyle(PS_DOT,2); first.rotatebyanchorpoint(1); circle(320, 240, 200); first.show(); FlushBatchDraw(); } EndBatchDraw(); closegraph(); return 0; } |
自己写旋转算法旋转将近180度就没办法保持半径了
double distance_before, distance_after;
double radian;
radian = angle * PI / 180;
distance_before = sqrt((center_x - anchor_x) * (center_x - anchor_x) + (center_y - anchor_y) * (center_y - anchor_y));
center_x = cos(radian) * (center_x - anchor_x) - sin(radian) * (center_y - anchor_y) + anchor_x;
center_y = cos(radian) * (center_y - anchor_y) + sin(radian) * (center_x - anchor_x) + anchor_y;
distance_after = sqrt((center_x - anchor_x) * (center_x - anchor_x) + (center_y - anchor_y) * (center_y - anchor_y));
if (distance_before != distance_after)
{
center_x += (distance_before - distance_after);
center_y -= (distance_before - distance_after);
}
distance_before = sqrt((left_bottom_x - anchor_x) * (left_bottom_x - anchor_x) + (left_bottom_y - anchor_y) * (left_bottom_y - anchor_y));
left_bottom_x = cos(radian) * (left_bottom_x - anchor_x) - sin(radian) * (left_bottom_y - anchor_y) + anchor_x;
left_bottom_y = cos(radian) * (left_bottom_y - anchor_y) + sin(radian) * (left_bottom_x - anchor_x) + anchor_y;
distance_after = sqrt((left_bottom_x - anchor_x) * (left_bottom_x - anchor_x) + (left_bottom_y - anchor_y) * (left_bottom_y - anchor_y));
if (distance_before != distance_after)
{
left_bottom_x += (distance_before - distance_after);
left_bottom_y -= (distance_before - distance_after);
}
distance_before = sqrt((left_top_x - anchor_x) * (left_top_x - anchor_x) + (left_top_y - anchor_y) * (left_top_y - anchor_y));
left_top_x = cos(radian) * (left_top_x - anchor_x) - sin(radian) * (left_top_y - anchor_y) + anchor_x;
left_top_y = cos(radian) * (left_top_y - anchor_y) + sin(radian) * (left_top_x - anchor_x) + anchor_y;
distance_after = sqrt((left_top_x - anchor_x) * (left_top_x - anchor_x) + (left_top_y - anchor_y) * (left_top_y - anchor_y));
if (distance_before != distance_after)
{
left_top_x += (distance_before - distance_after);
left_top_y -= (distance_before - distance_after);
}
distance_before = sqrt((right_top_x - anchor_x) * (right_top_x - anchor_x) + (right_top_y - anchor_y) * (right_top_y - anchor_y));
right_top_x = cos(radian) * (right_top_x - anchor_x) - sin(radian) * (right_top_y - anchor_y) + anchor_x;
right_top_y = cos(radian) * (right_top_y - anchor_y) + sin(radian) * (right_top_x - anchor_x) + anchor_y;
distance_after = sqrt((right_top_x - anchor_x) * (right_top_x - anchor_x) + (right_top_y - anchor_y) * (right_top_y - anchor_y));
if (distance_before != distance_after)
{
right_top_x += (distance_before - distance_after);
right_top_y -= (distance_before - distance_after);
}
distance_before = sqrt((right_bottom_x - anchor_x) * (right_bottom_x - anchor_x) + (right_bottom_y - anchor_y) * (right_bottom_y - anchor_y));
right_bottom_x = cos(radian) * (right_bottom_x - anchor_x) - sin(radian) * (right_bottom_y - anchor_y) + anchor_x;
right_bottom_y = cos(radian) * (right_bottom_y - anchor_y) + sin(radian) * (right_bottom_x - anchor_x) + anchor_y;
distance_after = sqrt((right_bottom_x - anchor_x) * (right_bottom_x - anchor_x) + (right_bottom_y - anchor_y) * (right_bottom_y - anchor_y));
if (distance_before != distance_after)
{
right_bottom_x += (distance_before - distance_after);
right_bottom_y -= (distance_before - distance_after);
}
distance_before = sqrt((top_middle_x - anchor_x) * (top_middle_x - anchor_x) + (top_middle_y - anchor_y) * (top_middle_y - anchor_y));
top_middle_x = cos(radian) * (top_middle_x - anchor_x) - sin(radian) * (top_middle_y - anchor_y) + anchor_x;
top_middle_y = cos(radian) * (top_middle_y - anchor_y) + sin(radian) * (top_middle_x - anchor_x) + anchor_y;
distance_after = sqrt((top_middle_x - anchor_x) * (top_middle_x - anchor_x) + (top_middle_y - anchor_y) * (top_middle_y - anchor_y));
if (distance_before != distance_after)
{
top_middle_x += (distance_before - distance_after);
top_middle_y -= (distance_before - distance_after);
}
distance_before = sqrt((bottom_middle_x - anchor_x) * (bottom_middle_x - anchor_x) + (bottom_middle_y - anchor_y) * (bottom_middle_y - anchor_y));
bottom_middle_x = cos(radian) * (bottom_middle_x - anchor_x) - sin(radian) * (bottom_middle_y - anchor_y) + anchor_x;
bottom_middle_y = cos(radian) * (bottom_middle_y - anchor_y) + sin(radian) * (bottom_middle_x - anchor_x) + anchor_y;
distance_after = sqrt((bottom_middle_x - anchor_x) * (bottom_middle_x - anchor_x) + (bottom_middle_y - anchor_y) * (bottom_middle_y - anchor_y));
if (distance_before != distance_after)
{
bottom_middle_x += (distance_before - distance_after);
bottom_middle_y -= (distance_before - distance_after);
}
distance_before = sqrt((right_middle_x - anchor_x) * (right_middle_x - anchor_x) + (right_middle_y - anchor_y) * (right_middle_y - anchor_y));
right_middle_x = cos(radian) * (right_middle_x - anchor_x) - sin(radian) * (right_middle_y - anchor_y) + anchor_x;
right_middle_y = cos(radian) * (right_middle_y - anchor_y) + sin(radian) * (right_middle_x - anchor_x) + anchor_y;
distance_after = sqrt((right_middle_x - anchor_x) * (right_middle_x - anchor_x) + (right_middle_y - anchor_y) * (right_middle_y - anchor_y));
if (distance_before != distance_after)
{
right_middle_x += (distance_before - distance_after);
right_middle_y -= (distance_before - distance_after);
}
distance_before = sqrt((left_middle_x - anchor_x) * (left_middle_x - anchor_x) + (left_middle_y - anchor_y) * (left_middle_y - anchor_y));
left_middle_x = cos(radian) * (left_middle_x - anchor_x) - sin(radian) * (left_middle_y - anchor_y) + anchor_x;
left_middle_y = cos(radian) * (left_middle_y - anchor_y) + sin(radian) * (left_middle_x - anchor_x) + anchor_y;
distance_after = sqrt((left_middle_x - anchor_x) * (left_middle_x - anchor_x) + (left_middle_y - anchor_y) * (left_middle_y - anchor_y));
if (distance_before != distance_after)
{
left_middle_x += (distance_before - distance_after);
left_middle_y -= (distance_before - distance_after);
}
只有本站会员才能查看附件,请 登录