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

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 閱讀(1425) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产精品99久久不卡二区| 国产精品国产成人国产三级| 久久亚洲私人国产精品va| 亚洲欧美日韩国产一区| 夜夜嗨一区二区| 亚洲综合第一页| 久久高清一区| 欧美va亚洲va日韩∨a综合色| 久久蜜桃精品| 亚洲国产婷婷综合在线精品| 亚洲经典自拍| 亚洲欧美中文日韩在线| 欧美一级专区| 女生裸体视频一区二区三区| 欧美视频日韩视频| 一区二区三区在线免费播放| 99www免费人成精品| 欧美一区二区成人| 欧美高清视频在线观看| 亚洲私人影吧| 欧美v日韩v国产v| 国产欧美日韩视频一区二区| 亚洲理伦在线| 久久久久久黄| 一区二区日韩精品| 亚洲欧美另类国产| 国产综合在线视频| 夜夜精品视频| 久久综合伊人| 亚洲伊人色欲综合网| 久久综合久久综合久久| 国产精品视频免费观看www| 亚洲国产精品精华液2区45| 亚洲一区www| 欧美激情中文不卡| 欧美在线观看视频| 国产精品久久久久av免费| 亚洲国产日日夜夜| 久久久国产成人精品| 亚洲视频播放| 欧美日韩另类综合| 亚洲日韩视频| 欧美成人日本| 久久久久久欧美| 国产精品久久久久国产a级| 一本色道久久综合狠狠躁篇怎么玩 | 国产亚洲精品aa| 亚洲少妇一区| 亚洲第一综合天堂另类专| 午夜精品免费视频| 国产精品播放| 国产精品99久久久久久久女警| 麻豆成人在线播放| 午夜激情久久久| 国产精品激情| 亚洲一区免费视频| 亚洲精品一二区| 欧美高清在线一区| 亚洲日本免费电影| 亚洲国产欧美不卡在线观看| 久久女同互慰一区二区三区| 国产亚洲va综合人人澡精品| 亚洲欧美激情在线视频| 亚洲免费观看高清完整版在线观看| 性欧美xxxx视频在线观看| 国产精品萝li| 欧美一区二区私人影院日本| 日韩午夜三级在线| 欧美一级理论片| 亚洲伊人观看| 国外精品视频| 美女亚洲精品| 欧美国产亚洲精品久久久8v| 亚洲国产影院| 亚洲人成毛片在线播放| 欧美国产91| 亚洲午夜日本在线观看| 亚洲午夜视频在线| 国产精品蜜臀在线观看| 香蕉成人伊视频在线观看| 99re66热这里只有精品3直播| 亚洲国产另类久久精品| 欧美多人爱爱视频网站| 免费中文字幕日韩欧美| 日韩视频一区二区三区在线播放免费观看| 欧美成年人视频网站| 免费成人黄色av| 一区二区欧美激情| 亚洲欧美日韩国产一区| 黄色精品一二区| 91久久精品国产91性色| 国产精品日韩欧美综合| 久久精品国产999大香线蕉| 久久久久久香蕉网| 中文av一区二区| 久久精品综合| 亚洲视频在线一区观看| 午夜在线精品| 日韩视频免费观看高清在线视频 | 亚洲一级影院| 久久精品视频99| 中文亚洲免费| 久久成人国产精品| 一本色道久久88亚洲综合88| 亚洲欧美卡通另类91av| 亚洲精品乱码久久久久久蜜桃91| 一本久道久久综合婷婷鲸鱼| 国产一区二区精品| 99re6这里只有精品视频在线观看| 国产午夜一区二区三区| 亚洲激情一区二区| 欧美精品导航| 亚洲第一视频| 国产日韩三区| 日韩亚洲欧美综合| 亚洲电影免费观看高清完整版在线观看| 日韩天堂在线观看| 亚洲国产日韩欧美综合久久| 亚洲你懂的在线视频| 亚洲视频综合在线| 免费在线观看日韩欧美| 久久精品视频网| 国产精品视频网址| 一本色道久久综合亚洲精品小说| 国产亚洲一区在线| 亚洲一区二区三区精品在线观看| 亚洲国产高清一区| 欧美有码视频| 欧美一区二区三区在线看| 欧美日韩成人综合| 91久久精品日日躁夜夜躁欧美 | 亚洲欧美日韩一区二区在线| 日韩一级成人av| 欧美88av| 欧美激情一二区| 亚洲国产精品悠悠久久琪琪| 欧美亚洲视频在线看网址| 小处雏高清一区二区三区| 欧美福利在线| 欧美三级不卡| 亚洲免费电影在线| 一区二区欧美亚洲| 欧美极品在线观看| 亚洲精品视频在线看| 亚洲乱码精品一二三四区日韩在线| 快播亚洲色图| 亚洲风情亚aⅴ在线发布| 亚洲国产精品一区二区久| 免费一区视频| 亚洲乱码国产乱码精品精可以看 | av成人黄色| 亚洲女人天堂av| 国产日韩精品一区观看| 欧美中文在线观看| 久久综合99re88久久爱| 尤物九九久久国产精品的分类| 久久精品国语| 亚洲国产视频直播| 亚洲字幕在线观看| 国产亚洲网站| 欧美va亚洲va香蕉在线| 日韩亚洲欧美成人| 在线日韩视频| 欧美日韩999| 亚洲砖区区免费| 久久婷婷国产综合精品青草| 在线日韩av片| 国产精品国码视频| 久久久综合精品| 亚洲精品一区二区三区av| 亚洲欧美一区二区精品久久久| 国产精品综合视频| 狼人天天伊人久久| 一区二区欧美日韩视频| 久久综合伊人77777麻豆| 亚洲精品免费观看| 国产精品网站在线观看| 久久天天躁狠狠躁夜夜爽蜜月 | 亚洲精华国产欧美| 久久国产精品一区二区三区| 亚洲日本久久| 国产亚洲欧美日韩一区二区| 欧美超级免费视 在线| 亚洲一区二区毛片| 亚洲国产合集| 久久艳片www.17c.com| 亚洲尤物在线| 亚洲黄色在线| 国产亚洲一区在线播放| 欧美日韩免费观看一区三区| 久久爱另类一区二区小说| 亚洲精品国精品久久99热| 久久免费精品视频| 亚洲欧美国内爽妇网| 99视频有精品| 亚洲国产1区| 国内精品久久久久久久果冻传媒 | 狠狠色狠狠色综合日日五| 欧美久久电影| 欧美国产激情|