青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Point in triangle test

LINK: http://www.blackpawn.com/texts/pointinpoly/default.html

Same Side Technique

A common way to check if a point is in a triangle is to find the vectors connecting the point to each of the triangle's three vertices and sum the angles between those vectors. If the sum of the angles is 2*pi then the point is inside the triangle, otherwise it is not. It works, but it is very slow. This text explains a faster and much easier method.

First off, forgive the nasty coloring. I'm really sorry about it. Honest.

Okay, A B C forms a triangle and all the points inside it are yellow. Lines AB, BC, and CA each split space in half and one of those halves is entirely outside the triangle. This is what we'll take advantage of.

For a point to be inside the traingle A B C it must be below AB and left of BC and right of AC. If any one of these tests fails we can return early.

But, how do we tell if a point is on the correct side of a line? I'm glad you asked.


If you take the cross product of [B-A] and [p-A], you'll get a vector pointing out of the screen. On the other hand, if you take the cross product of [B-A] and [p'-A] you'll get a vector pointing into the screen. Ah ha! In fact if you cross [B-A] with the vector from A to any point above the line AB, the resulting vector points out of the screen while using any point below AB yields a vector pointing into the screen. So all we need to do to distinguish which side of a line a point lies on is take a cross product.

The only question remaining is: how do we know what direction the cross product should point in? Because the triangle can be oriented in any way in 3d-space, there isn't some set value we can compare with. Instead what we need is a reference point - a point that we know is on a certain side of the line. For our triangle, this is just the third point C.

So, any point p where [B-A] cross [p-A] does not point in the same direction as [B-A] cross [C-A] isn't inside the triangle. If the cross products do point in the same direction, then we need to test p with the other lines as well. If the point was on the same side of AB as C and is also on the same side of BC as A and on the same side of CA as B, then it is in the triangle.

Implementing this is a breeze. We'll make a function that tells us if two points are on the same side of a line and have the actual point-in-triangle function call this for each edge.

function SameSide(p1,p2, a,b)
            cp1 = CrossProduct(b-a, p1-a)
            cp2 = CrossProduct(b-a, p2-a)
            if DotProduct(cp1, cp2) >= 0 then return true
            else return false
            function PointInTriangle(p, a,b,c)
            if SameSide(p,a, b,c) and SameSide(p,b, a,c)
            and SameSide(p,c, a,b) then return true
            else return false
            

It's simple, effective and has no square roots, arc cosines, or strange projection axis determination nastiness.



Barycentric Technique

The advantage of the method above is that it's very simple to understand so that once you read it you should be able to remember it forever and code it up at any time without having to refer back to anything. It's just - hey the point has to be on the same side of each line as the triangle point that's not in the line. Cake.

Well, there's another method that is also as easy conceptually but executes faster. The downside is there's a little more math involved, but once you see it worked out it should be no problem.

So remember that the three points of the triangle define a plane in space. Pick one of the points and we can consider all other locations on the plane as relative to that point. Let's go with A -- it'll be our origin on the plane. Now what we need are basis vectors so we can give coordinate values to all the locations on the plane. We'll pick the two edges of the triangle that touch A, (C - A) and (B - A). Now we can get to any point on the plane just by starting at A and walking some distance along (C - A) and then from there walking some more in the direction (B - A).

With that in mind we can now describe any point on the plane as

    P = A + u * (C - A) + v * (B - A)

Notice now that if u or v < 0 then we've walked in the wrong direction and must be outside the triangle. Also if u or v > 1 then we've walked too far in a direction and are outside the triangle. Finally if u + v > 1 then we've crossed the edge BC again leaving the triangle.

Given u and v we can easily calculate the point P with the above equation, but how can we go in the reverse direction and calculate u and v from a given point P? Time for some math!

    P = A + u * (C - A) + v * (B - A)       // Original equation
