使用SFML绘制曲线会产生错误的形状

时间:2019-12-26 09:23:43

标签: c++ sfml curve

我目前正在尝试SFML。我想使用数学曲线绘制复杂的形状。绘图仪中的方程式(我使用https://www.desmos.com/calculator)很好。绘图仪中的点列表(我使用http://www.shodor.org/interactivate/activities/SimplePlot/)很好。 SFML的结果显示出不必要的伪像,并且填充不正确(请参见https://i50.servimg.com/u/f50/19/87/95/25/sfml11.pnghttps://i50.servimg.com/u/f50/19/87/95/25/sfml12.png)。

我不知道为什么?是代码吗?显卡容量?我是否将SFML用于并非旨在用于SFML的目的?坏枣吗?

任何帮助表示赞赏,

MC

g++ -std=c++11 ./k.cpp -o ./k -Wfatal-errors -lsfml-graphics -lsfml-window -lsfml-system

#include <iostream>
#include <random>
#include <string>
#include <math.h>
#include "SFML/Graphics.hpp"

using namespace std;

struct point
    {
    double x;
    double y;
    };

struct ellipse
    {
    int index;
    point centerPoint;
    double radiusX;
    double radiusY;

    sf::ConvexShape SFMLShape;

    double xOffset;
    double yOffset;

    sf::Text ellipseTitle;
    point textPosition;
    };

ellipse computeCurve(point centerPoint,double radiusX,double radiusY,double beta)
    {
    // Based on https://www.mathcurve.com/courbes2d.gb/croixdemalte/croixdemalte.shtml
    // This is a four parameters variant. 

    int numberOfPoints=400;

    ellipse aCurve;
    aCurve.SFMLShape.setPointCount(numberOfPoints);
    aCurve.centerPoint.x=centerPoint.x;
    aCurve.centerPoint.y=centerPoint.y;
    aCurve.radiusX=radiusX;
    aCurve.radiusY=radiusY;
    double alpha=2*M_PI/numberOfPoints;

    std::random_device randomDevice;
    std::mt19937 seed(randomDevice());

    float l=-2;
    float p=-0.6;
    float n=0.5;
    float k=-0.5;

    point point;

    for(unsigned short i=0;i<=numberOfPoints/2;i++)
        {
        point.x=radiusX*cos(l*alpha*i)*(p+cos(n*alpha*i)/2 -k)+centerPoint.x; 
        point.y=radiusX*sin(alpha*i)*(p+cos(n*alpha*i)/2)+centerPoint.y;
        aCurve.SFMLShape.setPoint(i,sf::Vector2f(point.x,point.y));
cout << "computeFish 1 point n°" << i << " " << point.x << " " << point.y << endl;
        };

    for(unsigned short i=0;i<=numberOfPoints/2;i++)
        {
        point.x=radiusX*cos(l*alpha*i)*(p+cos(n*alpha*i)/2 -k)+centerPoint.x; 
        point.y=-radiusX*sin(alpha*i)*(p+cos(n*alpha*i)/2)+centerPoint.y;
        aCurve.SFMLShape.setPoint(numberOfPoints-i,sf::Vector2f(point.x,point.y));
cout << "computeFish 2 point n°" << numberOfPoints-i << " " << point.x << " " << point.y << endl;
        };

    aCurve.SFMLShape.setOrigin(aCurve.centerPoint.x,aCurve.centerPoint.y);
    aCurve.SFMLShape.setPosition(aCurve.centerPoint.x,aCurve.centerPoint.y);

    aCurve.textPosition.x=aCurve.centerPoint.x;
    aCurve.textPosition.y=aCurve.centerPoint.y;

    return aCurve;
    }

int main()
    {

    const   unsigned short windowWidth = 800;
    const   unsigned short windowHeight = 800;
    sf::ContextSettings settings;
    settings.antialiasingLevel = 4;
    sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Demo",sf::Style::Default); // Default / None // Fullscreen

    sf::Event myEvent;
    sf::Clock ellipseClock;

    bool stopped=false;
    point centerPoint;
    centerPoint.x=300;
    centerPoint.y=300;
    double radiusX=200;
    double radiusY=150;
    double beta=0;
    ellipse mt=computeCurve(centerPoint,radiusX,radiusY,beta);
    mt.SFMLShape.setOutlineColor(sf::Color::Red);
    mt.SFMLShape.setFillColor(sf::Color(40,40,40,127));
    mt.SFMLShape.setOutlineThickness(1.f);
    while (window.isOpen())
        {
        while (window.pollEvent(myEvent))
            {
            if (myEvent.type == sf::Event::EventType::Closed)
                {
                window.close();
                }
            }
        window.clear();
        if (ellipseClock.getElapsedTime().asMilliseconds() > 100.0f)
            {
            // 
            ellipseClock.restart();
            }
        window.draw(mt.SFMLShape);
        window.display();
        }
    return EXIT_SUCCESS;
    }```

1 个答案:

答案 0 :(得分:0)

您不能使用sf::ConvexShape绘制凹形。更精确地:

  

尽管sf::ConvexShape的名称意味着它仅应用于表示凸形,但其要求稍微宽松一些。实际上,形状必须满足的唯一要求是,如果您继续前进并从重心到其所有点绘制线,则这些线必须以相同的顺序绘制。您不可以“跳到上一行”。在内部,凸形状是使用三角扇自动构造的,因此,如果您的形状可以用三角扇表示,则可以使用sf::ConvexShape。通过这种宽松的定义,您可以使用sf::ConvexShape绘制星星。

解决此问题的方法是,可以用多个凸形制作凹形,但是对于您的用例来说,这是不可行的。您唯一可以做的就是绘制各个点(可能是如此之多,以至于看上去似乎是连续的),但是对于这种工作,SFML并不适合。