qberik

эллипс

Apr 3rd, 2022 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 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.setOutlineThickness(1);
  24.     //ellipse.setOutlineColor(Color::White);
  25.  
  26.     ellipse.setPointCount(pointCount);
  27.     for (int pointNo = 0; pointNo < pointCount; pointNo++) {
  28.         float angle = float(2 * M_PI * pointNo) / float(pointCount);
  29.         Vector2f point = {
  30.             ellipseRadius.x * sin(angle),
  31.             ellipseRadius.y * cos(angle)
  32.         };
  33.         ellipse.setPoint(pointNo, point);
  34.     }
  35.  
  36.     vector< vector< vector<Vertex> > > lines;
  37.  
  38.     int lineCount = 5;
  39.     int segmentCount = 200;
  40.  
  41.     int _x = 400;
  42.     int _y = 320;
  43.  
  44.  
  45.   vector< ConvexShape > small_ellipse;
  46.  
  47.  
  48.   for( int i = 0; i < lineCount; i++ ){
  49.     ConvexShape RightHalf;
  50.     RightHalf.setPosition( _x - 0.5, _y );
  51.     ConvexShape LeftHalf;
  52.     LeftHalf.setPosition( _x + 0.5 , _y );
  53.  
  54.  
  55.     vector< Color > col = { Color::Red, Color::Green, Color::Magenta };
  56.     RightHalf.setFillColor( col[ (i ) % col.size() ]  );
  57.     LeftHalf.setFillColor( col[ (i + 1 ) % col.size() ]  );
  58.  
  59.     RightHalf.setPointCount( segmentCount );
  60.     for( int j = 0; j < segmentCount; j++ ){
  61.       float angle = float( M_PI * j ) / float( segmentCount );
  62.       Vector2f point = {
  63.         ellipseRadius.x * sin(angle) * ( float( i + 1 ) / lineCount ) ,
  64.               ellipseRadius.y * cos(angle)
  65.       };
  66.       RightHalf.setPoint( j, point );
  67.     }
  68.  
  69.     LeftHalf.setPointCount( segmentCount );
  70.     for( int j = 0; j < segmentCount; j++ ){
  71.       float angle = M_PI + float( M_PI * j ) / float( segmentCount );
  72.       Vector2f point = {
  73.         ellipseRadius.x * sin(angle) * ( float( i + 1 ) / lineCount ) ,
  74.               ellipseRadius.y * cos(angle)
  75.       };
  76.       LeftHalf.setPoint( j, point );
  77.     }
  78.  
  79.     small_ellipse.push_back( RightHalf );
  80.     small_ellipse.push_back( LeftHalf );
  81.    
  82.   }
  83.  
  84.    
  85.  
  86.     for (int i = 0; i < lineCount; i++) {
  87.         vector< vector<Vertex> > line;
  88.         for (int j = 0; j < segmentCount - 1; j++) {
  89.             float a1 = float(2 * M_PI * j) / float(segmentCount);
  90.             float a2 = float(2 * M_PI * (j + 1)) / float(segmentCount);
  91.             float x = ellipseRadius.x * (float(i+1)  / lineCount);
  92.             float y = ellipseRadius.y;
  93.             vector<Vertex> lineSegment{
  94.               Vertex(Vector2f( x * sin(a1),  y * cos(a1))),
  95.               Vertex(Vector2f( x * sin(a2),  y * cos(a2))),
  96.             };
  97.             line.push_back(lineSegment);
  98.         }
  99.         lines.push_back(line);
  100.     }
  101.  
  102.     window.setFramerateLimit(60);
  103.     int t = 0;
  104.  
  105.     while (window.isOpen())
  106.     {
  107.         double speed = 1.0 / 10;
  108.         double angle = ( ( t * speed ));
  109.         double scale_x = 1 +  0.5 * cos( angle );
  110.         double scale_y = 1 +  0.5 * sin( angle );
  111.         t += 1;
  112.  
  113.         cout << scale_x << ' ' << scale_y << ' ' << t << endl;
  114.         Event event;
  115.         while (window.pollEvent(event))
  116.         {
  117.             if (event.type == Event::Closed)
  118.             {
  119.                 window.close();
  120.             }
  121.         }
  122.  
  123.         window.clear();
  124.  
  125.         ellipse.setScale(scale_x,scale_y);
  126.         window.draw( ellipse );
  127.  
  128.         for( int i = 0; i < small_ellipse.size() ; i++ ){
  129.           small_ellipse[ small_ellipse.size() - 1 - i].setScale(scale_x,scale_y);
  130.  
  131.           window.draw( small_ellipse[ small_ellipse.size() - 1 - i] );
  132.         }
  133.  
  134.  
  135.         for (int i = 0; i < lineCount; i++) {
  136.             for (int j = 0; j < segmentCount - 1; j++) {
  137.                 Vertex first = lines[i][j][0];
  138.                 Vertex second = lines[i][j][1];
  139.                 first.position.x = first.position.x * scale_x + _x;
  140.                 first.position.y = first.position.y * scale_y + _y;
  141.  
  142.                 second.position.x = second.position.x * scale_x + _x;
  143.                 second.position.y = second.position.y * scale_y + _y;
  144.  
  145.                 Vertex line[2] = {
  146.                   first,
  147.                   second
  148.                 };
  149.                 window.draw(line, 10, Lines);
  150.             }
  151.         }
  152.  
  153.  
  154.         window.display();
  155.     }
  156. }
  157.  
Add Comment
Please, Sign In to add comment