Added Area.h
This commit is contained in:
parent
7589d62732
commit
866f69034b
70
inc/hgl/math/Area.h
Normal file
70
inc/hgl/math/Area.h
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include<hgl/math/Vector.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* 根据三角形的三边长计算三角形的面积
|
||||||
|
*/
|
||||||
|
inline const float TriangleArea(const float a,const float b,const float c)
|
||||||
|
{
|
||||||
|
const float s=(a+b+c)*0.5f;
|
||||||
|
|
||||||
|
return sqrt(s*(s-a)*(s-b)*(s-c));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float TriangleArea(const Vector2f &a,const Vector2f &b,const Vector2f &c)
|
||||||
|
{
|
||||||
|
return fabs((a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x))*0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float TriangleArea(const Vector3f &a,const Vector3f &b,const Vector3f &c)
|
||||||
|
{
|
||||||
|
return glm::length(glm::cross(b-a,c-a))*0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float RectangleArea(const float width,const float height)
|
||||||
|
{
|
||||||
|
return width*height;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float RectangleArea(const Vector2f &size)
|
||||||
|
{
|
||||||
|
return size.x*size.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 正圆面积计算
|
||||||
|
* @param radius 半径
|
||||||
|
*/
|
||||||
|
inline const float CircleArea(const float radius)
|
||||||
|
{
|
||||||
|
return HGL_PI*radius*radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 椭圆面积计算
|
||||||
|
* @param l_radius 长半径
|
||||||
|
* @param s_radius 短半径
|
||||||
|
*/
|
||||||
|
inline const float EllipseArea(const float horizontal_radius,const float vertical_radius)
|
||||||
|
{
|
||||||
|
return HGL_PI*horizontal_radius*vertical_radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float EllipseArea(const Vector2f &radius)
|
||||||
|
{
|
||||||
|
return HGL_PI*radius.x*radius.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float SectorArea(const float radius,const float angle)
|
||||||
|
{
|
||||||
|
return HGL_PI*radius*radius*angle/360.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const float QuadArea(const Vector3f &a,const Vector3f &b,const Vector3f &c,const Vector3f &d)
|
||||||
|
{
|
||||||
|
return TriangleArea(a,b,c)+TriangleArea(a,c,d);
|
||||||
|
}
|
||||||
|
}//namespace hgl
|
8
inc/hgl/math/Bezier.h
Normal file
8
inc/hgl/math/Bezier.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include<hgl/math/Vector.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
|
||||||
|
}//namespace hgl
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include<hgl/math/PrimaryMathematics.h>
|
#include<hgl/math/PrimaryMathematics.h>
|
||||||
#include<hgl/math/FastTriangle.h>
|
#include<hgl/math/FastTriangle.h>
|
||||||
|
#include<hgl/math/Area.h>
|
||||||
#include<hgl/math/Vector.h> // Game Math and Geometry Library
|
#include<hgl/math/Vector.h> // Game Math and Geometry Library
|
||||||
#include<hgl/math/Matrix.h>
|
#include<hgl/math/Matrix.h>
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ namespace hgl
|
|||||||
constexpr const double HGL_SIN_270=-1;
|
constexpr const double HGL_SIN_270=-1;
|
||||||
constexpr const double HGL_SIN_315=-0.707106781187;
|
constexpr const double HGL_SIN_315=-0.707106781187;
|
||||||
|
|
||||||
#define HGL_DEF_DEG2RAD(ang) constexpr double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f);
|
#define HGL_DEF_DEG2RAD(ang) constexpr const double HGL_RAD_##ang=double(ang)*(HGL_PI/180.0f);
|
||||||
|
|
||||||
HGL_DEF_DEG2RAD(0)
|
HGL_DEF_DEG2RAD(0)
|
||||||
HGL_DEF_DEG2RAD(45)
|
HGL_DEF_DEG2RAD(45)
|
||||||
@ -104,25 +104,6 @@ namespace hgl
|
|||||||
|
|
||||||
return double(floor(value*per))/per;
|
return double(floor(value*per))/per;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 正圆面积计算
|
|
||||||
* @param radius 半径
|
|
||||||
*/
|
|
||||||
inline constexpr double CircleArea(const double radius)
|
|
||||||
{
|
|
||||||
return(radius*radius*HGL_PI);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 椭圆面积计算
|
|
||||||
* @param l_radius 长半径
|
|
||||||
* @param s_radius 短半径
|
|
||||||
*/
|
|
||||||
inline constexpr double ElipseArea(const double l_radius,const double s_radius)
|
|
||||||
{
|
|
||||||
return(l_radius*s_radius*HGL_PI);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 球体积计算
|
* 球体积计算
|
||||||
|
@ -261,19 +261,19 @@ namespace hgl
|
|||||||
return from + (to - from) * alpha;
|
return from + (to - from) * alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_nearly_equal(const float v1,const float v2,const float deviation=HGL_FLOAT_SMALL)
|
inline bool is_nearly_equal(const float v1,const float v2,const float deviation=HGL_FLOAT_KINDA_SMALL)
|
||||||
{
|
{
|
||||||
return fabsf(v1-v2)<=HGL_FLOAT_SMALL;
|
return fabsf(v1-v2)<=deviation;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_nearly_equal(const Vector2f &v1,const Vector2f &v2,const float deviation=HGL_FLOAT_SMALL)
|
inline bool is_nearly_equal(const Vector2f &v1,const Vector2f &v2,const float deviation=HGL_FLOAT_KINDA_SMALL)
|
||||||
{
|
{
|
||||||
return length_squared_2d(v1,v2)<=HGL_FLOAT_SMALL;
|
return length_squared_2d(v1,v2)<=deviation;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_nearly_equal(const Vector3f &v1,const Vector3f &v2,const float deviation=HGL_FLOAT_SMALL)
|
inline bool is_nearly_equal(const Vector3f &v1,const Vector3f &v2,const float deviation=HGL_FLOAT_KINDA_SMALL)
|
||||||
{
|
{
|
||||||
return length_squared(v1,v2)<=HGL_FLOAT_SMALL;
|
return length_squared(v1,v2)<=deviation;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -413,5 +413,15 @@ namespace hgl
|
|||||||
&&IsNearlyEqual(a.y,b.y)
|
&&IsNearlyEqual(a.y,b.y)
|
||||||
&&IsNearlyEqual(a.z,b.z);
|
&&IsNearlyEqual(a.z,b.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline const Vector3f LerpDirection(const Vector3f ¢er,const Vector3f &from_direction,const Vector3f &to_direction,const float alpha)
|
||||||
|
{
|
||||||
|
return glm::normalize(center + (from_direction - center) * alpha + (to_direction - center) * alpha);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Vector3f SLerpDirection(const Vector3f ¢er,const Vector3f &from,const Vector3f &to,const float alpha)
|
||||||
|
{
|
||||||
|
return glm::normalize(glm::slerp(from - center, to - center, alpha) + center);
|
||||||
|
}
|
||||||
}//namespace hgl
|
}//namespace hgl
|
||||||
#endif//HGL_ALGORITHM_MATH_VECTOR_INCLUDE
|
#endif//HGL_ALGORITHM_MATH_VECTOR_INCLUDE
|
||||||
|
@ -51,6 +51,8 @@ SET(MATH_INCLUDE_PATH ${CMCORE_ROOT_INCLUDE_PATH}/hgl/math)
|
|||||||
##Math--------------------------------------------------------
|
##Math--------------------------------------------------------
|
||||||
SET(MATH_HEADER_FILES ${MATH_INCLUDE_PATH}/bvec.h
|
SET(MATH_HEADER_FILES ${MATH_INCLUDE_PATH}/bvec.h
|
||||||
${MATH_INCLUDE_PATH}/FastTriangle.h
|
${MATH_INCLUDE_PATH}/FastTriangle.h
|
||||||
|
${MATH_INCLUDE_PATH}/Bezier.h
|
||||||
|
${MATH_INCLUDE_PATH}/Area.h
|
||||||
${MATH_INCLUDE_PATH}/Math.h
|
${MATH_INCLUDE_PATH}/Math.h
|
||||||
${MATH_INCLUDE_PATH}/MathConst.h
|
${MATH_INCLUDE_PATH}/MathConst.h
|
||||||
${MATH_INCLUDE_PATH}/Matrix.h
|
${MATH_INCLUDE_PATH}/Matrix.h
|
||||||
|
Loading…
x
Reference in New Issue
Block a user