我正在使用SFML库和Visual-C ++创建条形图。我想设置条形图的起点,因此我尝试使用move和setOrigin函数,但不能成功。

sf::RectangleShape rec1(sf::Vector2f(stage1x*1000, stage1y*1000));
sf::RectangleShape rec2(sf::Vector2f(stage2x*1000, stage2y*1000));
sf::RectangleShape rec3(sf::Vector2f(stage3x*1000, stage3y*1000));

rec1.setOrigin(0, 0);
rec2.setOrigin(0, 0);
rec3.setOrigin(0, 0);

rec1.move(200, 700); // This moves the Rectangle over 200 pixels and down 700 pixels
rec2.move(300, 700); // This moves the Rectangle over 300 pixels and down 700 pixels
rec3.move(400, 700); // This moves the Rectangle over 400 pixels and down 700 pixels

rec1.setFillColor(sf::Color(100, 250, 50));
rec2.setFillColor(sf::Color(10, 20, 100));
rec3.setFillColor(sf::Color(500, 600, 700));

最佳答案

为此有很多不同的方法。例如,您可以将窗口的sf::View的高度设置为负值,这实际上将垂直翻转所有内容(坐标从底部到顶部)。

SFML可绘制对象上的setOrigin()仅会移动其本地原点,即确定哪个点与setPosition()̀中指定的位置对齐。默认情况下,原点设置为(0, 0),因此您的代码不会更改任何内容。

我不直接使用move(),而是直接调用setPosition()(它设置的是绝对位置而不是相对位置)。如果您希望这些条向上移动,请如上所述翻转视图,或者简单地从其Y位置减去这些条的高度。

关于c++ - 如何设置条形图的起点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42325529/

10-11 23:12
查看更多