You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

28 lines
776 B

void PolygonEdgeDistance_float(float2 P, float n, out float distance) {
float R = 0.5;
float TAU = 2.0 * 3.14159265359;
n = round(n);
float rP = length(P);
float thetaP = atan2(P.y, P.x);
thetaP = fmod(thetaP + TAU, TAU);
float angleStep = TAU / n;
int k = int(floor(thetaP / angleStep)) % int(n);
float theta_k = k * angleStep;
float2 p1 = R * float2(cos(theta_k), sin(theta_k));
float2 p2 = R * float2(cos(theta_k + angleStep), sin(theta_k + angleStep));
float2 edgeDir = p2 - p1;
float2 toP = P - p1;
float edgeLength = length(edgeDir);
float t = dot(toP, edgeDir) / (edgeLength * edgeLength);
t = saturate(t);
float2 closestPoint = p1 + t * edgeDir;
distance = length(P - closestPoint);
}