RedPanda-CPP/linux/templates/hypotrochoid_c.txt

120 lines
3.5 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
#include <raygui/raygui.h>
void updateRadius(int baseL, int innerL, int *pBaseR, int *pInnerR) {
int totalL=baseL;
if (innerL>baseL)
totalL = (2*innerL-baseL);
int totalR = 420;
int remainder = totalR % totalL;
if (remainder!=0) {
if (remainder < totalL / 2) {
totalR -= remainder;
} else {
totalR += ( totalL - remainder);
}
}
if (innerL<baseL) {
*pBaseR = totalR;
*pInnerR = (totalR) / totalL * innerL;
} else {
*pBaseR = (totalR) / totalL * baseL;
*pInnerR = totalR / totalL * innerL;
}
}
int main() {
int baseL=2;
int innerL=3;
int baseR,innerR;
int cx=450,cy=450;
int speed = 1;
Color trackColor = BLUE;
updateRadius(baseL, innerL, &baseR, &innerR);
InitWindow(1300,900,"Hypotrochoid");
SetTraceLogLevel(LOG_WARNING);
SetTargetFPS(60);
GuiSetStyle(DEFAULT,TEXT_SIZE,20);
Image trackImage=GenImageColor(900,900,WHITE);
//border
ImageFillRectangleEx(&trackImage,0,0,900,900,LIGHTGRAY);
ImageFillRectangleEx(&trackImage,5,5,890,890,WHITE);
Image circlesImage = GenImageColor(900,900,BLANK);
float r=0;
int lastx,lasty;
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);
speed = GuiSliderBar((Rectangle){ 70, 120, 200, 30 },"Speed",TextFormat("%i", (int)speed), speed, 1, 50);
GuiLabel((Rectangle){ 20, 220, 80, 30 },TextFormat("Color: 0x%X%X%X ",(int)(trackColor.r), (int)(trackColor.g),(int)(trackColor.b)));
trackColor= GuiColorPicker((Rectangle){ 50, 250, 196, 192 }, NULL, trackColor);
int doClear = GuiButton((Rectangle){ 120, 700, 80, 30 },"Clear");
if (newInnerL!=innerL || newBaseL!=baseL) {
innerL=newInnerL;
baseL=newBaseL;
updateRadius(baseL, innerL, &baseR, &innerR);
lasty=cy;
lastx=cx+baseR;
r=0;
ImageClearBackground(&trackImage,WHITE);
ImageFillRectangleEx(&trackImage,0,0,900,900,LIGHTGRAY);
ImageFillRectangleEx(&trackImage,5,5,890,890,WHITE);
} else if (doClear) {
ImageClearBackground(&trackImage,WHITE);
ImageFillRectangleEx(&trackImage,0,0,900,900,LIGHTGRAY);
ImageFillRectangleEx(&trackImage,5,5,890,890,WHITE);
}
//update datas
r+=0.01;
float innerCX=cx+ (baseR-innerR)*cos(r);
float innerCY=cy+ (baseR-innerR)*sin(r);
float theta = r * baseL / innerL;
int x=round(innerCX + innerR * cos(theta));
int y=round(innerCY + innerR * sin(theta));
//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,innerCX,innerCY,innerR,1,LIGHTSLATEGRAY);
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();
}