一、什么是质心坐标?

在几何结构中,质心坐标是指图形中的点相对各顶点的位置。

以图1的线段 AB 为例,点 P 位于线段 AB 之间,

  图1 线段AB和点P

此时计算点 P 的公式为 

同理,在三角形 ABC 中,三角形内点 P 的计算公式为:——公式一。

公式一的最终表示形式为:

那么如何计算参数 m 和 n 呢?

下面给出推导过程:

根据公式一可得:

我们将  记作向量  ,将  记作向量  , 将  记作向量 ,则公式为:

然后分别乘以 v0  和 v1 得到如下两个公式:

继续化解方程式得:

令:

继续化简方程式得:

根据莱布尼茨公式可得:

其中d = 

二、质心坐标的应用

质心坐标的应用场景很多,可以用于:

  • 判断一个点是否在三角形内
  • 根据三角形三个顶点得到三角形内一个点P

三、代码实现

已知三角形的三个顶点,计算三角形内一个点 P 的代码实现:

//vPos1, vPos2,vPos3 分别代表三角形的三个顶点
//vP代表三角形内的一个点、
//fI代表 vPos1的系数
//fJ代表 vPos2的系数
//fK 代表 vPos3的系数
bool GetBarycentricCoord(vec2 vPos1, vec2 vPos2, vec2 vPos3, vec2 vP, float& fI, float& fJ, float& fK)
{
// Compute vectors
vec2 v0 = vPos2 - vPos1;
vec2 v1 = vPos3 - vPos1;
vec2 v2 = vP - vPos1; // Compute dot products
float fDot00 = Dot(v0, v0);
float fDot01 = Dot(v0, v1);
float fDot02 = Dot(v0, v2);
float fDot11 = Dot(v1, v1);
float fDot12 = Dot(v1, v2); // Compute barycentric coordinates
float fInvDenom = / (fDot00 * fDot11 - fDot01 * fDot01);
float fTempU = (fDot11 * fDot02 - fDot01 * fDot12) * fInvDenom;
float fTempV = (fDot00 * fDot12 - fDot01 * fDot02) * fInvDenom; // Check if point is in triangle or edge
bool bIsInTri = (fTempU >= ) && (fTempV >= ) && (fTempU + fTempV <= );
if (bIsInTri)
{
fJ = fTempU;
fK = fTempV;
fI = - fJ - fK;
}
return bIsInTri;
}

质心坐标(barycentric coordinates)及其应用的更多相关文章

  1. ray与triangle/quad求交二三事

    引擎中,ray与quad求交,算法未细看,但有求解二次方程,不解.ray与triangle求交,使用的是97年经典算法,仔细看过论文,多谢小武同学指点,用到了克拉默法则求解线性方程组.想模仿该方法,做 ...

  2. Discrete.Differential.Geometry-An.Applied.Introduction(sig2008)笔记

    -------------------------------------------------------------- Chapter 1: Introduction to Discrete D ...

  3. A trip through the Graphics Pipeline 2011_12 Tessellation

    Welcome back! This time, we’ll look into what is perhaps the “poster boy” feature introduced with th ...

  4. A trip through the Graphics Pipeline 2011_08_Pixel processing – “fork phase”

    In this part, I’ll be dealing with the first half of pixel processing: dispatch and actual pixel sha ...

  5. A trip through the Graphics Pipeline 2011_07_Z/Stencil processing, 3 different ways

    In this installment, I’ll be talking about the (early) Z pipeline and how it interacts with rasteriz ...

  6. A trip through the Graphics Pipeline 2011_06_(Triangle) rasterization and setup

    Welcome back. This time we’re actually gonna see triangles being rasterized – finally! But before we ...

  7. uva 11275 3D Triangles

    题意:三维空间中,给出两个三角形的左边,问是否相交. 面积法判断点在三角形内: #include<cstdio> #include<cmath> #include<cst ...

  8. Direct3D 11的流水线

    流水线 流水线(Pipeline)是理解D3D必须要掌握的概念. 整个流水线有很多步骤,有的步骤是固定功能,不用怎么配置,有的步骤是要写代码的,也就是所谓的着色器程序(Shader). 一般来说,将流 ...

  9. OpenGL - Tessellation Shader 【转】

    http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html Patch is just an ordered list of vertices (在tes ...

随机推荐

  1. 【转载】 spring事物配置,声明式事务管理和基于@Transactional注解的使用

    https://blog.csdn.net/bao19901210/article/details/41724355

  2. windows 下的 Apache SSL证书配置

    一.获取证书 1.从腾讯云获取 (1)登录腾讯云后,在“产品”中搜索[ssl]然后会得到 "SSL证书“,进入后点“立即选购” 这样就完成了证书的申请,等待一个工作日的审核. 审核通过后,进 ...

  3. ssm项目整合shiro

    pom.xml <properties> <shiro.version>1.2.2</shiro.version> </properties> < ...

  4. MySQL Transaction--查看未提交事务执行的SQL

    未提交事务 长期未提交事务,指开启事务后,长时间未向MySQL发出SQL执行请求或事务处理(COMMIT/ROLLBACK)请求,在系统表`information_schema`.`INNODB_TR ...

  5. java8_api_net

    网络编程1    操作ip地址        核心类 InetAddress        相关方法 getByName,getAllByName,getLocalHost    操作socket地址 ...

  6. java8_api_集合框架

    泛型表示 E,K,V,T 集合框架1    集合的概念    集合的好处    集合的层次结构        集合的概念        集合是一个包含一组对象的对象,也称为容器        集合中的 ...

  7. Freescale MKL16Z1288VF4 芯片调试接口

    WDOG监视内部系统操作,并在发生故障时强制复位.它可以运行在一个独立的1 kHz低功率振荡器,具有可编程刷新窗口,以检测程序流或系统频率的偏差. 看门狗计时器保持一个时间在系统上运行,并重置它,以防 ...

  8. C 标准库头文件

    头文件 说明 头文件 说明 <assert.h> 条件编译宏,将参数与零比较 <complex.h> (C99 起) 复数运算 <ctype.h> 用来确定包含于字 ...

  9. Java使用算数运算符实现两个整数互换

    有很简单的方法可以实现,不过还是用一步一个脚印的方法来试试 首先分析一下流程 这里有两个变量. int a = 10,b = 40; //此时 a 为10,b 为40 然后我们开始走路,在不依靠第三者 ...

  10. CURL 支持 GET、PUT、POST、DELETE请求

    一个方法解决所有的 curl 请求的问题. <?php function curlTypeData( $method, $url, $data=false, $json=false ) { $d ...