一、什么是质心坐标?

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

以图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. 如何将maven配置在eclipse上

    如何将maven配置在eclipse上 安装maven:(需要确保你已经安装了jdk) 去apache官网下载maven:http://maven.apache.org/download.cgi 下载 ...

  2. explain和profiling分析查询SQL时间

    mysql可以通过profiling命令查看到执行查询SQL消耗的时间. 默认情况下,mysql是关闭profiling的,命令: select @@profiling; +------------- ...

  3. 2017第八届蓝桥杯C/C++语言A组

    一:题目: 标题:迷宫 X星球的一处迷宫游乐场建在某个小山坡上.它是由10x10相互连通的小房间组成的. 房间的地板上写着一个很大的字母.我们假设玩家是面朝上坡的方向站立,则:L表示走到左边的房间,R ...

  4. BLE和2.4G实现通信

    1. 背景 客户的项目是无线控制灯具,目前采用2.4G芯片,一端是2.4G遥控器,一端是2.4G灯具.现在客户的需求是在不增加成本的条件下增加手机APP控制.因为BLE芯片一般会比纯2.4G芯片价格高 ...

  5. js设置,获取cookie

    function setCookie(c_name,value,expireMinutes){ var exdate=new Date(); exdate.setMinutes(exdate.getM ...

  6. js/php判断移动端还是PC端

    if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobi ...

  7. rust 如何搜索,如何debug (解决)

    requirement c程序可以用手动查看.h文件获得定义,或者用dumpbin分析lib或者dll获得二进制信息. 但是rust如何得到库的定义呢? rust如何查看函数定义? rust如何deb ...

  8. Scaffold(Material库中提供的页面脚手架)知识点

    Scaffold 包含:appBar.body.floatingActionButton

  9. Inno Setup打包带有MSI文件的程序

    [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相同的AppId值. ; (生成新的GUID,点击 工具|在IDE中生成GUID.) AppId={{47A1 ...

  10. 创建maven工程的时候卡死的解决办法

    在idea的maven,runner,properties里面添加 archetypeCatalog=internal