# calculate Triangle Circumcircle And Inscribed_Circle/InCircle

## Recommended Posts

luckyyyyyy    145
Hi... I want to calculate Triangle Circumcircle And Inscribed_Circle/InCircle in 3D space.... if anybody know C/java code please give me link of that code...remember in 3D space... please help me.. THanks..

##### Share on other sites
VizOne    598
This is how it works in general:

Assuming a triangle consisting of three points A, B and C

Circumcircle:
- find the points halfway on AB and AC (AB being the segment between A and B, AC being the segment between A and C)
- create perpendicular lines on AB and AC in those points
- the intersection between those two lines is the centerpoint of the circumcircle
- the radius is the distance between the center point and any of the three points A, B and C

Incircle:
- create the bisecting line in two points, e.g. A and B
- the intersection of those lines is the center point of the incircle
- the radius is given by the distance between the centerpoint and any of the three triangle sides.

Here's some code (it's in C#, but it should be readable)

Circumcircle:
// lines from a to b and a to cvar AB = B - A;var AC = C - A;// perpendicular vector on trianglevar N = Vector3.Normalize(Vector3.Cross(AB, AC));// find the points halfway on AB and ACvar halfAB = A + AB*0.5f;var halfAC = A + AC*0.5f;// build vectors perpendicular to ab and acvar perpAB = Vector3.Cross(AB, N);var perpAC = Vector3.Cross(AC, N);// find intersection between the two lines// D: halfAB + t*perpAB// E: halfAC + s*perpACvar center = LineLineIntersection(halfAB, perpAB, halfAC, perpAC);// the radius is the distance between center and any point// distance(A, B) = length(A-B)var radius = Vector3.Distance(center, A);

Incircle
// create bisecting lines in a and bvar AB = Vector3.Normalize(B - A);var AC = Vector3.Normalize(C - A);var bisectADir = AB + AC;var BA = Vector3.Normalize(A - B);var BC = Vector3.Normalize(C - B);var bisectBDir =BA + BC;// find intersection => center point of incirclevar center = LineLineIntersection(A, bisectADir, B, bisectBDir);// find distance to any side => radiusvar radius = PointLineDistance(center, A, B - A);

And the two helper methods LineLineIntersection and PointLineDistance:

/// <summary>/// Calculates the intersection point between two lines, assuming that there is such a point./// </summary>/// <param name="originD">The origin of the first line</param>/// <param name="directionD">The direction of the first line.</param>/// <param name="originE">The origin of the second line.</param>/// <param name="directionE">The direction of the second line.</param>/// <returns>The point at which the two lines intersect.</returns>Vector3 LineLineIntersection(Vector3 originD, Vector3 directionD, Vector3 originE, Vector3 directionE) {  directionD.Normalize();  directionE.Normalize();  var N = Vector3.Cross(directionD, directionE);  var SR = originD - originE;  var absX = Math.Abs(N.X);  var absY = Math.Abs(N.Y);  var absZ = Math.Abs(N.Z);  float t;  if (absZ > absX && absZ > absY) {    t = (SR.X*directionE.Y - SR.Y*directionE.X)/N.Z;  } else if (absX > absY) {    t = (SR.Y*directionE.Z - SR.Z*directionE.Y)/N.X;  } else {    t = (SR.Z*directionE.X - SR.X*directionE.Z)/N.Y;  }  return originD - t*directionD;}/// <summary>/// Calculates the distance between a point and a line./// </summary>/// <param name="P">The point.</param>/// <param name="S">The origin of the line.</param>/// <param name="D">The direction of the line.</param>/// <returns>/// The distance of the point to the line./// </returns>float PointLineDistance(Vector3 P, Vector3 S, Vector3 D) {  D.Normalize();  var SP = P - S;  return Vector3.Distance(SP, Vector3.Dot(SP, D)*D);}

Vector3 is some 3D float vector implementation providing default vector operations such as addition, scaling, dot product and cross product.

Here's a screenshot of an app using the code above:

luckyyyyyy    145
thank u very much sir.....

its gr8.....

---------------------

-----------------------------

// converts trilinear coordinates to Cartesian coordinates relative
// to the incenter; thus, the incenter has coordinates (0.0, 0.0)
public Coordinate toCartesian(double alpha, double beta, double gamma)
{
double area = getArea();
double as = getSideA(), bs = getSideB(), cs = getSideC();
double r = 2 * area / (as + bs + cs);
double k = 2 * area / (as*alpha + bs*beta + cs*gamma);
double cosC = Math.cos(getAngleC()), sinC = Math.sin(getAngleC());

//Look At here..
//this routine is very very simple...but here how can we find Z coordinate...here we only need a one formula for determine the Z coordinate...can we fine Z??????

//-------------------------------------------------------------------

double x = (k*beta - r + (k*alpha - r)*cosC) / sinC;
double y = k*alpha - r;

double z = ???????????????????????????????????????????

//-------------------------------------------------------------------

return new Coordinate(x, y);
}

public Circle getCircumcircle()
{
double cosA = Math.cos(getAngleA());
double cosB = Math.cos(getAngleB());
double cosC = Math.cos(getAngleC());
Coordinate center = toCartesian(cosA, cosB, cosC);
double as = getSideA(), bs = getSideB(), cs = getSideC();
double s = 0.5 * (as + bs + cs);
double radius = (as * bs * cs) / (4 * Math.sqrt(
s * (as + bs - s) * (as + cs - s) * (bs + cs - s)));
}

public Circle getIncircle()
{
Coordinate center = toCartesian(1.0, 1.0, 1.0);
double as = getSideA(), bs = getSideB(), cs = getSideC();
double semiperimeter = 0.5 * (as + bs + cs);
double radius = getArea() / semiperimeter;
}

[/edit]

thanks..