Finder what point is to the left of a line/point after spinning it

坚强是说给别人听的谎言 提交于 2019-11-26 22:11:14

问题


I am currently trying to write a shader in unity that draws a triangular pattern around countries in a risk-styled game if both countries are not owned by the same player (visual aid to see your borders).

Right now, I'm having an issue with making the shader set the countries properly.

It always sets country 0 to the left, and country 1 to the right - country 0 and 1 are set programically.

The line, a border, can be between 0 and 359 degrees.

How I find the countries 0 and 1 is I draw 3 points to the left and right of the midpoint of the line, one .01f, one .1f and one 1f away from the midpoints in each direction, then spin them around the midpoint to the appropriate location.

After that I do an even-odd check to see if the points are inside or outside of each country, and compare the weight results (closest gets 3 points, mid gets 2, furthest gets 1, just in case someone builds a really screwed up country that flanks the other country).

In my test map, a close to equally sliced octagon, the borders showed up correctly (after I reversed the positions of country 0 and 1 in the event the angle was over 90 and less then or equal 180). Worked without a flaw, but in other maps it doesn't work very well.

Everything but the country allocation works well, so I'm curious if anyone knows of a better way to figure out which point is to the left or a spun line, or a better conceptual way to handle this.

That above is basically when I'm doing, red being left right being blue, then I'm just checking 3 different spots then weighing in the lefts and rights found with even/odding it into the appropriate countries (one at +/- .01, the other at +/- .1 and the third 1, in case of even/odd rounding issues with closeness).

I then flip them if I find that country A is to the right, as it is on the left according to the angles I had draw. (my shader renders left first and right second, hence why I do this).


回答1:


  1. which way is left/right on a line?

    From last edit is this not your case. Why not use dot product?

    So if the line goes in -x direction the result is negative and if in the +x direction then the result is positive. if the result is zero that means the line goes up or down only or it is juts a point. If you need specific direction instead of left/right then use appropriate a vector instead of x axis.

    • dot(a,b)=a.x*b.x+a.y*b.y in 2D
    • dot(a,b)=a.x*b.x+a.y*b.y+a.z*b.z in 3D

    Image is relevant for cases where a vector is in unit size in that case the result of dot is perpendicular projection of b into a just like on image

  2. on which side is some point?

    I think this is what you need.

    As you can see if you handle line (P0,P1) and point P you want to classify as triangle then its polygon winding determines also the side of the line. So for implicit axis directions:

    • CW(clockwise) polygon winding means right side of the line
    • CCW(counter-clockwise) polygon winding means left side of the line

    How to get winding? ... simply compute normal vector and get its Z coordinate. Its polarity (sign) determines winding (CW/CCW or the other way around depends on the coordinate system).

    • normal vector is computed as cross product of the two vertices of triangle (P1-P0)x(P-P1)

    No need to compute other axises just the z so:

    • normal.z = ((P1.x-P0.x)*(P.y-P1.y)) - ((P1.y-P0.y)*(P.x-P1.x))

    Now just do if (normal.z<0) ... else ... it should never be zero unless you call it for point on the line or the line is a point ... look here at similar question: Determine rotation direction /toward/ variable point on a circle



来源:https://stackoverflow.com/questions/27478243/finder-what-point-is-to-the-left-of-a-line-point-after-spinning-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!