1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#include "MathLibraryDLL.h"
#include <iostream>
#include <cmath>
// C接口实现
extern "C"
{
void* Vector3_Create(float x, float y, float z)
{
return new MathLibDLL::Vector3(x, y, z);
}
void Vector3_Destroy(void* vector)
{
delete static_cast<MathLibDLL::Vector3*>(vector);
}
void Vector3_Add(void* result, void* a, void* b)
{
auto* res = static_cast<MathLibDLL::Vector3*>(result);
auto* vecA = static_cast<MathLibDLL::Vector3*>(a);
auto* vecB = static_cast<MathLibDLL::Vector3*>(b);
*res = *vecA + *vecB;
}
float Vector3_Magnitude(void* vector)
{
auto* vec = static_cast<MathLibDLL::Vector3*>(vector);
return vec->Magnitude();
}
void Vector3_Print(void* vector)
{
auto* vec = static_cast<MathLibDLL::Vector3*>(vector);
vec->Print();
}
float ToRadians(float degrees)
{
return degrees * 3.14159f / 180.0f;
}
float ToDegrees(float radians)
{
return radians * 180.0f / 3.14159f;
}
float Clamp(float value, float min, float max)
{
if (value < min) return min;
if (value > max) return max;
return value;
}
float Lerp(float a, float b, float t)
{
return a + (b - a) * Clamp(t, 0.0f, 1.0f);
}
}
// C++接口实现
namespace MathLibDLL
{
Vector3::Vector3(float x, float y, float z) : m_X(x), m_Y(y), m_Z(z) {}
Vector3::~Vector3() {}
Vector3 Vector3::operator+(const Vector3& other) const
{
return Vector3(m_X + other.m_X, m_Y + other.m_Y, m_Z + other.m_Z);
}
Vector3 Vector3::operator-(const Vector3& other) const
{
return Vector3(m_X - other.m_X, m_Y - other.m_Y, m_Z - other.m_Z);
}
Vector3 Vector3::operator*(float scalar) const
{
return Vector3(m_X * scalar, m_Y * scalar, m_Z * scalar);
}
float Vector3::Magnitude() const
{
return std::sqrt(m_X * m_X + m_Y * m_Y + m_Z * m_Z);
}
Vector3 Vector3::Normalize() const
{
float mag = Magnitude();
if (mag > 0)
return Vector3(m_X / mag, m_Y / mag, m_Z / mag);
return Vector3();
}
float Vector3::Dot(const Vector3& other) const
{
return m_X * other.m_X + m_Y * other.m_Y + m_Z * other.m_Z;
}
Vector3 Vector3::Cross(const Vector3& other) const
{
return Vector3(
m_Y * other.m_Z - m_Z * other.m_Y,
m_Z * other.m_X - m_X * other.m_Z,
m_X * other.m_Y - m_Y * other.m_X
);
}
void Vector3::Print() const
{
std::cout << "(" << m_X << ", " << m_Y << ", " << m_Z << ")" << std::endl;
}
}
|