质心坐标(barycentric coordinates)及其应用
一、什么是质心坐标?
在几何结构中,质心坐标是指图形中的点相对各顶点的位置。
以图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)及其应用的更多相关文章
- ray与triangle/quad求交二三事
引擎中,ray与quad求交,算法未细看,但有求解二次方程,不解.ray与triangle求交,使用的是97年经典算法,仔细看过论文,多谢小武同学指点,用到了克拉默法则求解线性方程组.想模仿该方法,做 ...
- Discrete.Differential.Geometry-An.Applied.Introduction(sig2008)笔记
-------------------------------------------------------------- Chapter 1: Introduction to Discrete D ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- uva 11275 3D Triangles
题意:三维空间中,给出两个三角形的左边,问是否相交. 面积法判断点在三角形内: #include<cstdio> #include<cmath> #include<cst ...
- Direct3D 11的流水线
流水线 流水线(Pipeline)是理解D3D必须要掌握的概念. 整个流水线有很多步骤,有的步骤是固定功能,不用怎么配置,有的步骤是要写代码的,也就是所谓的着色器程序(Shader). 一般来说,将流 ...
- OpenGL - Tessellation Shader 【转】
http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html Patch is just an ordered list of vertices (在tes ...
随机推荐
- String对象的属性和方法
String对象的属性和方法 创建字符串的两种方法: 1.直接量:var str = ""; 2.字符串对象创建: new String(""); Stri ...
- Mybatis逆向工程的配置
源码github下载地址:https://github.com/wcyong/mybatisGeneratorCustom.git 参考文章:https://www.cnblogs.com/whgk/ ...
- win10连接宽带,拨号提示错误720:不能建立到远程计算机的连接,解决方法
使用账号密码登录时,一直报720错误.解决方法是卸载以下IP驱动.卸载之后重新连接就正常了.亲测有效
- Git自动化合并多个Commit
目录 git rebase逻辑 git editor的修改 处理git-rebase-todo文件 Python实现 当我们有多个commit或者从开源处拿到多个commit时,想合成一个commit ...
- MySQL Hardware--CentOS 6修改CPU性能模式
cpufrequtils命令 ## 安装: yum install cpufrequtils ## 查看CPU信息: cpufreq-info -m 输出CPU信息为: analyzing CPU : ...
- openstack--10--知识点补充
关于uuid和Fernet方式比较 提供令牌有四种方式[fernet|pkiz|pki|uuid]默认是uuid.
- FPGA Asynchronous FIFO设计思路
FPGA Asynchronous FIFO设计思路 将一个多位宽,且在不停变化的数据从一个时钟域传递到另一个时钟域是比较困难的. 同步FIFO的指针比较好确定,当FIFO counter达到上限值时 ...
- 调整Eclipse中代码字体字号
Window-->preferences-->general-->appearence-->Colors and fonts中的Basic节点选text font,Edit一下
- 远程访问服务器tensorboard
转自https://blog.csdn.net/Hansry/article/details/83004648 1. 在登录远程服务器的时候使用命令: : server_name@server.add ...
- 视频流PS,PS封装H264
出处: ISOIEC 13818-1 PS流: PS流由PSGOP组成,每个PSGOP是由I帧起始的多帧集合,每个GOP之间没有相互依赖信息,可以剪切拼接. | PSGOP0 | PSGOP1 | P ...