Getting slice number of regular polygon from coordinates

64 Views Asked by At

For an n sided polygon divided up into n triangles, i want the triangle "index" from a coordinate inside the polygon

Example:

example

The coordinates at the green dot should give 2, the red dot should give 5 and the blue dot should give 3

Coordinate range is from 0 to diameter of the polygon

The polygon is not necessarily a 6 sided polygon

1

There are 1 best solutions below

0
On BEST ANSWER

Solved it using this(C# code but can probably be converted into math equation)

float arctan = Mathf.Atan(originRelativePos.x / originRelativePos.y);
float sectorDec = arctan / ((2 * Mathf.PI) / sectorCount);
float max = sectorCount / 4;

sectorDec = originRelativePos.x < 0 ? -sectorDec : sectorDec;
sectorDec = sectorDec > 0 ? sectorDec : max * 2 + sectorDec + 1;
sectorDec = originRelativePos.x < 0 ? sectorCount - sectorDec : sectorDec;

int sector = (int)sectorDec;

sector is the number of the sector, originRelativePos is position relative to the origin