Files
dl_old/dl/math/dl_math.cpp
T
2023-11-12 23:40:44 +08:00

249 lines
4.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "dl_math.h"
namespace dl::Math
{
std::vector<dl::PointU> getCircle(const Size& size, const PointU& xy, unsigned rad)
{
if (rad == 0)
return { xy };
std::vector<PointU> ret;
//遍历 宽度为 1 + r * 2
unsigned w = rad * 2 + 1;
//中心坐标为 (r, r)
PointU center = { rad,rad };
for (unsigned j = 0; j < w; ++j)
{
for (unsigned i = 0; i < w; ++i)
{
if (DisPow2U(PointU{ i, j }, center) <= rad * rad)
{
PointU out_xy = PointU{ i, j } - xy;
if (out_xy[0] < size[0]
&& out_xy[1] < size[1])
{
ret.push_back(out_xy);
}
}
}
}
return ret;
}
std::vector<dl::Point> getCircle(const Point& xy, unsigned rad)
{
if (rad == 0)
return { xy };
std::vector<Point> ret;
int r = rad;
for (int j = -r; j <= r; ++j)
{
for (int i = -r; i <= r; ++i)
{
if (i * i + j * j <= r * r)
ret.push_back(Point{ i, j } + xy);
}
}
return ret;
}
dl::Position4 Slerp(const Position4& q0, const Position4& q1, const Float& t)
{
Float magnitude = sqrt(DisPow2(q0) * DisPow2(q1));
assert(magnitude > 0);
Float product = DotProduct(q0, q1) / magnitude;
Float product_abs = std::abs(product);
if (product_abs < 1_f - std::numeric_limits<Float>::epsilon())
{
Float theta = acos(product_abs);
Float d = sin(theta);
assert(d > 0);
Float sign = product < 0 ? -1_f : 1_f;
Float s0 = sin((1_f - t) * theta) / d;
Float s1 = sin(sign * t * theta) / d;
return q0 * s0 + q1 * s1;
}
else
return q0;
}
std::vector<dl::Point> DrawLineLerp(const Point& p0, const Point& p1)
{
std::vector<Point> ret;
int N = DistanceDiagonal(p0, p1);
for (int step = 0; step <= N; ++step)
{
double t = N ? ((double)step / N) : 0.0;
ret.push_back(Each::Lerp(p0, p1, t, std::round));
}
return ret;
}
std::vector<dl::Point> DrawLineBresenham(const Point& p0, const Point& p1)
{
std::vector<Point> ret;
const int& x0 = p0[0];
const int& y0 = p0[1];
const int& x1 = p1[0];
const int& y1 = p1[1];
int dx = x1 - x0;
int dy = y1 - y0;
int ux = dx > 0 ? 1 : -1;
int uy = dy > 0 ? 1 : -1;
dx = abs(dx);
dy = abs(dy);
int dx_2 = dx * 2;
int dy_2 = dy * 2;
if (dx > dy)
{
int e = -dx;
int x = x0;
int y = y0;
for (x = x0; x != x1; x += ux)
{
ret.push_back({ x, y });
e = e + dy_2;
if (e > 0)
{
y += uy;
e = e - dx_2;
}
}
ret.push_back({ x, y });
}
else
{
int e = -dy;
int x = x0;
int y = y0;
for (y = y0; y != y1; y += uy)
{
ret.push_back({ x, y });
e = e + dx_2;
if (e > 0)
{
x += ux;
e = e - dy_2;
}
}
ret.push_back({ x, y });
}
return ret;
}
std::vector<dl::Point> DrawLineOrth(const Point& p0, const Point& p1)
{
std::vector<Point> ret;
int dx = p1[0] - p0[0];
int dy = p1[1] - p0[1];
int nx = abs(dx);
int ny = abs(dy);
int sign_x = dx > 0 ? 1 : -1;
int sign_y = dy > 0 ? 1 : -1;
Point p = p0;
ret.push_back(p);
int ix = 0;
int iy = 0;
for (; ix < nx || iy < ny;)
{
int decision = (1 + 2 * ix) * ny - (1 + 2 * iy) * nx;
if (decision < 0)
{//水平
p[0] += sign_x;
ix++;
}
else
{//垂直
p[1] += sign_y;
iy++;
}
ret.push_back(p);
}
return ret;
}
std::vector<dl::Point> DrawLineCover(const Point& p0, const Point& p1)
{
std::vector<Point> ret;
int dx = p1[0] - p0[0];
int dy = p1[1] - p0[1];
int nx = abs(dx);
int ny = abs(dy);
int sign_x = dx > 0 ? 1 : -1;
int sign_y = dy > 0 ? 1 : -1;
Point p = p0;
ret.push_back(p);
int ix = 0;
int iy = 0;
for (; ix < nx || iy < ny;)
{
int decision = (1 + 2 * ix) * ny - (1 + 2 * iy) * nx;
if (decision == 0)
{//对角线
p[0] += sign_x;
p[1] += sign_y;
ix++;
iy++;
}
else if (decision < 0)
{//水平
p[0] += sign_x;
ix++;
}
else
{//垂直
p[1] += sign_y;
iy++;
}
ret.push_back(p);
}
return ret;
}
void MergePath(std::list<Point>& list_pos)
{
size_t size = list_pos.size();
if (list_pos.size() < 3)
return;//少于3个节点不用处理
//初始向量
auto iter_0 = list_pos.begin();//前一个节点
auto iter_1 = iter_0;//后一个节点
++iter_1;
Point v = *iter_1 - *iter_0;//向量指向前一个节点
++iter_0;
++iter_1;
//直到后一个到end
for (; iter_1 != list_pos.end();)
{
Point v1 = *iter_1 - *iter_0;
if (v1 == v)
{//方向相同,删除前一个节点
list_pos.erase(iter_0);
iter_0 = iter_1++;//iter_0失效、指向iter_1,iter_1指向下一位置
}
else
{
v = v1;
++iter_0;
++iter_1;
}
}
}
}