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);
}
}
}
for
loops body to another function. \$\endgroup\$solar system stimulation
do you intend simulation? \$\endgroup\$