c++ - 创建箭头类

标签 c++

我正在尝试创建一个带有两个点的箭头类。这是我的:

struct Arrow : Lines {              // an Arrow is a Line that has an arrow at the end
    Arrow(Point p1, Point p2)       // construct an Arrow from two points
        :p1(p1), p2(p2) {}
    void draw_lines() const;

    Point p1;
    Point p2;
};

这是我拥有的 draw_lines() const 函数:

void Arrow::draw_lines() const
{
    Lines arrow;
    arrow.add(p1, p2);
    arrow.draw_lines();

}

它通过输入 Arrow a(Point(x,y), Point(x1,y1)); 来工作。然后假设使用 (x,y) 计算箭头,它变成 p1,(x1,y1) 变成 p2,作为方向指南,(x1,y1) 作为箭头基点。我希望箭头看起来像这样,只有实心:--->。如何计算箭头指向的角度?箭头的线条需要为两个x,y坐标,如(p2.x, p2.y, (箭头后点相对于p2的x坐标), (箭头后点相对于p2的y坐标p2)..x 和.y 符号返回点 p 的 x 和 y 坐标。

感谢您的帮助。

最佳答案

这是使用 atan2 的样子。

const double pi = 3.1415926535897931;
const int r = 5;
const double join_angle = pi / 6.0; // 30 degrees
const double stem_angle = atan2(p2.y-p1.y, p2.x-p1.x);

Lines arrow;
arrow.add(p1, p2);
arrow.add(p2, Point(p2.x - r*cos(stem_angle+join_angle), p2.y - r*sin(stem_angle+join_angle)));
arrow.add(p2, Point(p2.x - r*cos(stem_angle-join_angle), p2.y - r*sin(stem_angle-join_angle)));

这正是您在评论中描述的方法:

Ideally, I would like to enter a const int to be added or subtracted to the angle of the slope to create the angle of the arrowhead lines, and another for the length, and it takes care of the nitty gritty point coordinates itself.

除了 double 在以弧度存储 join_angle 方面比 int 好得多。

关于c++ - 创建箭头类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241130/

相关文章:

c++ - Boost.Spirit、Nabialek 技巧和错误处理

c++ - 我怎样才能告诉 clang-tidy 检查 pragma 一次而不是 llvm 风格的标题守卫?

c++ - 对由指针组成的结构数组进行排序时出现段错误

c++ - 获取缺陷对象的坐标值

c++ - 工作目录 - 解决方案

c++ - 使用 SDL 消失的矩形

c++ - 程序在 CIN 输入处崩溃 | C++

c++ - 派生到基类转换

php-src - ZEND_TSRMLS_CACHE_UPDATE - 它是什么以及何时需要?

c++ - 这是在 Windows 中将大型 DIB 绘制到屏幕上的最快方法