C Graphics - 如何以特定角度移动对象

C Graphics - How to move an object with specific angle

我正在开发 C 图形程序,我会向最终用户询问投影角度,然后使用该角度从地球(圆形)表面发射火箭。

但是我做不到。

这是我在 google 上找到的:

x1 = x + cos(angle) * distance;
y1 = y + sin(angle) * distance;

其中 x1 y1 是对象的新像素位置。

我试过了,但似乎不起作用。另外我希望火箭不断移动直到屏幕结束,但是上面的代码会直接将对象从位置 A 打印到位置 B。

完整的程序代码

#include        <stdio.h>
#include        <conio.h>
#include     <graphics.h>
#include          <dos.h>
#include         <math.h>
#include       <stdlib.h>
#include     <iostream.h>

#define cld cleardevice()

int _moonRadius = 20, _earthRadius = 40, _marsRadius = 25;

void mars () {
    setfillstyle(9, BROWN);
    setcolor(BROWN);
    circle(getmaxx() - 25, 50, _marsRadius);
    floodfill(getmaxx() - 27, 52, BROWN);
}

void moon () {
    setfillstyle(9, WHITE);
    setcolor(WHITE);
    circle(getmaxx()/2, getmaxy()/2, _moonRadius);
    floodfill(getmaxx()/2, getmaxy()/2, WHITE);

    // Moon's gravitational area
    setfillstyle(SOLID_FILL, DARKGRAY);
    setcolor(DARKGRAY);
    circle(getmaxx()/2, getmaxy()/2, _moonRadius * 5);
}

void earth () {
    setfillstyle(9, GREEN);
    setcolor(GREEN);
    circle(40, getmaxy() - 100, _earthRadius);
    floodfill(42, getmaxy() - 102, GREEN);
}

void rocket (int x, int y) {
    setcolor(WHITE);
    rectangle(x, y - 105, x + 70, y - 95);
}

void rocket_clear (int x, int y) {
    setcolor(BLACK);
    rectangle(x, y - 105, x + 70, y - 95);
}

void main () {
    clrscr();
    int angle, speed;

    printf("Please provide input parameters.");
    printf("Enter projection angle (range from 5 to 90)\n");
    scanf("%d", &angle);

    printf("Enter projection speed (range from 10 to 100)\n");
    scanf("%d", &speed);

    int gd=DETECT, gm, i, j, k;
    initgraph(&gd, &gm, "C:\TURBOC3\BGI");

    // Planets and rocket
    mars();
    moon();
    earth();
    rocket(80, 550); // let say initial pixel position x = 80, y = 550
    
    // Moving the rocket
    // Right now its only moving towards horizontal line, with speed implementation
    // Now here I want to implement the angle of projection
    for (i = 81; i < getmaxx() + 100; i++) {
        // Also I am not sure about this loop's final range, should it go to getmaxx() or some other range
        rocket(i, 550);
        rocket_clear(i - 1, 550); // 550 is hard coded right now, so rocket will move only horizontally
        delay(500 / speed);
    }
    getch();
}

需要你们的帮助。

(供参考:你也可以想象子弹从杀手位置移动到人的位置有一定角度)

谢谢:)

确保您通过 radians 中的角度。从度数转换:radians=degrees*PI/180(PI 在 math.h 中定义,应包含在 graphics.h 中)确保您的变量是 doubles.

接下来您可能希望将 X/Y 坐标捆绑在一个结构中,这样您就可以 return 从函数中获取新位置:

typedef struct {
    double x;
    double y
} coord_t;

coord_t new_pos(double x, double y, double distance, double angle_deg) {
    coord_t result;
    double angle_rad = angle_deg * PI / 180;
    result.x = x + cos(angle_rad) * distance;
    result.y = y + sin(angle_rad) * distance;
    return result;
}

或者您可以以弧度为单位处理所有角度,这样函数就不必进行额外的计算。

请阅读//=====

开头的评论
#include        <stdio.h>
#include        <conio.h>
#include     <graphics.h>
#include          <dos.h>
#include         <math.h>
#include       <stdlib.h>
#include     <iostream.h>

#define cld cleardevice()

//===== making these values as constants
static const int _moonRadius = 20, _earthRadius = 40, _marsRadius = 25;
static double projection_angle = 0.0;

void mars () {
    setfillstyle(9, BROWN);
    setcolor(BROWN);
    circle(getmaxx() - _marsRadius, 50, _marsRadius);
    floodfill(getmaxx() - 27, 52, BROWN);
}

void moon () {
    setfillstyle(9, WHITE);
    setcolor(WHITE);
    circle(getmaxx()/2, getmaxy()/2, _moonRadius);
    floodfill(getmaxx()/2, getmaxy()/2, WHITE);

    // Moon's gravitational area
    setfillstyle(SOLID_FILL, DARKGRAY);
    setcolor(DARKGRAY);
    circle(getmaxx()/2, getmaxy()/2, _moonRadius * 5);
}

void earth () {
    setfillstyle(9, GREEN);
    setcolor(GREEN);
    circle(40, getmaxy() - 100, _earthRadius);
    floodfill(42, getmaxy() - 102, GREEN);
}

void rocket (int x, int y) {
    setcolor(WHITE);
    //===== a box of size 10x10
    rectangle(x, y, x + 10, y - 10);
}

void rocket_clear (int x, int y) {
    setcolor(BLACK);
    //===== a box of size 10x10
    rectangle(x, y, x + 10, y - 10);
}

void main () {
    clrscr();
    int angle, speed;

    printf("Please provide input parameters.");
    printf("Enter projection angle (range from 5 to 90)\n");
    scanf("%d", &angle);
    //===== angle validation
    if (angle < 5 || angle > 90)
    {
        printf("Please provide angle in range [5, 90]\n");
        getch();
        return;
    }
    //===== calculate angle in radians
    projection_angle = (angle * 3.14) / 180.0;
    printf("projection_angle = %d\n", projection_angle);

    printf("Enter projection speed (range from 10 to 100)\n");
    scanf("%d", &speed);
    //===== speed validation
    if (speed < 10 || speed > 100)
    {
        printf("Please provide speed in range [10, 100]\n");
        getch();
        return;
    }

    int gd=DETECT, gm, i, j, k;
    initgraph(&gd, &gm, "C:\TURBOC3\BGI");

    // Planets and rocket
    mars();
    moon();
    earth();
    rocket(80, 550); // let say initial pixel position x = 80, y = 550

    // Moving the rocket
    // Right now its only moving towards horizontal line, with speed implementation
    // Now here I want to implement the angle of projection

    //===== to store prev position
    int prev_i = 0, prev_j = 0;
    //===== increments will be constant for a given angle and speed
    const int x_inc = cos(projection_angle) * speed;
    const int y_inc = sin(projection_angle) * speed;

    //===== i and j will be updated with their respective increments
    for (i = 90, j = getmaxy() - 100; i < getmaxx() + 100 && j >= -10; i += x_inc, j -= y_inc) {
        // Also I am not sure about this loop's final range, should it go to getmaxx() or some other range

        //===== clear the previous position
        rocket_clear(prev_i, prev_j); // 550 is hard coded right now, so rocket will move only horizontally
        //===== draw rocket at current position
        rocket(i, j);
        //===== make current position as previous position
        prev_i = i;
        prev_j = j;
        //printf("x_inc = %lf, y_inc = %lf\n", cos(projection_angle) * speed, sin(projection_angle) * speed);
        delay(500 / speed);
    }
    getch();
}

注意:您可以用实际的 Pi 替换 3.14。参考 this.