refactor: rectangle functions

This commit is contained in:
huaqo-games
2025-11-06 14:27:27 +01:00
parent cc53a19fd5
commit cd6afe32f0
3 changed files with 43 additions and 41 deletions
+3
View File
@@ -232,5 +232,8 @@ Vector2 GetRandomVector(const Rectangle bounds);
Vector2 RotationToVector2(float rotation); Vector2 RotationToVector2(float rotation);
float Vector2ToRotation(Vector2 vec); float Vector2ToRotation(Vector2 vec);
Vector2 GetPositionInsideRectangle(Rectangle rec);
Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max);
#endif //ENGINE_H #endif //ENGINE_H
-41
View File
@@ -1,47 +1,6 @@
#include "appTypes.h" #include "appTypes.h"
Vector2 GetPositionInsideRectangle(Rectangle rec)
{
int randX = GetRandomValue(rec.x, rec.width);
int randY = GetRandomValue(rec.y, rec.height);
Vector2 randVector = {randX, randY};
return randVector;
}
Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max)
{
int side = GetRandomValue(0, 3);
Vector2 pos = {0};
switch (side)
{
case 0: // left
pos.x = rec.x - GetRandomValue(min, max);
pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height));
break;
case 1: // right
pos.x = rec.x + rec.width + GetRandomValue(min, max);
pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height));
break;
case 2: // top
pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width));
pos.y = rec.y - GetRandomValue(min, max);
break;
case 3: // bottom
pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width));
pos.y = rec.y + rec.height + GetRandomValue(min, max);
break;
}
return pos;
}
const TextureAsset birdsTextureAssets[BIRDS_TEX_COUNT] = { const TextureAsset birdsTextureAssets[BIRDS_TEX_COUNT] = {
{"assets/birds.png", 16.0f, 16.0f, 45.0f} {"assets/birds.png", 16.0f, 16.0f, 45.0f}
}; };
+40
View File
@@ -0,0 +1,40 @@
#include "engine.h"
Vector2 GetPositionInsideRectangle(Rectangle rec)
{
int randX = GetRandomValue(rec.x, rec.width);
int randY = GetRandomValue(rec.y, rec.height);
Vector2 randVector = {randX, randY};
return randVector;
}
Vector2 GetPositionOutsideRectangle(Rectangle rec, int min, int max)
{
int side = GetRandomValue(0, 3);
Vector2 pos = {0};
switch (side)
{
case 0: // left
pos.x = rec.x - GetRandomValue(min, max);
pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height));
break;
case 1: // right
pos.x = rec.x + rec.width + GetRandomValue(min, max);
pos.y = GetRandomValue((int)rec.y, (int)(rec.y + rec.height));
break;
case 2: // top
pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width));
pos.y = rec.y - GetRandomValue(min, max);
break;
case 3: // bottom
pos.x = GetRandomValue((int)rec.x, (int)(rec.x + rec.width));
pos.y = rec.y + rec.height + GetRandomValue(min, max);
break;
}
return pos;
}