一、什么是质心坐标?

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

以图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. PHP全局变量,超全局变量

    php中有许多超全局变量,这意味着它们在一个脚本的全部作用域中都可用.在函数或方法中无需执行 global $variable; 就可以访问它们. 这些超全局变量是: $GLOBALS    引用全局 ...

  2. 构建之法 chapter1 心得

    阅读完了<构建之法>第一章后,觉得我们平时使用的软件并不是自己想象中的那样简单,用的时候是觉得很方便,但从来没有考虑过一个软件的背后需要一个团队多少的付出才能换来一个获得用户频频好评的软件 ...

  3. js ·节点的知识点

    1. DOM document object model (1) 节点树状图 Document>documentElement>body>tagname 2. 我们常用的节点类型 元 ...

  4. PyQt5 教程地址

    https://maicss.gitbooks.io/pyqt5/content/介绍.htmlPyQt5{ QtCore { BasicTimer#定时器 } QtWidgets#窗口工具 { QA ...

  5. js实现复制内容到粘贴板

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. 下载jar包的网站

    http://mvnrepository.com/ http://findjar.com http://sourceforge.net/

  7. Tenka 1 Computer Contest C-Align

    C - Align Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement You are giv ...

  8. 链表中倒数第k个节点(Java)

    链表中倒数第k个节点 题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路:two-pointers思想,因为是单链表,没法得prevous点,直接遍历得到链表长度再重新遍历效率很低. 采用双指 ...

  9. 使用python调用其他脚本

    cmd = '<command line string>' print(cmd) p = subprocess.Popen(args=cmd, shell=True, stdout=sub ...

  10. mysql数据备份及恢复

    备份工具 mysqldump mysqldump是mysql和mariadb上最好的备份工具之一,免费开源. mysqldump 首先查询每个数据库和每个表的结构与数据,然后把查出的所有内容导出到文本 ...