Skip to content

Commit

Permalink
adjust: type.h 重命名为 types.h,并增加支持常见操作的 Rect 结构体
Browse files Browse the repository at this point in the history
  • Loading branch information
yixy-only committed Jun 27, 2024
1 parent 045e876 commit 5e50e48
Show file tree
Hide file tree
Showing 6 changed files with 648 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <windef.h>
#include "ege_def.h"
#include "ege_math.h"
#include "type.h"
#include "types.h"

// 交换颜色中 R 通道和 B 通道: 0xAARRGGBB -> 0xAABBGGRR
#define RGBTOBGR(color) ((color_t)((((color) & 0xFF) << 16) | (((color) & 0xFF0000) >> 16) | ((color) & 0xFF00FF00)))
Expand Down
2 changes: 1 addition & 1 deletion src/ege_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "ege_graph.h"
#include "ege_dllimport.h"

#include "type.h"
#include "types.h"
#include "utils.h"
#include "color.h"
#include "encodeconv.h"
Expand Down
86 changes: 0 additions & 86 deletions src/type.h

This file was deleted.

161 changes: 161 additions & 0 deletions src/types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "ege_head.h"
#include "ege_common.h"
#include "types.h"

namespace ege
{

//------------------------------------------------------------------------------
// Rect
//------------------------------------------------------------------------------

Rect normalize(const Rect& rect)
{
return Rect(rect).normalize();
}

Rect intersect(const Rect& a, const Rect& b)
{
return Rect(a).intersect(b);
}

Rect& Rect::intersect(const Rect& rect)
{
if (isNull() || rect.isNull())
return setEmpty();

Rect a(*this), b(rect);
a.normalize();
b.normalize();

if ( (a.right() <= b.left()) || (b.right() <= a.left())
|| (a.bottom() <= b.top()) || (b.bottom() <= a.top() )) {
return setEmpty();
}

int left = MAX(a.left(), b.left());
int top = MAX(a.top(), b.top());
int right = MIN(a.right(), b.right());
int bottom = MIN(a.bottom(), b.bottom());

setBounds(left, top, right, bottom);

return *this;
}

Rect& Rect::unite(const Rect& rect)
{
if (rect.isNull())
return *this;;

if (isNull())
return *this = rect;

Rect a(*this), b(rect);
a.normalize();
b.normalize();

int left = MIN(a.left(), b.left());
int top = MIN(a.top(), b.top());
int right = MAX(a.right(), b.right());
int bottom = MAX(a.bottom(), b.bottom());

setBounds(left, top, right, bottom);
return *this;
}

Rect& Rect::unite(int x, int y, int width, int height)
{
unite(Rect(x, y, width, height));
return *this;
}

Rect& Rect::unite(const Point& point)
{
if (isNull()) {
set(point, Size(1, 1));
} else {
if (point.x < left()) {
setLeft(point.x);
} else if (point.x >= right()) {
setRight(point.x + 1);
}

if (point.y < top()) {
setTop(point.y);
} else if (point.y >= bottom()) {
setBottom(point.y + 1);
}
}

return *this;
}

Rect& Rect::unite(const Point points[], int count)
{
unite(bounds(points, count));
return *this;
}

Rect unite(const Rect& a, const Rect& b)
{
return Rect(a).unite(b);
}

Rect bounds(const Point points[], int count)
{
if (count <= 0)
return Rect();

int xMin = points[0].x, xMax = xMin;
int yMin = points[0].y, yMax = yMin;

for (int i = 1; i < count; i++) {
if (points[i].x < xMin) {
xMin = points[i].x;
} else if (points[i].x > xMax) {
xMax = points[i].x;
}

if (points[i].y < yMin) {
yMin = points[i].y;
} else if (points[i].y > yMax) {
yMax = points[i].y;
}
}

Point leftTop(xMin, yMin);
Point bottomRight(xMax + 1, yMax + 1);

return Rect(leftTop, bottomRight, false);
}

Rect bounds(const Point& a, const Point& b)
{
int xMin, xMax;
if (a.x <= b.x) {
xMin = a.x;
xMax = b.x;
} else {
xMin = b.x;
xMax = a.x;
}

int yMin, yMax;
if (a.y <= b.y) {
yMin = a.y;
yMax = b.y;
} else {
yMin = b.y;
yMax = a.y;
}

Point leftTop(xMin, yMin);
Point bottomRight(xMax + 1, yMax + 1);

return Rect(leftTop, bottomRight, false);
}

//------------------------------------------------------------------------------

}
Loading

0 comments on commit 5e50e48

Please sign in to comment.