(P - A) = u * (C - A) + v * (B - A)     // Subtract A from both sides
v2 = u * v0 + v * v1                    // Substitute v0, v1, v2 for less writing
// We have two unknowns (u and v) so we need two equations to solve
// for them.  Dot both sides by v0 to get one and dot both sides by
// v1 to get a second.
(v2) . v0 = (u * v0 + v * v1) . v0
(v2) . v1 = (u * v0 + v * v1) . v1
// Distribute v0 and v1
v2 . v0 = u * (v0 . v0) + v * (v1 . v0)
v2 . v1 = u * (v0 . v1) + v * (v1 . v1)
// Now we have two equations and two unknowns and can solve one
// equation for one variable and substitute into the other.  Or
// if you're lazy like me, fire up Mathematica and save yourself
// some handwriting.
Solve[v2.v0 == {u(v0.v0) + v(v1.v0), v2.v1 == u(v0.v1) + v(v1.v1)}, {u, v}]
u = ((v1.v1)(v2.v0)-(v1.v0)(v2.v1)) / ((v0.v0)(v1.v1) - (v0.v1)(v1.v0))
v = ((v0.v0)(v2.v1)-(v0.v1)(v2.v0)) / ((v0.v0)(v1.v1) - (v0.v1)(v1.v0))

Here's an implementation in Flash that you can play with. :)

// Compute vectors
                v0 = C - A
                v1 = B - A
                v2 = P - A
                // Compute dot products
                dot00 = dot(v0, v0)
                dot01 = dot(v0, v1)
                dot02 = dot(v0, v2)
                dot11 = dot(v1, v1)
                dot12 = dot(v1, v2)
                // Compute barycentric coordinates
                invDenom = 1 / (dot00 * dot11 - dot01 * dot01)
                u = (dot11 * dot02 - dot01 * dot12) * invDenom
                v = (dot00 * dot12 - dot01 * dot02) * invDenom
                // Check if point is in triangle
                return (u > 0) && (v > 0) && (u + v < 1)
                

The algorithm outlined here follows one of the techniques described in Realtime Collision Detection. You can also find more information about Barycentric Coordinates at Wikipedia and MathWorld.

