如何从一个函数返回两个浮点数然后将它们放入另一个函数

时间:2020-03-12 09:28:20

标签: c variables

我是C语言的新手,目前正在从事一个项目。但是,我需要从我的函数中返回一个名为rocket_path的变量,然后将其放入名为Collision的函数中。这样我就可以检测到火箭在月球上的碰撞。

#include <graphics_lib.h>
#include <stdio.h>
#include <math.h>
 //#include <midi_lib.h>

void Planets_Rocket(void);
float rocket_path(float mars_Distance , float moon_Distance);
void background_music();
void Collision();

int main(void)
{
    Planets_Rocket();
    rocket_path();
    background_music();
    GFX_UpdateDisplay();
    getchar();
    return 0;
}

//Drawing of rocket Path
float rocket_path()
{

    float angle_Initial;
    float velocity_Initial;
    float time;
    int pos_X_Initial;
    int pos_Y_Initial;
    float pos_x;
    float pos_y;
    int i;
    float moon_Distance_x;
    float moon_Distance_y;
    float moon_Distance;
    float mars_Distance_x;
    float mars_Distance_y;
    float mars_Distance;
    float acceleration_x;
    float acceleration_y;
    float displacement_x;
    float displacement_y;
    float angle_Rocket_moon;
    float moon_influence;
    float velocity_x;
    float velocity_y;

    pos_X_Initial = 160;
    pos_Y_Initial = 1100;

    printf("Please enter angle of launch: \n");
    scanf("%f", &angle_Initial);

    printf("Please enter your launch velocity: \n");
    scanf("%f", &velocity_Initial);

    velocity_x = velocity_Initial * ( cos ((angle_Initial) * (M_PI / 180)) );
    velocity_y = velocity_Initial * ( sin ((angle_Initial) * (M_PI / 180)) );

    pos_x = pos_X_Initial;
    pos_y = pos_Y_Initial;
    time = 0;

    GFX_MoveTo(160 , 1100);

    for(i = 0 ; i < 500 ; i++)
    {
        GFX_SetColour(WHITE);

        mars_Distance_x = (900 - pos_x);
        mars_Distance_y = (50 - pos_y);
        mars_Distance = sqrt((mars_Distance_x * mars_Distance_x) + (mars_Distance_y * mars_Distance_y));

        moon_Distance_x = (600 - pos_x);
        moon_Distance_y = (700 - pos_y);
        moon_Distance = sqrt((moon_Distance_x * moon_Distance_x) + (moon_Distance_y * moon_Distance_y));

        if(moon_Distance <=125)
        {
            angle_Rocket_moon = atan((moon_Distance_x) / (moon_Distance_y));
            moon_influence = (1.62 / (moon_Distance * moon_Distance)) * 5000;

            if (700 - pos_y >= 0)
            {
                acceleration_x = (moon_influence * sin(angle_Rocket_moon)); // TOP
                acceleration_y = (moon_influence * cos(angle_Rocket_moon));
            }
            else
            {
                acceleration_x = (moon_influence * cos(angle_Rocket_moon)); // BTM
                acceleration_y = (moon_influence * sin(angle_Rocket_moon));
            }


            if (700 - pos_y >= 0){
                acceleration_y = -acceleration_y;
            }


            if (600 - pos_x <= 0){
                acceleration_x = -acceleration_x;
            }

            velocity_x = velocity_x + (acceleration_x * time);
            velocity_y = velocity_y + (acceleration_y * time);

            displacement_x = (velocity_x * time) - (0.5 * acceleration_x * time * time);
            displacement_y = (velocity_y * time) - (0.5 * acceleration_y * time * time);

            pos_x += displacement_x;
            pos_y -= displacement_y;
        }
        else{
            pos_x += velocity_x * time;
            pos_y -= velocity_y * time;
        }

        time += 0.01;
        printf("%f" , acceleration_x);
        GFX_DrawLineTo(pos_x, pos_y, 3);

        return mars_Distance;
        return moon_Distance;
    }
    getchar();
}

float Collision(float mars_Distance , float moon_Distance)
{
    if(moon_Distance <=25)
    {
        printf("Mission Failed better luck next time");
        GFX_CloseWindow();
    }
    if(mars_Distance <=75)
    {
        printf("Well Done");
        getchar();
        GFX_CloseWindow();
    }
}

//Drawing of the planets
void Planets_Rocket()
{
    //Opening a graphics window
    GFX_InitWindow(1200, 1200);
    //Drawing the planet Earth
    GFX_DrawFilledCircle(0, 1100, 80, 2);
    //Drawing moon and the moon's area of influence
    GFX_DrawFilledCircle(600, 700, 125, 8);
    GFX_DrawFilledCircle(600, 700, 25, 7);
    //Drawing of the planet Mars
    GFX_DrawFilledCircle(900, 50, 75, 4);
    //Drawing of the rocket using a rectangle and 3 triangles
    GFX_DrawFilledRectangle(80, 1090, 130, 1110, 15);
    GFX_DrawFilledTriangle(130, 1090, 130, 1110, 160, 1100, 4);
    GFX_DrawFilledTriangle(82, 1090, 88, 1090, 82, 1070, 15);
    GFX_DrawFilledTriangle(82, 1110, 88, 1110, 82, 1130, 15);
    GFX_UpdateDisplay();
}

void background_music()
{
   /* int k;

    for(k = 0 ; k < 500 ; k++){
    MIDI_Init();
    MIDI_SendNote(int 116, int 1, int 10);
    MIDI_SendNote(int 128, int 1, int 10);

    }
  */
}

0 个答案:

没有答案