2022-06-25 18:33:19 +08:00
|
|
|
#include <raylib.h>
|
|
|
|
#include <rdrawing.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define RAYGUI_IMPLEMENTATION
|
2022-07-03 09:19:25 +08:00
|
|
|
#include <raygui.h>
|
2022-06-25 18:33:19 +08:00
|
|
|
|
2024-05-24 16:10:17 +08:00
|
|
|
void updateRadius(float baseL, float outerL,float pointL, float *pBaseR, float *pOuterR, float *pPointR) {
|
2022-06-26 00:10:18 +08:00
|
|
|
int totalL=baseL+outerL;
|
|
|
|
if (pointL>outerL)
|
|
|
|
totalL+=pointL;
|
|
|
|
else
|
|
|
|
totalL+=outerL;
|
2023-08-12 18:25:46 +08:00
|
|
|
int totalR = 340;
|
2022-06-25 18:33:19 +08:00
|
|
|
int remainder = totalR % totalL;
|
|
|
|
if (remainder!=0) {
|
|
|
|
if (remainder < totalL / 2) {
|
|
|
|
totalR -= remainder;
|
|
|
|
} else {
|
|
|
|
totalR += ( totalL - remainder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*pBaseR = (totalR) / totalL * baseL;
|
|
|
|
*pOuterR = (totalR) / totalL * outerL;
|
2022-06-26 00:10:18 +08:00
|
|
|
*pPointR = (totalR) / totalL * pointL;
|
2022-06-25 18:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
2024-05-24 16:10:17 +08:00
|
|
|
float baseL=2;
|
|
|
|
float outerL=13;
|
|
|
|
float pointL=3;
|
|
|
|
float baseR,outerR,pointR;
|
2023-08-12 18:25:46 +08:00
|
|
|
int cx=350,cy=350;
|
2024-05-24 16:10:17 +08:00
|
|
|
float speed = 1;
|
2022-06-25 18:33:19 +08:00
|
|
|
Color trackColor = BLUE;
|
2022-06-26 00:10:18 +08:00
|
|
|
updateRadius(baseL, outerL, pointL, &baseR, &outerR, & pointR);
|
2023-08-12 18:25:46 +08:00
|
|
|
InitWindow(1000,700,"Epitrochoid");
|
2022-06-25 18:33:19 +08:00
|
|
|
SetTraceLogLevel(LOG_WARNING);
|
|
|
|
SetTargetFPS(60);
|
|
|
|
GuiSetStyle(DEFAULT,TEXT_SIZE,20);
|
|
|
|
|
2023-08-12 18:25:46 +08:00
|
|
|
Image trackImage=GenImageColor(700,700,WHITE);
|
2022-06-25 18:33:19 +08:00
|
|
|
//border
|
2023-08-12 18:25:46 +08:00
|
|
|
ImageFillRectangleEx(&trackImage,0,0,700,700,LIGHTGRAY);
|
|
|
|
ImageFillRectangleEx(&trackImage,5,5,690,690,WHITE);
|
2022-06-25 18:33:19 +08:00
|
|
|
|
2023-08-12 18:25:46 +08:00
|
|
|
Image circlesImage = GenImageColor(700,700,BLANK);
|
2022-06-25 18:33:19 +08:00
|
|
|
float r=0;
|
|
|
|
int lastx,lasty;
|
|
|
|
lasty=cy;
|
2022-06-26 00:10:18 +08:00
|
|
|
lastx=cx+(baseR+outerR-pointR);
|
2022-06-25 18:33:19 +08:00
|
|
|
int frameCount = 0;
|
|
|
|
while(!WindowShouldClose()) {
|
|
|
|
//GUI
|
2024-05-24 16:10:17 +08:00
|
|
|
float newOuterL = outerL, newBaseL = baseL, newPointL = pointL;
|
|
|
|
GuiSliderBar((Rectangle){ 70, 20, 100, 30 },"Outer",TextFormat("%i", (int)outerL), &newOuterL, 1, 50);
|
|
|
|
GuiSliderBar((Rectangle){ 70, 60, 100, 30 },"Base",TextFormat("%i", (int)baseL), &newBaseL, 1, 50);
|
|
|
|
GuiSliderBar((Rectangle){ 70, 100, 100, 30 },"Point",TextFormat("%i", (int)pointL), &newPointL, 1, 50);
|
|
|
|
GuiSliderBar((Rectangle){ 70, 150, 100, 30 },"Speed",TextFormat("%i", (int)speed), &speed, 1, 50);
|
|
|
|
GuiLabel((Rectangle){ 20, 220, 180, 30 },TextFormat("Color: 0x%02X%02X%02X",(int)(trackColor.r), (int)(trackColor.g),(int)(trackColor.b)));
|
|
|
|
GuiColorPicker((Rectangle){ 50, 250, 196, 192 }, NULL, &trackColor);
|
2023-08-12 18:25:46 +08:00
|
|
|
int doClear = GuiButton((Rectangle){ 120, 500, 80, 30 },"Clear");
|
2022-06-26 00:10:18 +08:00
|
|
|
if (newOuterL!=outerL || newBaseL!=baseL || newPointL!=pointL) {
|
2022-07-03 09:32:46 +08:00
|
|
|
if (newOuterL!=outerL)
|
|
|
|
pointL=newOuterL;
|
2022-06-26 00:10:18 +08:00
|
|
|
else
|
|
|
|
pointL=newPointL;
|
2022-06-25 18:33:19 +08:00
|
|
|
outerL=newOuterL;
|
|
|
|
baseL=newBaseL;
|
2022-06-26 00:10:18 +08:00
|
|
|
updateRadius(baseL, outerL, pointL, &baseR, &outerR, & pointR);
|
2022-06-25 18:33:19 +08:00
|
|
|
lasty=cy;
|
2022-06-26 00:10:18 +08:00
|
|
|
lastx=cx+(baseR+outerR-pointR);
|
2022-06-25 18:33:19 +08:00
|
|
|
r=0;
|
|
|
|
ImageClearBackground(&trackImage,WHITE);
|
2023-08-12 18:25:46 +08:00
|
|
|
ImageFillRectangleEx(&trackImage,0,0,700,700,LIGHTGRAY);
|
|
|
|
ImageFillRectangleEx(&trackImage,5,5,690,690,WHITE);
|
2022-06-25 18:33:19 +08:00
|
|
|
} else if (doClear) {
|
|
|
|
ImageClearBackground(&trackImage,WHITE);
|
2023-08-12 18:25:46 +08:00
|
|
|
ImageFillRectangleEx(&trackImage,0,0,700,700,LIGHTGRAY);
|
|
|
|
ImageFillRectangleEx(&trackImage,5,5,690,690,WHITE);
|
2022-06-25 18:33:19 +08:00
|
|
|
}
|
|
|
|
//update datas
|
|
|
|
r+=0.01;
|
|
|
|
float outerCX=cx+ (baseR+outerR)*cos(r);
|
|
|
|
float outerCY=cy+ (baseR+outerR)*sin(r);
|
2022-06-26 00:10:18 +08:00
|
|
|
float theta = r * (baseL+outerL) / outerL;
|
|
|
|
int x=round(outerCX - pointR * cos(theta));
|
|
|
|
int y=round(outerCY - pointR * sin(theta));
|
2022-06-25 18:33:19 +08:00
|
|
|
|
|
|
|
//update image (in CPU)
|
|
|
|
//ImageClearBackground(&trackImage,WHITE);
|
|
|
|
ImageDrawLineEx(&trackImage,lastx,lasty,x,y,3,trackColor);
|
|
|
|
|
|
|
|
frameCount++;
|
|
|
|
if (frameCount>=speed) {
|
|
|
|
ImageClearBackground(&circlesImage,BLANK);
|
|
|
|
//base circle
|
|
|
|
ImageDrawCircleEx(&circlesImage,cx,cy,baseR,1,LIGHTRED);
|
|
|
|
ImageDrawCircleEx(&circlesImage,outerCX,outerCY,outerR,1,LIGHTSLATEGRAY);
|
2022-06-26 00:10:18 +08:00
|
|
|
ImageDrawLineEx(&circlesImage,cx,cy,outerCX,outerCY,1,LIGHTRED);
|
|
|
|
ImageDrawLineEx(&circlesImage,x,y,outerCX,outerCY,1,LIGHTSLATEGRAY);
|
2022-06-25 18:33:19 +08:00
|
|
|
ImageDrawPointEx(&circlesImage,x,y,7,RED);
|
|
|
|
|
|
|
|
//Drawing in GPU
|
|
|
|
Texture trackTexture = LoadTextureFromImage(trackImage);
|
|
|
|
Texture circlesTexture = LoadTextureFromImage(circlesImage);
|
|
|
|
BeginDrawing();
|
|
|
|
ClearBackground(WHITE);
|
|
|
|
DrawTexture(trackTexture,300,0,WHITE);
|
|
|
|
DrawTexture(circlesTexture,300,0,WHITE);
|
|
|
|
EndDrawing();
|
|
|
|
UnloadTexture(circlesTexture);
|
|
|
|
UnloadTexture(trackTexture);
|
|
|
|
frameCount=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastx=x;
|
|
|
|
lasty=y;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Clean up
|
|
|
|
UnloadImage(circlesImage);
|
|
|
|
UnloadImage(trackImage);
|
|
|
|
CloseWindow();
|
2022-07-03 09:19:25 +08:00
|
|
|
}
|