一、什么是质心坐标?

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

以图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. git 入门教程

    git 入门教程之协同开发 前面我们已经介绍过远程仓库的相关概念,不过那时并没有深入探讨,只是讲解了如何创建远程仓库以及推送最新工作成果到远程仓库,实际上远程仓库对于团队协同开发很重要,不仅仅是团队协 ...

  2. ubuntu下cannot find lib....so.x 寻找动态链接库

    默认从/lib . /usr/lib 以及配置文件/etc/ld.so.conf内所列的目录下加载.so文件, 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld ...

  3. spring定时器cron

    关于cron表达式(参考资料):Cron 表达式包括以下 7 个字段: 秒 分 小时 月内日期 月 周内日期 年(可选字段) 特殊字符Cron 触发器利用一系列特殊字符,如下所示: 反斜线(/)字符表 ...

  4. ss-R:// 链接的含义

    1.问题 ss-R:// Mi41LmZ1Y2twcHBwcC50b2RheToyNDI4ODphdXRoX2FlczEyOF9tZDU6YWVzLTI1Ni1jdHI6dGxzMS4yX3RpY2t ...

  5. lua 5.3最简单plugin编写

    #include <windows.h> #include "lauxlib.h" /* Pop-up a Windows message box with your ...

  6. go 的数据类型

    bool string int int8 int16 int32(rune) int64 uint uint8(byte) uint16 uint32 uint64 uintptr:无符号整型,用于存 ...

  7. laravel的ORM模型的find(),findOrFail(),first(),firstOrFail(),get(),list(),toArray()之间的区别

    find($id)需要一个id并返回一个模型.如果不存在匹配的模型,则返回null. findOrFail($id)需要一个id并返回一个模型.如果不存在匹配的模型,则会引发错误, 它会抛出一个err ...

  8. Rabbit MQ

    前言: MQ 是什么?队列是什么,MQ 我们可以理解为消息队列,队列我们可以理解为管道.以管道的方式做消息传递. 场景: 1.其实我们在双11的时候,当我们凌晨大量的秒杀和抢购商品,然后去结算的时候, ...

  9. cut字符串截取

    cut字符串截取 -d 按字节截取 [root@slave elk]# ll total 0 drwxr-xr-x. 6 root root 194 Jan 24 16:15 bigdesk 截取前2 ...

  10. osx免驱网卡推荐

    1. 单频2.4G芯片为Realtek RTL8188cu, RTL8192cu,都可以用,如TP-Link TL-WN821N.TP-Link TL-WN823N等等:2. 单频2.4G芯片为Med ...