| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3234 人关注过本帖
标题:求一个旋转算法
只看楼主 加入收藏
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
求一个旋转算法
一条直线通过端点绕一个半径为200的圆的圆心进行360度的旋转,在旋转的期间直线的端点一直在圆环上
自己写旋转算法旋转将近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);
        }
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: cos sin 旋转 算法 sqrt 
2021-09-07 14:09
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
收藏
得分:0 
完整的项目文件
#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;
}
2021-09-07 14:19
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
收藏
得分:0 
EasyX_Help.zip (83.33 KB)

easyx图形库
2021-09-07 14:21
diycai
Rank: 8Rank: 8
等 级:贵宾
威 望:19
帖 子:147
专家分:895
注 册:2021-5-18
收藏
得分:20 
太乱了,猜测可能是误差累加导致。
建议删除first.rotatebyanchorpoint方法,改写Line::show()如下,参数懒得加成变量,你自己搞定吧

    void Line::show()const
    {
        static int angle = 0;
        setlinecolor(linecolor);
        setlinestyle(linestyle, thickness);
//       line(CR_center_x(),CR_center_y(), CR_top_middle_x(), CR_top_middle_y());

        angle++;

        double x1, y1, x2, y2;
        double radian;
        radian = angle * acos(-1) / 180;
        
        x1 = 320 + sin(radian) * 200;
        y1 = 40 + (1-cos(radian)) *200;

        x2 = 320 + sin(radian) * 220;
        y2 = 20 + (1-cos(radian)) *220;


        line(x1,y1,x2,y2);

        return;
    }
2021-09-08 12:28
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
这代码确实乱,费眼睛。既 重复的代码不利用函数,又 瞎面向对象。

程序代码:
#define _CRT_SECURE_NO_WARNINGS
#include "graphics.h"
#define _USE_MATH_DEFINES
#include <cmath>

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 <conio.h>

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();
}


easyx 是个啥,伪DOS图形模式?以上代码中,
我用了非标准的 M_PI,你可以改用 <numbers> 中的 std::numbers::pi
传给 line\circle 的应该是 int 类型,你可以用 <cmath> 中的 lround 转换成 long 再强转 int
2021-09-08 16:15
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
收藏
得分:0 
rjsp 结账的时候看漏了,直接就结算了 ,抱歉,抱歉,
easyx 是个图形库,它的line里面的参数是int类型的,我rect类是double的类型,数据交互的时候,从double转int 有精度缩减,从而导致一直错误
2021-09-08 19:10
快速回复:求一个旋转算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.086427 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved