gpt4 book ai didi

c++ - 使用 SFML 绘制曲线会给出错误的形状

转载 作者:行者123 更新时间:2023-12-03 00:18:08 25 4
gpt4 key购买 nike

我目前正在尝试 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 用于其设计目的以外的目的?坏juju?

感谢任何帮助,

主持人

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;
}```

最佳答案

您无法使用sf::ConvexShape绘制凹形。更准确地说:

Although the name of sf::ConvexShape implies that it should only be used to represent convex shapes, its requirements are a little more relaxed. In fact, the only requirement that your shape must meet is that if you went ahead and drew lines from its center of gravity to all of its points, these lines must be drawn in the same order. You are not allowed to "jump behind a previous line". Internally, convex shapes are automatically constructed using triangle fans, so if your shape is representable by a triangle fan, you can use sf::ConvexShape. With this relaxed definition, you can draw stars using sf::ConvexShape for example.

解决这一限制的一种方法是,您可以从多个凸形状制作凹形状,但对于您的用例来说,这是不可行的。您唯一能做的就是绘制各个点(可能太多以至于看起来是连续的),但对于这种工作,SFML 并不适合。

关于c++ - 使用 SFML 绘制曲线会给出错误的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com