1
Graphics / Re: sf::VertexArray: Help Generating Verticies of This Object
« on: August 11, 2014, 01:27:20 am »
You want to describe the outer rim of a circle?
x = radius * cos(angle)
y = radius * sin(angle)
Where angle is in radians.
So:
x = radius * cos(angle)
y = radius * sin(angle)
Where angle is in radians.
So:
#include <SFML/Graphics.hpp>
const float DEGTORAD = 0.0174532925199432957f;
int main()
{
const int SWIDTH = 1024;
const int SHEIGHT = 768;
std::vector<sf::Vertex> vertices(32);
float radius = 100;
float TOTALANGLE = 180.f;
vertices[0]= sf::Vertex({0,0});
vertices[0].color = sf::Color::White;
for (int i = 0; i < 31; i+=2)
{
float angle = i / 30.f * TOTALANGLE * DEGTORAD;
vertices[i+1] = sf::Vertex( {radius * cos(angle), radius * sin(angle)} );
vertices[i+1].color = sf::Color::White;
}
for(auto &a: vertices)
{
a.position += sf::Vector2f(100,100);
}
sf::RenderWindow win(sf::VideoMode(SWIDTH,SHEIGHT),"Cossin",sf::Style::Close);
while(win.isOpen())
{
sf::Event e;
while (win.pollEvent(e))
{
if(e.type == sf::Event::Closed)
{
win.close();
}
}
win.clear();
//drawing
win.draw(&vertices[0],vertices.size(),sf::PrimitiveType::Lines);
win.display();
}
return 0;
}
NOTE: This describes a 180 degree half-circle in 32 x,y points starting at 0,0 and stretching out to 100,100. What you do with those is up to you.
const float DEGTORAD = 0.0174532925199432957f;
int main()
{
const int SWIDTH = 1024;
const int SHEIGHT = 768;
std::vector<sf::Vertex> vertices(32);
float radius = 100;
float TOTALANGLE = 180.f;
vertices[0]= sf::Vertex({0,0});
vertices[0].color = sf::Color::White;
for (int i = 0; i < 31; i+=2)
{
float angle = i / 30.f * TOTALANGLE * DEGTORAD;
vertices[i+1] = sf::Vertex( {radius * cos(angle), radius * sin(angle)} );
vertices[i+1].color = sf::Color::White;
}
for(auto &a: vertices)
{
a.position += sf::Vector2f(100,100);
}
sf::RenderWindow win(sf::VideoMode(SWIDTH,SHEIGHT),"Cossin",sf::Style::Close);
while(win.isOpen())
{
sf::Event e;
while (win.pollEvent(e))
{
if(e.type == sf::Event::Closed)
{
win.close();
}
}
win.clear();
//drawing
win.draw(&vertices[0],vertices.size(),sf::PrimitiveType::Lines);
win.display();
}
return 0;
}