关于#c++#的问题:制作程序动画,一个小圆球的运动轨迹是一个小圆,有两条相互垂直的相交直线,交点即为小圆球圆心,直线不要超过大圆

img


如图所示,外面有个大圆,一个小圆球的运动轨迹是一个小圆,有两条相互垂直的相交直线,交点即为小圆球圆心,圆球运动,两直线随之运动,所有的运动都在大圆内,直线不要超过大圆


#include <SFML/Graphics.hpp>
#include <cmath>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Circle Animation");
    window.setFramerateLimit(60);

    // 创建大圆
    float radius = 200.f;
    sf::CircleShape circle(radius);
    circle.setFillColor(sf::Color::Transparent);
    circle.setOutlineThickness(2.f);
    circle.setOutlineColor(sf::Color::White);
    circle.setPosition(window.getSize().x / 2.f - radius, window.getSize().y / 2.f - radius);

    // 创建两条相互垂直的相交直线
    sf::RectangleShape line1(sf::Vector2f(radius * 2.f, 2.f));
    line1.setFillColor(sf::Color::White);
    line1.setPosition(circle.getPosition().x, circle.getPosition().y + radius);

    sf::RectangleShape line2(sf::Vector2f(2.f, radius * 2.f));
    line2.setFillColor(sf::Color::White);
    line2.setPosition(circle.getPosition().x + radius, circle.getPosition().y);

    // 创建小球
    sf::CircleShape ball(10.f);
    ball.setFillColor(sf::Color::Red);

    // 计算小球轨迹的圆心位置
    sf::Vector2f center = circle.getPosition() + sf::Vector2f(radius, radius);

    // 设置小球轨迹的半径和起始角度
    float ballRadius = 50.f;
    float angle = 0.f;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }

        // 计算小球在圆上的位置
        float x = center.x + ballRadius * std::cos(angle);
        float y = center.y + ballRadius * std::sin(angle);

        // 设置小球的位置
        ball.setPosition(x, y);

        // 更新角度
        angle += 0.03f;

        // 绘制图形
        window.clear();
        window.draw(circle);
        window.draw(line1);
        window.draw(line2);
        window.draw(ball);
        window.display();
    }

    return 0;
}

参考GPT和自己的思路:您可以使用C++中的图形库(如OpenGL、SFML、SDL等)来制作程序动画并实现所需的功能。下面是一个简单的示例程序,用于演示如何在窗口中绘制一个大圆和一个小圆球,以及小圆球的运动轨迹和相交直线的运动:

#include <SFML/Graphics.hpp>

