在线生成点
在手动方面有点,但我可以建议一种方法。
让我们想象一个圆,与确定的角度上的线相交。在那条线上,我们随机放置一些点。
类似这样的:
半径 100、角度 45º(PI / 4 弧度)和 100 个随机点的圆
然后,无论每个点在哪里,我们都将绘制我们的纹理精灵,并带有一些 alpha(透明度)。结果将如下所示:
改变圆的半径,点数,结果可能会有所不同
进一步说明
在我的示例中,我使用了一个类,它代表BlurredSprite。它将保存这些点,以及要绘制的sf::Texture。
class BlurredSprite : public sf::Drawable, sf::Transformable {
public:
BlurredSprite(const sf::Texture &t, float radius, float angle, unsigned int points = 30) :
m_texture(t)
{
auto getPointOverLine = [=]{
auto angleOverX = angle - std::_Pi*0.5; // Angle 0 is a vertical line, so I rotate it 45 degrees clockwise (substract)
if (rand() % 2){
angleOverX += std::_Pi; // Half of the points will be on the "first quadrant", the other half over the "third", if you consider (0,0) the center of the circle
}
auto l = radius * ((rand() * 1.f) / RAND_MAX);
auto x = cos(angleOverX) * (l);
auto y = sin(angleOverX) * (l);
return sf::Vector2f(x, y);
};
while (m_points.size() < points){
m_points.push_back(getPointOverLine());
}
}
private:
std::vector<sf::Vector2f> m_points;
sf::Texture m_texture;
};
使用getPointOverLine lambda,我在线上创建了一个随机点,但可以使用其他选项来执行此操作。事实上,这些点的分布方式会影响最终结果。
我需要重写draw 方法,以使这个类在窗口上可绘制。我还覆盖了setPosition 方法(来自sf::Transformable),因为如果你移动它,你应该移动它的所有点。
public:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const{
auto it = m_points.begin();
while (it != m_points.end()){
sf::Sprite sp(m_texture);
auto col = sp.getColor();
auto rect = sp.getTextureRect();
auto orig = sp.getOrigin() + sf::Vector2f(rect.width, rect.height)*0.5f;
col.a = 25;
sp.setColor(col);
sp.setOrigin(orig);
sp.setPosition(*it);
target.draw(sp);
it++;
}
}
virtual void setPosition(sf::Vector2f pos){
sf::Transformable::setPosition(pos);
for (int i = 0; i < m_points.size(); ++i){
m_points[i] = pos + m_points[i];
}
}