RedPanda-CPP/platform/linux/templates/hypotrochoid/hypotrochoid_c.txt

140 lines
4.2 KiB
Plaintext
Raw Normal View History

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
2022-06-26 00:10:18 +08:00
void updateRadius(int baseL, int innerL,int pointL, int *pBaseR, int *pInnerR, int *pPointR) {
2022-06-25 18:33:19 +08:00
int totalL=baseL;
2022-06-26 00:10:18 +08:00
if (innerL>baseL) {
if (innerL>pointL)
totalL = (2*innerL-baseL);
else {
totalL = (innerL+pointL-baseL);
}
} else {
if (pointL>innerL)
totalL = baseL-innerL+pointL;
}
2023-08-12 22:27:25 +08:00
int totalR = 320;
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);
}
}
2022-06-26 00:10:18 +08:00
*pBaseR = totalR / totalL * baseL;
*pInnerR = totalR / totalL * innerL;
*pPointR = totalR / totalL * pointL;
2022-06-25 18:33:19 +08:00
}
int main() {
2022-06-26 00:10:18 +08:00
int baseL=6;
2022-06-25 18:33:19 +08:00
int innerL=3;
2022-06-26 00:10:18 +08:00
int pointL=6;
int baseR,innerR,pointR;
2023-08-12 22:27:25 +08:00
int cx=350,cy=350;
2022-06-25 18:33:19 +08:00
int speed = 1;
Color trackColor = BLUE;
2022-06-26 00:10:18 +08:00
updateRadius(baseL, innerL, pointL, &baseR, &innerR, &pointR);
2023-08-12 22:27:25 +08:00
InitWindow(1100,700,"Hypotrochoid");
2022-06-25 18:33:19 +08:00
SetTraceLogLevel(LOG_WARNING);
SetTargetFPS(60);
GuiSetStyle(DEFAULT,TEXT_SIZE,20);
2023-08-12 22:27:25 +08:00
Image trackImage=GenImageColor(700,700,WHITE);
2022-06-25 18:33:19 +08:00
//border
2023-08-12 22:27:25 +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 22:27:25 +08:00
Image circlesImage = GenImageColor(700,700,BLANK);
2022-06-25 18:33:19 +08:00
float r=0;
int lastx,lasty;
2022-06-26 00:10:18 +08:00
bool skip=true;
2022-06-25 18:33:19 +08:00
lasty=cy;
lastx=cx+baseR;
int frameCount = 0;
while(!WindowShouldClose()) {
//GUI
int newInnerL = GuiSliderBar((Rectangle){ 70, 20, 200, 30 },"Inner",TextFormat("%i", (int)innerL), innerL, 1, 50);
int newBaseL = GuiSliderBar((Rectangle){ 70, 60, 200, 30 },"Base",TextFormat("%i", (int)baseL), baseL, 1, 50);
2022-06-26 00:10:18 +08:00
int newPointL = GuiSliderBar((Rectangle){ 70, 100, 200, 30 },"Point",TextFormat("%i", (int)pointL), pointL, 1, 50);
speed = GuiSliderBar((Rectangle){ 70, 150, 200, 30 },"Speed",TextFormat("%i", (int)speed), speed, 1, 50);
2023-08-12 22:27:25 +08:00
GuiLabel((Rectangle){ 20, 220, 200, 30 },TextFormat("Color: 0x%02X%02X%02X ",(int)(trackColor.r), (int)(trackColor.g),(int)(trackColor.b)));
2022-06-25 18:33:19 +08:00
trackColor= GuiColorPicker((Rectangle){ 50, 250, 196, 192 }, NULL, trackColor);
2023-08-12 22:27:25 +08:00
int doClear = GuiButton((Rectangle){ 120, 500, 80, 30 },"Clear");
2022-06-26 00:10:18 +08:00
if (newInnerL!=innerL || newBaseL!=baseL || newPointL!=pointL) {
if (newInnerL!=innerL)
pointL=newInnerL;
else
pointL=newPointL;
2022-06-25 18:33:19 +08:00
innerL=newInnerL;
baseL=newBaseL;
2022-06-26 00:10:18 +08:00
updateRadius(baseL, innerL, pointL, &baseR, &innerR, &pointR);
2022-06-25 18:33:19 +08:00
r=0;
2022-06-26 00:10:18 +08:00
skip=true;
2022-06-25 18:33:19 +08:00
ImageClearBackground(&trackImage,WHITE);
2023-08-12 22:27:25 +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 22:27:25 +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
float innerCX=cx+ (baseR-innerR)*cos(r);
float innerCY=cy+ (baseR-innerR)*sin(r);
2022-06-26 00:10:18 +08:00
int x,y;
float theta;
if (innerL<baseL) {
theta = r * (baseL-innerL) / innerL;
x=round(innerCX + pointR * cos(theta));
y=round(innerCY - pointR * sin(theta));
} else {
theta = r * (innerL-baseL) / innerL;
x=round(innerCX + pointR * cos(theta));
y=round(innerCY + pointR * sin(theta));
}
2022-06-25 18:33:19 +08:00
//update image (in CPU)
//ImageClearBackground(&trackImage,WHITE);
2022-06-26 00:10:18 +08:00
if (!skip)
ImageDrawLineEx(&trackImage,lastx,lasty,x,y,3,trackColor);
2022-06-25 18:33:19 +08:00
frameCount++;
if (frameCount>=speed) {
ImageClearBackground(&circlesImage,BLANK);
//base circle
ImageDrawCircleEx(&circlesImage,cx,cy,baseR,1,LIGHTRED);
2022-06-26 00:10:18 +08:00
ImageDrawCircleEx(&circlesImage,innerCX,innerCY,innerR,1,LIGHTGRAY);
ImageDrawLineEx(&circlesImage,innerCX,innerCY,cx,cy,1,LIGHTRED);
ImageDrawLineEx(&circlesImage,innerCX,innerCY,x,y,1,LIGHTGRAY);
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;
2022-06-26 00:10:18 +08:00
skip=false;
r+=0.01;
2022-06-25 18:33:19 +08:00
}
//Clean up
UnloadImage(circlesImage);
UnloadImage(trackImage);
CloseWindow();
2023-08-12 22:27:25 +08:00
}