Last active
May 24, 2022 22:50
-
-
Save thu0x31/8f4dcbb8e288953ebad8bb8aae4f25ad to your computer and use it in GitHub Desktop.
Inncircle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function float sidesToIncircleRadius(float A, B, C) { | |
float s = (A + B + C) / 2; | |
float area = sqrt(s * (s-A) * (s-B) * (s-C)); | |
float radius = area / s; | |
return radius; | |
} | |
// https://www.mathopenref.com/coordincenter.html | |
function void drawInnCircle(vector pA, pB, pC; int totalPoints) { | |
// 頂点A,B,Cに対向する辺の長さ | |
float A = distance(pB, pC); | |
float B = distance(pA, pC); | |
float C = distance(pA, pB); | |
//xz | |
vector pos = set( | |
(A * pA.x + B * pB.x + C * pC.x) / (A + B + C), | |
pA.y, | |
(A * pA.z + B * pB.z + C * pC.z) / (A + B + C) | |
); | |
int centerPointN = addpoint(0, pos); | |
setpointgroup(0, "incircleCenter", centerPointN, 1); | |
float radius = sidesToIncircleRadius(A, B, C); | |
float TWO_PI = 3.141592 * 2; | |
int pointsN[]; | |
for(int i = 0; i <= totalPoints; i++) { | |
vector circlePointsPos = set( | |
pos.x + radius * cos(TWO_PI/totalPoints * i), | |
pos.y, | |
pos.z + radius * sin(TWO_PI/totalPoints * i) | |
); | |
int pointN = addpoint(0, circlePointsPos); | |
setpointattrib(0, "N", pointN, normalize(pos - circlePointsPos)); | |
push(pointsN, pointN); | |
} | |
int incircleN = addprim(0, "polyline", pointsN); | |
setprimgroup(0, "incircle", incircleN, 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment