-3
\$\begingroup\$

I made some code about solar system stimulation in C. It is working, but it looks too long.

So, Are there some ways to shorten my code?

Also this website told me your code is too full to upload this.

#include <Windows.h>
#include <stdio.h>
#include <math.h>

#define solar_size 20
#define PI 3.141592654
#define rad angle*180/PI

int angle;

double sun_x, sun_y, earth_x, earth_y;
double x, y, px, py;
double earth_speed = 0.05;
double x2, y2, x3, y3, x4, y4, x5, y5, x6, y6;

int main(void) {
    double sun;
    sun = sqrt(solar_size * 10);

    HWND hwnd = GetForegroundWindow();
    HDC hdc = GetWindowDC(hwnd);

    SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
    Rectangle(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));

    TextOut(hdc, 250, 450, L"solar system Simulation", 23);
    sun_x = 300 ;
    sun_y = 240 ;

    SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(255, 0, 0)));
    SelectObject(hdc, CreateSolidBrush(RGB(255, 0, 0)));
    Ellipse(hdc, sun_x - sun, sun_y - sun, sun_x + solar_size, sun_y + solar_size);

    while (1) {
        for (angle = 0;; angle++) {
            x = 30 * cos(angle * earth_speed * 2.5) + sun_x;
            y = 30 * sin(angle * earth_speed * 2.5) + sun_y;

            x2 = 55 * cos(angle * earth_speed * 1.5) + sun_x;
            y2 = 55 * sin(angle * earth_speed * 1.5) + sun_y;

            x3 = 85 * cos(angle * earth_speed) + sun_x;
            y3 = 85 * sin(angle * earth_speed) + sun_y;

            x4 = 110 * cos(angle * earth_speed * 0.6) + sun_x;
            y4 = 110 * sin(angle * earth_speed * 0.6) + sun_y;

            x5 = 140 * cos(angle * earth_speed * 0.4) + sun_x;
            y5 = 140 * sin(angle * earth_speed * 0.4) + sun_y;

            x6 = 180 * cos(angle * earth_speed * 0.15) + sun_x;
            y6 = 180 * sin(angle * earth_speed * 0.15) + sun_y;

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(120, 120, 120)));
            SelectObject(hdc, CreateSolidBrush(RGB(120, 120, 120)));
            Ellipse(hdc, x, y, x + 8, y + 8);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(100, 80, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(100, 80, 0)));
            Ellipse(hdc, x2, y2, x2 + 12, y2 + 12);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 50, 120)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 50, 120)));
            Ellipse(hdc, x3, y3, x3 + 12, y3 + 12);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(120, 20, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(120, 20, 0)));
            Ellipse(hdc, x4, y4, x4 + 10, y4 + 10);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(200, 80, 20)));
            SelectObject(hdc, CreateSolidBrush(RGB(200, 80, 20)));
            Ellipse(hdc, x5, y5, x5 + 17, y5 + 17);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(255, 220, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(255, 220, 0)));
            Ellipse(hdc, x6, y6, x6 + 21, y6 + 21);

            Sleep(50);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x, y, x + 8, y + 8);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x2, y2, x2 + 12, y2 + 12);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x3, y3, x3 + 12, y3 + 12);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x4, y4, x4 + 10, y4 + 10);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x5, y5, x5 + 17, y5 + 17);

            SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
            SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
            Ellipse(hdc, x6, y6, x6 + 21, y6 + 21);     
            }
        }

    }
\$\endgroup\$
4
  • \$\begingroup\$ You could factor out the code appearing in the for loops body to another function. \$\endgroup\$ May 8, 2020 at 8:49
  • 3
    \$\begingroup\$ Please title your question for what the code presented is to accomplish: Heed How do I ask a Good Question? solar system stimulation do you intend simulation? \$\endgroup\$
    – greybeard
    May 8, 2020 at 9:01
  • 2
    \$\begingroup\$ Describe what your code does to yourself. For each sentence describing the program, create a method or specific construction. Works most of the time. Same with documentation, I have had people ask me what to document. I normally reply well what does the code do? They answer and I reply, right write that down (and rename your variables to the terms you just used). \$\endgroup\$ May 8, 2020 at 9:12
  • 1
    \$\begingroup\$ The current code is less than a 10th of our character limit. \$\endgroup\$
    – Mast
    May 9, 2020 at 9:42

1 Answer 1

3
\$\begingroup\$

Haven't tested if the code works. I've simply shortened the code that was given in the question.

In the code given in the question, there are variables x,x2,..x6 and y,...y6. These variables are all being used in a similar manner and a lot of code is redundant.

For instance lines like

SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(255, 0, 0)));
SelectObject(hdc, CreateSolidBrush(RGB(255, 0, 0)));
Ellipse(hdc, sun_x - sun, sun_y - sun, sun_x + solar_size, sun_y + solar_size);

are constantly occurring in the code and differ only by some constants. So instead of using 6 variables, an array would be better. You could iterate through it and set the values one by one. Also, another array can be used to store the constant values that differ for each variable.

#include <Windows.h>
#include <stdio.h>
#include <math.h>

#define solar_size 20
#define PI 3.141592654
#define rad angle*180/PI

int angle;

double sun_x, sun_y, earth_x, earth_y;
double px, py;
double earth_speed = 0.05;
double x[6],y[6];

int main(void) {
    double sun;
    sun = sqrt(solar_size * 10);

    HWND hwnd = GetForegroundWindow();
    HDC hdc = GetWindowDC(hwnd);

    SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
    Rectangle(hdc, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));

    TextOut(hdc, 250, 450, L"solar system Simulation", 23);
    sun_x = 300 ;
    sun_y = 240 ;

    SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(255, 0, 0)));
    SelectObject(hdc, CreateSolidBrush(RGB(255, 0, 0)));
    Ellipse(hdc, sun_x - sun, sun_y - sun, sun_x + solar_size, sun_y + solar_size);



    double speedCoefficients[] = {2.5, 1.5, 1, 0.6, 0.4, 0.15};
    int trignometricCoefficients[] = {30, 55, 85, 110, 140, 180};
    int ellipseCoefficients[]={8, 12, 12, 10, 17, 21};
    int r=[120, 100, 0, 120, 200, 255];
    int g=[120, 80, 50, 20, 80, 220];
    int b=[120, 0, 120, 0, 20, 0];

    while (1) {
        for (angle = 0;; angle++) {
            for(int i=0;i<6;i++)
              {
                x[i] = trignometricCoefficients[i] * cos(angle * earth_speed * speedCoefficients[i]) + sun_x;
                y[i] = trignometricCoefficients[i] * sin(angle * earth_speed * speedCoefficients[i]) + sun_y;

                SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(r[i], g[i], b[i])));
                SelectObject(hdc, CreateSolidBrush(RGB(r[i], g[i], b[i])));
                Ellipse(hdc, x[i], y[i], x[i] + ellipseCoefficients[i], y[i] + ellipseCoefficients[i]);
              }

            Sleep(50);

            for(int i=0;i<6;i++)
              {
                SelectObject(hdc, CreatePen(PS_SOLID, 3, RGB(0, 0, 0)));
                SelectObject(hdc, CreateSolidBrush(RGB(0, 0, 0)));
                Ellipse(hdc, x[i], y[i], x[i] + ellipseCoefficients[i], y[i] + ellipseCoefficients[i]);
              }

            }
        }

    }
\$\endgroup\$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.