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

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


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   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>
            亚洲精品一级| 日韩午夜电影av| 欧美在线啊v一区| 亚洲一区欧美| 国产精品一级| 久久久亚洲一区| 可以看av的网站久久看| 亚洲国产综合视频在线观看| 亚洲国产精品精华液2区45| 女仆av观看一区| 亚洲小说春色综合另类电影| 亚洲欧美国产高清va在线播| 国产一区自拍视频| 欧美黄色大片网站| 欧美日韩中文字幕在线| 性感少妇一区| 欧美a级一区二区| 亚洲视频网站在线观看| 午夜精品久久久久久久99热浪潮| 黄色影院成人| 9l国产精品久久久久麻豆| 国产精品亚洲精品| 欧美福利一区| 国产伦精品一区二区三| 免费久久99精品国产自| 欧美午夜视频网站| 久久色在线观看| 欧美私人啪啪vps| 久久免费视频在线| 欧美三区视频| 男人插女人欧美| 国产精品久久久久av| 欧美1区2区| 国产亚洲综合在线| 亚洲裸体在线观看| 在线观看精品| 亚洲欧美日韩国产成人| 日韩午夜电影av| 久久亚洲美女| 欧美一区二区三区四区夜夜大片| 欧美成人高清视频| 老司机午夜免费精品视频| 欧美色一级片| 亚洲欧洲日本mm| 亚洲电影下载| 欧美中文字幕久久| 久久成人这里只有精品| 欧美三级日本三级少妇99| 欧美激情一区二区三区在线视频观看 | 午夜精品久久久久久久99樱桃| 亚洲欧洲一级| 久久久无码精品亚洲日韩按摩| 欧美一区二区私人影院日本| 欧美日韩一区成人| 91久久久亚洲精品| 91久久久一线二线三线品牌| 久久久噜噜噜久噜久久| 久久久精品视频成人| 国产欧美一级| 亚洲综合首页| 欧美在线一二三四区| 国产精品美女在线观看| 亚洲小说欧美另类婷婷| 亚洲欧美清纯在线制服| 国产精品理论片| 亚洲一区免费网站| 欧美一级大片在线观看| 国产欧美日韩亚洲一区二区三区| 夜夜嗨av一区二区三区免费区| 一区二区三区色| 国产精品久久久久久福利一牛影视 | 最新成人av网站| 浪潮色综合久久天堂| 亚洲大片av| 日韩视频一区| 欧美午夜电影完整版| 亚洲视频一区二区免费在线观看| 亚洲永久免费观看| 国产精品视频一区二区三区| 亚洲女性裸体视频| 免费观看30秒视频久久| 亚洲国产精品ⅴa在线观看| 欧美精品v国产精品v日韩精品| 亚洲精品日韩精品| 亚洲欧美日韩一区二区三区在线观看 | 欧美午夜精品久久久久免费视| 一区二区三区色| 久久青草久久| 亚洲精品一区久久久久久| 欧美日韩国产系列| 欧美一级视频一区二区| 牛牛国产精品| 亚洲一区二区三区精品在线观看| 国产精品实拍| 久久久亚洲成人| 一区二区电影免费观看| 久久精品国产99国产精品澳门| 亚洲激情网址| 国产精品裸体一区二区三区| 久久精品国产99精品国产亚洲性色| 亚洲高清激情| 久久av免费一区| 日韩午夜精品视频| 国内精品视频666| 欧美日韩一区国产| 久久天天躁夜夜躁狠狠躁2022| 一区二区三区不卡视频在线观看| 久久米奇亚洲| 亚洲欧美另类在线观看| 亚洲激情欧美| 国产一区二区三区奇米久涩| 欧美精品乱码久久久久久按摩| 午夜精品久久久久久99热软件| 亚洲国产一区二区三区在线播| 欧美一区网站| 中文国产亚洲喷潮| 亚洲激情自拍| 韩国成人精品a∨在线观看| 欧美日韩卡一卡二| 欧美1区免费| 久久黄色影院| 亚洲欧美日韩一区在线观看| 亚洲精品美女在线观看| 欧美高清日韩| 麻豆免费精品视频| 欧美尤物巨大精品爽| 亚洲一区黄色| 一区二区三区视频在线| 亚洲人成亚洲人成在线观看图片| 国产人成一区二区三区影院| 国产精品久久久久久户外露出| 欧美xxx在线观看| 久久亚洲不卡| 久久精品国产一区二区电影| 亚洲欧美电影院| 亚洲男人天堂2024| 亚洲精品美女在线| 亚洲人成在线免费观看| 亚洲国产日韩欧美一区二区三区| 免费看av成人| 欧美激情在线免费观看| 欧美国产视频一区二区| 免费在线一区二区| 欧美激情视频在线播放 | 久久久久久久久综合| 欧美在线啊v一区| 久久不射2019中文字幕| 久久激情婷婷| 久久人人九九| 欧美福利精品| 欧美欧美全黄| 欧美三级日本三级少妇99| 欧美日韩亚洲一区二| 欧美三级中文字幕在线观看| 欧美肉体xxxx裸体137大胆| 国产精品日韩欧美一区二区| 国产日本欧美在线观看| 国模精品娜娜一二三区| 亚洲福利av| 一区二区三区国产精华| 亚洲综合欧美日韩| 久久久久久久久久久成人| 久久亚洲私人国产精品va| 免费不卡在线视频| 亚洲欧洲日韩女同| 亚洲精品小视频| 亚洲午夜在线观看| 久久久久久久久岛国免费| 欧美激情按摩| 一区二区三区四区在线| 欧美一级视频精品观看| 美女精品网站| 欧美香蕉大胸在线视频观看| 国产视频一区免费看| 亚洲精品乱码久久久久| 亚洲一区在线观看视频| 久久婷婷国产综合尤物精品| 欧美凹凸一区二区三区视频| 亚洲精品专区| 久久久精品免费视频| 欧美日韩aaaaa| 狠狠色伊人亚洲综合成人| 一本到12不卡视频在线dvd| 欧美伊人久久| 亚洲精选久久| 久久久久久久尹人综合网亚洲| 欧美三级电影网| 亚洲承认在线| 午夜亚洲激情| 91久久精品美女高潮| 欧美一区二区三区在线看| 欧美精品亚洲| 在线观看欧美日韩国产| 亚洲在线中文字幕| 亚洲福利视频专区| 久久激情综合网| 国产精品一区二区三区乱码| 亚洲免费精彩视频| 欧美国产日韩在线| 久久久久国产一区二区|