Tuesday, September 13, 2022
HomeGame Developmentunity - How do I decide if a given grid coordinate is...

unity – How do I decide if a given grid coordinate is absolutely inside a triangle?


I am utilizing Unity’s Grid system, and I am drawing triangles on the grid. Every triangle is recorded as a Vector2Int origin level and a Record of three Vector2Ints recording its relative vertices, as an illustration:

origin = new Vector2Int(0, 0);

relativeVertexPositions = new Record<Vector2Int>() {
    new Vector2Int(-3, 0),
    new Vector2Int(3, 0),
    new Vector2Int(0, -3),
};

It is a bit bizarre that the origin is on the underside of the triangle nevertheless it’s for an in-game cause – a personality is supposed to face there. I can redefine the origin level to elsewhere within the triangle if wants be.

The above code would generate a triangle as follows:

enter image description here

I am utterly caught on the best way to discover out whether or not a given Vector2Int (i.e. an integer x,y place – that is presumably an engine-agnostic downside) is absolutely contained in the triangle. These 4 squares are marked in blue on my sketch. Triangles differ in dimension, however all the time observe this similar logic – in a size-4 triangle, there shall be 9 inner x,y coordinates; in a size-5 triangle, there shall be 16, and so forth.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments