• Advertisement
Sign in to follow this  

Calculate the Angle Bisectors to get the Incenter of a Triangle

This topic is 440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

i am currently trying to calculate the incenter of a triangle. I read about this here: http://www.mathopenref.com/triangleincircle.html

 

My current problem is to calculate the angle bisectors. My triangle consists of three vertices (x and y coordinate). My current approach looks like this:

            Point v1 = listOfVertices[0];
            Point v2 = listOfVertices[1];
            Point v3 = listOfVertices[2];

            double a = getLengthOfLine(new Line(v1, v2));
            double b = getLengthOfLine(new Line(v2, v3));
            double c = getLengthOfLine(new Line(v3, v1));

            double angle = Math.Acos((a * a - b * b - c * c) / (-2.0 * b * c)) / 2.0;
            double slope = Math.Tan(angle);
            double yPart = v2.Y - slope * v2.X;

            Line bisecLine1 = new Line(v2, new Point(v2.X + 100, (int)((v2.X + 100) * slope + yPart)));

            double angle2 = Math.Acos((b * b - a * a - c * c) / (-2.0 * a * c)) / 2.0;
            double slope2 = Math.Tan(angle2);
            double yPart2 = v1.Y - slope2 * v1.X;

            Line bisecLine2 = new Line(v1, new Point(v1.X - 100, (int)((v1.X - 100) * slope + yPart)));

But as soon as i change my vertice order from this:

            listOfVertices = new List<Point>()
            {
                new Point(200,100),
                new Point(25,75),
                new Point(75,200)
            };

to something else, my code doesn't work anymore (e.g. produces some weird lines), this is what i get with the code above (works as expected i think):

 

incircle.png

 

How can i determine, which side is a,b,c and which vertex belongs to which side?

 

Edit: Looks like i have some other problems in my bisector code too :(

incircle.png

Edited by IsItSharp

Share this post


Link to post
Share on other sites
Advertisement
Ditch angles. They are a lot of trouble.

Embrace complex numbers! You can identify a point (x,y) with the complex number x+i*y, and similarly use complex numbers to represent vectors. You end up with cleaner code that works:
 
#include <iostream>
#include <complex>

typedef std::complex<float> C;

C normalize(C z) {
  return z / std::abs(z);
}

float dot(C a, C b) {
  return (a * std::conj(b)).real(); // Probably not the best implementation, but this one is cute and compact
}

struct Ray {
  C point, direction;
  
  Ray(C point, C direction) : point(point), direction(direction) {
  }
};

Ray bisector(C a, C b, C c) { // bisector at vertex b
  C bc = normalize(c - b);
  C ba = normalize(a - b);

  return Ray(b, normalize(bc + ba));
}

C intersection(Ray r1, Ray r2) {
  C r2_normal = r2.direction * C(0.0f, 0.1f);
  
  float t = dot(r2.point - r1.point, r2_normal) / dot(r1.direction, r2_normal);

  return r1.point + t * r1.direction;
}

C incenter(C a, C b, C c) {
  return intersection(bisector(a, b, c), bisector(b, c, a));
}

int main() {
  std::cout << incenter(C(0.0f, 1.0f), C(1.0f, 0.0f), C(0.0f, 0.0f)) << '\n';
}

Share this post


Link to post
Share on other sites

Ditch angles. They are a lot of trouble.

Embrace complex numbers! You can identify a point (x,y) with the complex number x+i*y, and similarly use complex numbers to represent vectors. You end up with cleaner code that works:


QTF

Not only will this simplify your code and be less buggy but usually runs faster too.

Share this post


Link to post
Share on other sites
If I could remember my Geometry from 25 years ago, I would have written:

C incenter(C a, C b, C c) {
  float l_a = std::abs(b-c);
  float l_b = std::abs(c-a);
  float l_c = std::abs(a-b);
  return (l_a * a + l_b * b + l_c * c) / (l_a + l_b + l_c);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement