Skip to content

Instantly share code, notes, and snippets.

@thu0x31
Created May 21, 2022 17:05
Show Gist options
  • Save thu0x31/d1fd1be1a305d256911b943886bc39ea to your computer and use it in GitHub Desktop.
Save thu0x31/d1fd1be1a305d256911b943886bc39ea to your computer and use it in GitHub Desktop.
arcFrom2Points vex
function int[] arcFrom2Points(vector centerPos; float radius; int totalPoints;
vector startPos, endPos;
string direction) {
float threshold = 0.01;
if(radius < threshold) {
return{};
}
float TWO_PI = PI * 2;
vector v1 = normalize(startPos - centerPos);
vector v2 = normalize(endPos - centerPos);
float startRad = atan2(v1.z, v1.x);
// (atan2(v2.z, v2.x) - startRad) - (atan2(v1.z, v1.x) - startRad) ベクトルを{1,0,0}に初期化する
float rad = startRad*2 - atan2(v2.z, v2.x) - atan2(v1.z, v1.x);
if((rad < 0) && (direction == "Left")) {
rad += TWO_PI;
} else if((rad > 0) && (direction == "Right")) {
rad -= TWO_PI;
}
int points[];
for(int i = 0; i <= totalPoints; i++) {
vector pos = set(
centerPos.x + radius * cos(startRad - i * (rad/totalPoints)),
centerPos.y,
centerPos.z + radius * sin(startRad - i * (rad/totalPoints))
);
int pointN = addpoint(0, pos);
setpointattrib(0, "arcLength", pointN, rad*TWO_PI);
setpointattrib(0, "dictCenter", pointN, normalize(centerPos - pos));
if(direction == "Right") {
setpointattrib(0, "N", pointN, normalize(centerPos - pos));
}
if(direction == "Left") {
setpointattrib(0, "N", pointN, normalize(pos - centerPos));
}
push(points, pointN);
}
// debug
// addprim(0, "polyline", addpoint(0, centerPos + v1 * radius), addpoint(0, centerPos));
// addprim(0, "polyline", addpoint(0, centerPos + v2 * radius), addpoint(0, centerPos));
return points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment