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

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)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   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| 亚洲九九九在线观看| 一区二区三区 在线观看视| 亚洲第一偷拍| 欧美成人午夜| 一区二区久久久久| 一区二区久久久久| 国产精品系列在线| 久久精品理论片| 久久久噜噜噜久噜久久| 亚洲国产导航| 亚洲欧洲久久| 国产乱码精品一区二区三区忘忧草| 性欧美长视频| 久热这里只精品99re8久| 亚洲日韩欧美视频一区| 中文在线不卡视频| 国产一区在线免费观看| 欧美黄免费看| 欧美日韩免费看| 久久影院午夜论| 欧美日韩 国产精品| 欧美一区二区三区电影在线观看| 久久狠狠婷婷| 一区二区欧美视频| 久久精品国产亚洲一区二区三区| 亚洲精品一品区二品区三品区| 99精品视频免费观看视频| 国产女人aaa级久久久级| 欧美大片91| 国产精品视频专区| 亚洲第一区中文99精品| 国产精品欧美日韩久久| 欧美国产高潮xxxx1819| 国产精品一区二区久久久| 欧美sm视频| 国产精品久久久久久av福利软件| 老司机精品久久| 国产精品福利在线观看| 蜜桃av久久久亚洲精品| 欧美先锋影音| 欧美激情欧美狂野欧美精品| 国产精品免费一区二区三区观看| 亚洲第一页中文字幕| 国产伦理一区| 在线视频精品一区| 亚洲精品一区二区三区蜜桃久| 性久久久久久久久| 亚洲与欧洲av电影| 男女精品视频| 久久一二三四| 国产日韩精品一区观看| 日韩网站在线观看| 亚洲精品日韩综合观看成人91| 午夜日韩在线观看| 亚洲免费网址| 欧美日韩国产美女| 亚洲高清久久网| 亚洲国产美女精品久久久久∴| 亚洲免费在线看| 亚洲免费网站| 国产精品高精视频免费| 亚洲精品一区二区三区不| 在线不卡中文字幕播放| 香蕉久久夜色精品国产| 亚洲男女自偷自拍| 欧美视频在线播放| 日韩图片一区| 一区二区冒白浆视频| 欧美激情第二页| 亚洲国产综合91精品麻豆| 最新亚洲激情| 欧美成人激情在线| 亚洲激情影视| 日韩午夜免费视频| 欧美精品九九| 一区二区三区日韩在线观看| 亚洲深夜av| 国产精品久久久久久av下载红粉 | 99亚洲一区二区| 欧美顶级大胆免费视频| 亚洲精品国久久99热| 艳女tv在线观看国产一区| 欧美日韩亚洲高清| 一区二区三区导航| 欧美在线播放一区二区| 国产无一区二区| 久久精品夜色噜噜亚洲a∨ | 先锋影音国产一区| 亚洲在线不卡| 久久av红桃一区二区小说| 久久久久国产一区二区三区四区 | 午夜一区二区三区在线观看| 欧美在线亚洲一区| 国模叶桐国产精品一区| 老司机久久99久久精品播放免费| 久热这里只精品99re8久| 亚洲高清一区二| 欧美人牲a欧美精品| 亚洲无亚洲人成网站77777 | 亚洲欧美日韩国产一区| 国产日韩综合| 女女同性精品视频| 亚洲天堂第二页| 免费在线观看精品| 亚洲视频高清| 国产一区二区精品久久91| 欧美刺激午夜性久久久久久久| 亚洲私人影院| 欧美国产日韩二区| 亚洲欧美www| 亚洲国产精品久久久久秋霞影院| 欧美视频日韩| 欧美wwwwww| 久久av一区二区三区| 亚洲理论在线观看| 欧美成人免费全部| 欧美在线一区二区| av成人免费在线观看| 黄色成人在线| 国产精品久久久久影院色老大| 欧美www视频| 久久精品道一区二区三区| 在线视频中文亚洲| 亚洲人成网站999久久久综合| 久久伊人免费视频| 久久成人国产| 午夜久久一区| 亚洲一区二区三区免费在线观看| 尤物99国产成人精品视频| 国产麻豆综合| 欧美午夜激情在线| 欧美日本精品| 欧美福利一区| 免费观看日韩av| 久久精品国产99| 欧美一区二区三区日韩视频| 亚洲一二三区在线| 一区二区三区四区国产精品| 亚洲精品中文字| 亚洲国产精品视频一区| 欧美国产精品劲爆| 欧美成人69av| 欧美h视频在线| 欧美国产激情| 欧美高清视频一区二区| 欧美成人午夜激情在线| 免费观看30秒视频久久| 久久一区二区三区国产精品 | 久久国内精品视频| 欧美在线视频全部完| 欧美中文在线免费| 久久精品国产亚洲精品| 久久精品99无色码中文字幕 | 日韩视频一区二区在线观看| 亚洲精品1234| 亚洲精品一区在线观看| 99精品久久久| 亚洲一级影院| 午夜精品国产精品大乳美女| 午夜日韩av| 久久久久久一区二区| 另类av导航| 亚洲激情av在线| 一本在线高清不卡dvd| 亚洲一区二区免费视频| 久久福利视频导航| 久久久噜噜噜久噜久久| 欧美大片18| 欧美午夜视频在线| 国产区在线观看成人精品| 一区视频在线| 99成人在线| 欧美亚洲一区二区在线| 老司机午夜精品视频| 亚洲国产精品久久| 亚洲一级网站| 久久久久久久久久看片| 欧美激情综合五月色丁香小说| 国产精品国产馆在线真实露脸| 国产婷婷色一区二区三区| 91久久国产综合久久| 亚洲一区二区精品在线观看| 久久看片网站| aa级大片欧美| 久久久久.com| 欧美午夜寂寞影院| 影音先锋日韩资源| 亚洲女性裸体视频| 亚洲电影毛片| 香港成人在线视频| 欧美精品乱人伦久久久久久| 国产一区二区三区久久悠悠色av | 在线观看精品| 亚洲综合日韩在线| 91久久亚洲| 久久久久久精| 国产日韩精品入口| 在线视频精品一| 欧美www视频|