int main()
{
    // 创建窗口
    sf::RenderWindow window(sf::VideoMode(800, 600), "Animation");
    
    // 创建大圆
    sf::CircleShape bigCircle(200.f);
    bigCircle.setFillColor(sf::Color::Transparent);
    bigCircle.setOutlineColor(sf::Color::White);
    bigCircle.setOutlineThickness(2.f);
    bigCircle.setPosition(300.f, 200.f);
    
    // 创建小圆球和其运动轨迹
    sf::CircleShape smallCircle(10.f);
    smallCircle.setFillColor(sf::Color::Red);
    smallCircle.setPosition(400.f, 250.f);
    sf::VertexArray trajectory(sf::LineStrip, 101);
    trajectory[0].position = sf::Vector2f(400.f, 250.f);
    for (int i = 1; i < 101; i++)
    {
        float x = 400.f + 100.f * std::cos(i * 3.14159f / 50.f);
        float y = 250.f + 100.f * std::sin(i * 3.14159f / 50.f);
        trajectory[i].position = sf::Vector2f(x, y);
        trajectory[i].color = sf::Color::Red;
    }
    
    // 创建相交直线
    sf::VertexArray horizontalLine(sf::Lines, 2);
    horizontalLine[0].position = sf::Vector2f(300.f, 250.f);
    horizontalLine[1].position = sf::Vector2f(500.f, 250.f);
    horizontalLine[0].color = sf::Color::Green;
    horizontalLine[1].color = sf::Color::Green;
    sf::VertexArray verticalLine(sf::Lines, 2);
    verticalLine[0].position = sf::Vector2f(400.f, 150.f);
    verticalLine[1].position = sf::Vector2f(400.f, 350.f);
    verticalLine[0].color = sf::Color::Green;
    verticalLine[1].color = sf::Color::Green;
    
    // 主循环
    while (window.isOpen())
    {
        // 处理事件
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        
        // 更新小圆球的位置和相交直线的位置
        float time = std::clock() / static_cast<float>(CLOCKS_PER_SEC);
        float angle = time * 2.f * 3.14159f / 5.f;
        float x = 400.f + 100.f * std::cos(angle);
        float y = 250.f + 100.f * std::sin(angle);
        smallCircle.setPosition(x, y);
        verticalLine[0].position = sf::Vector2f(x, 150.f);
        verticalLine[1].position = sf::Vector2f(x, 350.f);
        horizontalLine[0].position = sf::Vector2f(300.f, y);
// 确保小圆球和相交直线在大圆内
float distance = std::sqrt((x - 400.f) * (x - 400.f) + (y - 250.f) * (y - 250.f));
if (distance > 190.f)
{
float factor = 190.f / distance;
x = 400.f + factor * (x - 400.f);
y = 250.f + factor * (y - 250.f);
smallCircle.setPosition(x, y);
verticalLine[0].position = sf::Vector2f(x, 150.f);
verticalLine[1].position = sf::Vector2f(x, 350.f);
}
if (x < 300.f)
{
x = 300.f;
smallCircle.setPosition(x, y);
verticalLine[0].position = sf::Vector2f(x, 150.f);
verticalLine[1].position = sf::Vector2f(x, 350.f);
}
else if (x > 500.f)
{
x = 500.f;
smallCircle.setPosition(x, y);
verticalLine[0].position = sf::Vector2f(x, 150.f);
verticalLine[1].position = sf::Vector2f(x, 350.f);
}
if (y < 150.f)
{
y = 150.f;
smallCircle.setPosition(x, y);
horizontalLine[0].position = sf::Vector2f(300.f, y);
}
else if (y > 350.f)
{
y = 350.f;
smallCircle.setPosition(x, y);
horizontalLine[0].position = sf::Vector2f(300.f, y);
}
horizontalLine[1].position = sf::Vector2f(x, y);
// 清空窗口
    window.clear(sf::Color::Black);
    
    // 在窗口中绘制图形
    window.draw(bigCircle);
    window.draw(smallCircle);
    window.draw(trajectory);
    window.draw(horizontalLine);
    window.draw(verticalLine);
    
    // 显示窗口
    window.display();
}

return 0;
}


该程序使用SFML图形库绘制图形,创建了一个窗口,其中包含一个大圆、一个小圆球、小圆球的运动轨迹、两条相交直线,以及一些简单的逻辑来确保它们都在大圆内,而且相交直线不会超过大圆。在主循环中,程序通过计算当前时间来更新小圆球的位置和相交直线的位置,然后使用窗口的draw函数在窗口中绘制所有图形,并在每次循环结束时显示窗口。

答题不易,希望采纳💕
以下是一个基本的C++程序,可以制作该动画:

c++
#include <iostream>
#include <graphics.h> 

using namespace std;

const int width = 800;
const int height = 600;
const int radius = 50; // 小球半径
const int lineLength = 100; // 直线长度
const int centerX = width / 2;
const int centerY = height / 2;

int main()
{
    initwindow(width, height, "Animation");

    int x = centerX + radius, y = centerY;
    int angle = 0;
    int speed = 5; // 运动速度

    while (true)
    {
        setcolor(WHITE);
        circle(centerX, centerY, radius); // 画大圆

        int newX = centerX + lineLength * cos(angle), newY = centerY + lineLength * sin(angle);
        line(centerX, centerY, newX, newY); // 画直线1

        newX = centerX - lineLength * sin(angle), newY = centerY + lineLength * cos(angle);
        line(centerX, centerY, newX, newY); // 画直线2

        setcolor(RED);
        circle(x, y, radius); // 画小球

        angle += speed;
        if (angle >= 360) angle = 0;

        x = centerX + lineLength * cos(angle);
        y = centerY + lineLength * sin(angle);

        delay(10);
        cleardevice(); // 清除图形窗口
    }

    closegraph();
    return 0;
}

注意:以上代码使用了Graphics.h库,需要在编译器中配置相关环境。