使用SFML绘制曲线时出现错误的形状

3
我目前在尝试使用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时违反了其设计目的?或者是什么不好的魔法?

需要任何帮助,感激不尽。

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

使用sf::ConvexShape无法绘制凹多边形。更确切地说:

尽管 sf::ConvexShape 的名称暗示它只用于表示凸多边形,但其要求略微宽松。实际上,你的形状必须满足的唯一要求是:如果你从其重心向所有点绘制直线,则这些直线必须按相同的顺序绘制。你不能“跳到之前的线后面”。在内部,凸形状自动使用三角形扇构建,因此如果可以使用三角形扇表示你的形状,你可以使用 sf::ConvexShape。借助这种宽松的定义,例如,可以使用 sf::ConvexShape 绘制星星。

解决此限制的方法是将多个凸多边形组合成凹多边形,但对于您的用例不可行。您唯一能做的事情就是绘制单个点(也许是许多点,以至于它看起来连续),但对于这种工作,SFML 不适合。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接