Advertisement
qberik

Untitled

Mar 23rd, 2022
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. using namespace sf;
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     constexpr int pointCount = 200;
  13.     const Vector2f ellipseRadius = { 200.f, 80.f };
  14.  
  15.     ContextSettings settings;
  16.     settings.antialiasingLevel = 8;
  17.     RenderWindow window(VideoMode({ 800,600 }), "Ellipse", Style::Default, settings);
  18.  
  19.     ConvexShape ellipse;
  20.     ellipse.setPosition({ 400,320 });
  21.     ellipse.setFillColor(Color::Blue);
  22.  
  23.     ellipse.setPointCount(pointCount);
  24.     for (int pointNo = 0; pointNo < pointCount; pointNo++) {
  25.         float angle = float(2 * M_PI * pointNo) / float(pointCount);
  26.         Vector2f point = {
  27.             ellipseRadius.x * sin(angle),
  28.             ellipseRadius.y * cos(angle)
  29.         };
  30.         ellipse.setPoint(pointNo, point);
  31.     }
  32.  
  33.   vector< vector< vector<Vertex> > > lines;
  34.  
  35.   int lineCount = 5;
  36.   int segmentCount = 200;
  37.  
  38.   int _x = 400;
  39.   int _y = 320;
  40.  
  41.   for( int i = 0; i < lineCount; i++ ){
  42.     vector< vector<Vertex> > line;
  43.     for( int j = 0; j < segmentCount - 1; j++ ){
  44.           float a1 = float(2 * M_PI * j ) / float( segmentCount );
  45.           float a2 = float(2 * M_PI * (j + 1) ) / float( segmentCount );
  46.       float x = ellipseRadius.x * ( float(i) / lineCount  );
  47.       float y = ellipseRadius.y;
  48.       vector<Vertex> lineSegment{
  49.         sf::Vertex(sf::Vector2f( _x + x * sin(a1), _y + y * cos(a1))),
  50.         sf::Vertex(sf::Vector2f( _x + x * sin(a2), _y + y * cos(a2))),
  51.       };
  52.       line.push_back( lineSegment );
  53.     }
  54.     lines.push_back( line );
  55.   }
  56.  
  57.   /*
  58.     double circleRadius = sqrt(pow(ellipseRadius.x, 2) + pow(ellipseRadius.y, 2));
  59.     double angle = atan(ellipseRadius.y / ellipseRadius.x);
  60.     double step = -angle * 2 / 10;
  61.  
  62.     VertexArray arrays[10];
  63.     VertexArray base = VertexArray(TrianglesStrip, 22);
  64.  
  65.     for (int i = 0; i < 10; i++) {
  66.         arrays[i] = VertexArray(TrianglesStrip, 22);
  67.     }
  68.  
  69.   double t = 3;
  70.     for (int i = 0; i <= 10; i++) {
  71.         double currentAngle = angle + step * i;
  72.         base[2 * i] = Vector2f( circleRadius * cos(currentAngle) - 200, circleRadius * sin(currentAngle) + 320);
  73.         base[2 * i].color = Color::Red;
  74.         base[2 * i + 1] = Vector2f( (circleRadius + t) * cos(currentAngle) - 200, (circleRadius + t) * sin(currentAngle) + 320);
  75.         base[2 * i + 1].color = Color::Red;
  76.     }
  77.  
  78.   double f = 3;
  79.     for (int i = 0; i < 10; i++) {
  80.         for (int j = 0; j < 22; j++) {
  81.             arrays[i][j] = Vector2f( ( ( base[j].position.x ) * ( ( i - 5 + 1 ) * f  ) ) + 400 , ( base[j].position.y));
  82.             if (j % 2 == 0)
  83.             {
  84.                 arrays[0][j].position.x *= (1 - (2 * i + 1) / 100);
  85.             }
  86.         }
  87.     }
  88.  
  89.   */
  90.  
  91.     while (window.isOpen())
  92.     {
  93.         Event event;
  94.         while (window.pollEvent(event))
  95.         {
  96.             if (event.type == Event::Closed)
  97.             {
  98.                 window.close();
  99.             }
  100.         }
  101.  
  102.         window.clear();
  103.         window.draw(ellipse);
  104.     for( int i = 0; i < lineCount; i ++ ){
  105.       for( int j = 0; j < segmentCount - 1; j++ ){
  106.         Vertex line[2] = {
  107.           lines[i][j][0],
  108.           lines[i][j][1]
  109.         };
  110.         //std::cout << line[0] << line[1] << std::endl;
  111.         window.draw( line, 3, sf::Lines );  
  112.       }
  113.     }
  114.     /*
  115.     for (int i = 0; i < 10; i++) {
  116.             window.draw(arrays[i]);
  117.     }
  118.     window.draw( base );
  119.     */
  120.         window.display();
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement