Skip to content
doubtly

1 answer

Best answer

Answer: Draw the Sun as a filled circle at the screen centre, draw 8 elliptical orbits with ellipse(), place a filled planet on each orbit at \((x_c + a\cos\theta,\ y_c - b\sin\theta)\), and in a loop increase each planet's angle \(\theta\) (inner planets faster), clear the screen and redraw until a key is pressed.

Logic

  • Centre: midx = getmaxx()/2, midy = getmaxy()/2.
  • Orbit i has semi-axes xrad[i], yrad[i]; a planet at angle \(\theta\) lies at \(x = midx + xrad\cos\theta\), \(y = midy - yrad\sin\theta\).
  • Each frame: cleardevice(), draw Sun, orbits and planets, add speed[i] to angle[i], delay().
  • 8 planets are drawn (Mercury to Neptune). Pluto has been classed as a dwarf planet since 2006, so it is not drawn.

Program (Turbo C)

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

#define PLANETS 8

int main()
{
    int gd = DETECT, gm, err, i, midx, midy, px, py;
    int xrad[PLANETS], yrad[PLANETS], angle[PLANETS], speed[PLANETS], size[PLANETS];
    int color[PLANETS] = { LIGHTGRAY, YELLOW, BLUE, RED, BROWN, LIGHTRED, CYAN, LIGHTBLUE };
    float rad;

    initgraph(&gd, &gm, "C:\\TC\\BGI");
    err = graphresult();
    if (err != grOk) {
        printf("Graphics error: %s\n", grapherrormsg(err));
        getch();
        return 1;
    }

    midx = getmaxx() / 2;
    midy = getmaxy() / 2;

    for (i = 0; i < PLANETS; i++) {
        xrad[i] = 50 + i * 33;         /* orbit sizes: inner to outer */
        yrad[i] = 25 + i * 16;
        size[i] = 4 + i % 4;           /* planet radius in pixels */
        angle[i] = i * 45;             /* start at different places */
        speed[i] = 16 - i * 2;         /* inner planets move faster */
    }

    while (!kbhit()) {
        cleardevice();

        /* Sun at the centre */
        setcolor(YELLOW);
        setfillstyle(SOLID_FILL, YELLOW);
        circle(midx, midy, 20);
        floodfill(midx, midy, YELLOW);

        for (i = 0; i < PLANETS; i++) {
            /* orbit */
            setcolor(WHITE);
            ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);

            /* planet position on its elliptical orbit */
            rad = angle[i] * 3.14159 / 180;
            px = midx + (int)(xrad[i] * cos(rad));
            py = midy - (int)(yrad[i] * sin(rad));
            setcolor(color[i]);
            setfillstyle(SOLID_FILL, color[i]);
            fillellipse(px, py, size[i], size[i]);

            angle[i] = (angle[i] + speed[i]) % 360;   /* move for next frame */
        }
        delay(200);
    }

    getch();
    closegraph();
    return 0;
}

Output: a yellow Sun at the centre of the screen with 8 white elliptical orbits around it; a coloured planet sits on each orbit and the planets revolve anticlockwise, the inner ones faster, until a key is pressed. Checked with gcc using a stand-in graphics.h that logs the drawing calls; the first frame placed the Sun at (319, 239) with radius 20, the orbits from 50 × 25 up to 281 × 137, and the planets at (369,239), (377,211), (319,183), (214,188), (138,239), (167,313), (319,359) and (517,335), all inside a 640 × 480 screen.

Doubtly · 1 May 2023 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.