posted on 2009-12-01 16:58 zmj 閱讀(1421) 評論(0)  編輯 收藏 引用

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲欧美日韩一区二区在线 | 免费成人高清| 亚洲一区二区精品在线观看| 亚洲欧洲精品一区二区| 亚洲国产一区二区a毛片| 亚洲第一黄网| 最新亚洲一区| 亚洲一区二区三区乱码aⅴ蜜桃女| 亚洲免费视频一区二区| 先锋a资源在线看亚洲| 久久国产精品一区二区三区四区| 老司机午夜精品视频在线观看| 欧美电影资源| 一本大道av伊人久久综合| 亚洲专区在线| 你懂的视频欧美| 欧美体内she精视频| 国产在线播放一区二区三区| 亚洲欧洲在线看| 午夜精品久久久久久久蜜桃app| 久久久久久久综合日本| 亚洲电影av| 亚洲欧洲在线一区| 亚洲香蕉伊综合在人在线视看| 久久成人羞羞网站| 欧美日韩一区二区精品| 精品福利电影| 一本一本久久| 麻豆精品精华液| 99热这里只有精品8| 欧美在线观看一区二区| 欧美精品在线观看播放| 国产一区二三区| 一区二区三区视频在线看| 欧美综合国产| 一本色道久久综合| 蜜乳av另类精品一区二区| 国产精品三上| 一区二区三区四区国产精品| 欧美韩国日本综合| 欧美影视一区| 国产精品理论片| 99在线热播精品免费| 免费一区视频| 欧美淫片网站| 国产精品一区二区黑丝| 夜夜嗨av一区二区三区网页| 欧美阿v一级看视频| 欧美在线三区| 国产亚洲精品aa午夜观看| 亚洲淫性视频| 亚洲精品日韩一| 免费在线看一区| 亚洲欧美成人一区二区三区| 国内精品一区二区| 亚洲综合日韩在线| 亚洲黄色性网站| 久久久噜噜噜久噜久久 | 99视频在线观看一区三区| 欧美成人国产一区二区| 欧美一区二区三区男人的天堂 | 一区二区av在线| 欧美顶级少妇做爰| 久久综合影音| 亚洲欧洲在线一区| 亚洲高清免费视频| 欧美福利电影在线观看| 在线欧美福利| 欧美承认网站| 欧美91大片| 99爱精品视频| 亚洲图片激情小说| 国产精品一级久久久| 久久久亚洲精品一区二区三区 | 国产精品av免费在线观看| 亚洲无线视频| 中文无字幕一区二区三区| 国产精品成人观看视频免费| 亚洲影院色无极综合| 亚洲一区二区在线观看视频| 国产日韩精品一区二区三区在线| 久久久久久久久久久成人| 久久久水蜜桃av免费网站| 亚洲人成7777| 一区二区三区精品国产| 国产日韩欧美精品在线| 久久躁狠狠躁夜夜爽| 欧美成人精品高清在线播放| 中文久久精品| 欧美一区精品| 日韩视频国产视频| 亚洲一区二区久久| 一区二区三区在线不卡| 亚洲精选大片| 激情欧美一区二区三区在线观看 | 欧美一区二区三区免费观看视频 | 国产精品一区久久久| 久久精品国产在热久久| 男人的天堂亚洲在线| 亚洲制服av| 男同欧美伦乱| 久久久久久久久久久久久9999| 午夜一区不卡| 日韩亚洲欧美一区| 狠狠干成人综合网| 欧美激情一区二区三区成人| 国产精品久久9| 欧美国产日韩精品免费观看| 国产精品久久久久99| 欧美va亚洲va香蕉在线| 国产精品每日更新在线播放网址| 免费欧美电影| 国产精品色午夜在线观看| 久久天天躁狠狠躁夜夜爽蜜月| 欧美国产视频日韩| 久久精品人人| 国产精品v亚洲精品v日韩精品| 久久午夜av| 国产精品久久久久久久久久免费看 | 国语自产精品视频在线看一大j8 | 亚洲欧美不卡| 男女av一区三区二区色多| 午夜在线精品偷拍| 欧美国产高清| 美女免费视频一区| 国产乱子伦一区二区三区国色天香 | 欧美mv日韩mv国产网站app| 午夜精品区一区二区三| 欧美黄色网络| 欧美黄色aaaa| 在线观看亚洲视频| 欧美一区二区三区电影在线观看| 亚洲国产高清一区二区三区| 欧美淫片网站| 久久久噜久噜久久综合| 国产精品午夜电影| 亚洲一区二区三区中文字幕| 亚洲欧美偷拍卡通变态| 欧美先锋影音| 日韩亚洲一区二区| 亚洲一区二区三区四区中文| 欧美人在线观看| 日韩视频一区二区三区| 亚洲视频播放| 国产精品久久久久久久app| 亚洲美女中出| 亚洲一区二区欧美| 国产精品入口尤物| 亚洲欧美中文另类| 久久精品亚洲一区| 国产亚洲精品v| 久久久青草青青国产亚洲免观| 欧美v亚洲v综合ⅴ国产v| 亚洲精品护士| 欧美性大战xxxxx久久久| 午夜精品在线看| 久久亚洲精品网站| 亚洲国产精品专区久久| 欧美freesex8一10精品| 欧美日韩中文另类| 亚洲一区二区成人| 久久一区二区精品| 亚洲人体大胆视频| 欧美精品精品一区| 亚洲一区二区在线看| 免费观看成人www动漫视频| 最新日韩在线| 国产精品视频大全| 久久亚洲私人国产精品va| 亚洲人成在线播放网站岛国| 午夜精品国产更新| 在线观看91久久久久久| 欧美日韩免费一区| 性色av一区二区三区| 欧美国产日韩精品| 香蕉免费一区二区三区在线观看| 影视先锋久久| 国产精品欧美风情| 欧美激情亚洲| 欧美在线啊v| 日韩写真视频在线观看| 久久久中精品2020中文| 夜夜精品视频| 伊人色综合久久天天| 欧美日韩精品免费在线观看视频| 性欧美大战久久久久久久免费观看| 亚洲第一精品电影| 久久久国产一区二区| 亚洲午夜精品在线| 亚洲第一页中文字幕| 国产精品中文字幕欧美| 欧美日韩ab| 欧美激情精品| 久久婷婷麻豆| 欧美一区二区三区在线免费观看| 一区二区不卡在线视频 午夜欧美不卡在| 蜜桃av一区二区三区| 久久精品国产综合| 亚洲在线观看视频| 日韩视频一区二区|