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

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>
            国产精品高潮在线| 亚洲第一区中文99精品| 国产精品一区二区a| 欧美视频1区| 欧美午夜精品理论片a级大开眼界| 免费观看亚洲视频大全| 欧美另类99xxxxx| 欧美午夜电影完整版| 国产精品网站在线| 精品电影一区| 日韩一级大片| 欧美一区二区视频97| 老鸭窝毛片一区二区三区| 欧美韩日一区| 亚洲淫片在线视频| 狂野欧美性猛交xxxx巴西| 欧美激情网站在线观看| 国产精品视频网址| 亚洲电影观看| 亚洲一区一卡| 免费不卡中文字幕视频| 日韩亚洲欧美在线观看| 欧美一区二区黄色| 欧美日韩伦理在线免费| 国语自产精品视频在线看抢先版结局 | 欧美成人黄色小视频| 亚洲成人在线网| 一本色道久久综合亚洲91| 欧美在线播放| 欧美日韩国产美| 激情视频一区| 香港久久久电影| 91久久久在线| 久久福利毛片| 欧美三级在线| 亚洲人屁股眼子交8| 久久精品盗摄| 亚洲天堂成人| 欧美激情无毛| 亚洲国产欧美日韩| 久久久久久69| 亚洲一区二区三区在线观看视频| 久久综合激情| 国产主播精品| 欧美一区二区视频网站| 99精品国产在热久久婷婷| 另类av导航| 在线看片欧美| 免费在线成人| 久久精品道一区二区三区| 国产精品腿扒开做爽爽爽挤奶网站| 亚洲免费电影在线| 欧美激情一二三区| 麻豆精品视频| 亚洲第一页中文字幕| 久久免费高清| 久久三级福利| 亚洲国产成人av在线| 久久综合给合久久狠狠色| 久久激情视频| 欲香欲色天天天综合和网| 久久―日本道色综合久久| 久久精品国产99国产精品澳门| 国产美女精品免费电影| 欧美一区二区在线免费观看| 亚洲一区二区三区影院| 国产日韩成人精品| 久久综合给合| 久久伊人一区二区| 亚洲国产精品一区二区www| 欧美成人日韩| 欧美日韩国产高清视频| 一区二区三区视频免费在线观看| 亚洲日本中文字幕| 欧美视频国产精品| 久久av一区二区| 久久精品国产在热久久 | 国产欧美日韩在线 | 欧美高清视频一区二区| 欧美大片一区二区| 亚洲天堂成人在线观看| 欧美日韩理论| 亚洲精品美女91| 亚洲日本免费电影| 国产精品久久久久久久久久久久| 亚洲专区在线视频| 欧美一区二区三区在线| 亚洲欧洲精品一区二区三区波多野1战4| 最新亚洲电影| 国产精品一区在线观看你懂的| 久久久中精品2020中文| 欧美成人一品| 香港久久久电影| 猫咪成人在线观看| 欧美一区国产二区| 久久免费黄色| 亚洲一区二区三区高清| 久久国内精品自在自线400部| 亚洲乱码精品一二三四区日韩在线 | 亚洲一二三区在线观看| 亚洲第一页中文字幕| 一区二区三区欧美| 亚洲大胆av| 亚洲一区在线播放| 亚洲欧洲日韩女同| 欧美制服丝袜| 亚洲一级片在线看| 久久天天狠狠| 欧美专区在线观看| 欧美日本网站| 欧美不卡视频一区发布| 国产精品久久久久久久久搜平片| 快射av在线播放一区| 国产精品久久波多野结衣| 欧美激情a∨在线视频播放| 国产欧美一区二区三区久久人妖| 亚洲欧洲精品成人久久奇米网 | 欧美成人第一页| 国产毛片一区| 亚洲一区二区三区免费观看| 亚洲激情电影在线| 久久国产精品第一页| 午夜精品久久久久久久男人的天堂| 蜜臀av一级做a爰片久久| 久久免费偷拍视频| 国产日韩欧美精品一区| 一区二区三区日韩精品视频| 亚洲三级网站| 欧美.www| 亚洲国产一区二区a毛片| 在线观看成人网| 久久精品一区二区三区不卡| 欧美一级大片在线观看| 国产精品亚洲片夜色在线| 国产亚洲精品bt天堂精选| 欧美国产日韩一区二区在线观看| 国产一区二区中文字幕免费看| 亚洲一区精品电影| 亚洲欧美久久久久一区二区三区| 欧美日韩国产探花| 久久精品导航| 亚洲自拍偷拍网址| 亚洲专区一区二区三区| 欧美片第一页| 日韩视频在线一区二区| 99在线热播精品免费| 欧美另类女人| 日韩午夜中文字幕| 亚洲一区欧美二区| 国产精品视频导航| 欧美一级专区| 美日韩丰满少妇在线观看| 在线观看中文字幕亚洲| 美女精品国产| 亚洲人体偷拍| 亚洲欧美日本在线| 国内精品久久国产| 免费一级欧美片在线观看| 最近中文字幕日韩精品| 亚洲视频一二| 黄色成人在线| 欧美日本国产精品| 亚洲欧美国产精品专区久久| 久久久欧美一区二区| 亚洲国产精品久久久久秋霞影院| 欧美国产国产综合| 亚洲欧美成人在线| 欧美成人伊人久久综合网| 一区二区三区成人| 国产一级久久| 欧美激情精品久久久久久蜜臀| 99re这里只有精品6| 久久成人一区| 亚洲精品女人| 国产亚洲aⅴaaaaaa毛片| 女人天堂亚洲aⅴ在线观看| 99精品视频免费全部在线| 久久久久久黄| 日韩亚洲精品在线| 国产一区二区三区高清在线观看| 欧美成ee人免费视频| 亚洲欧美日韩成人| 亚洲高清色综合| 久久精品中文字幕一区| 99re6热在线精品视频播放速度| 国产伦精品一区二区三| 欧美国产日韩亚洲一区| 久久不射中文字幕| 在线亚洲电影| 亚洲国产cao| 久久综合福利| 久久精品道一区二区三区| 一区二区三区回区在观看免费视频| 国产在线视频欧美| 国产日韩欧美在线| 国产精品久久精品日日| 欧美精品一区三区在线观看| 久久久久久久综合日本| 亚洲欧美日本在线| 亚洲午夜黄色|