# Visual C++ - Find Acute, Obtuse or Right Angle Triangle Given 3 points

Here is the Visual C++ source code to find whether it is actue, obtuse or right angle triangle given given 3 points.

The logic is explained below:

Find the slope between the two lines AB and BC and lets call it m1 and m2. To find out the angle between these two lines, use the following formula.

ARCTAN( (m1 - m2) / (1 + m1 * m2))

The result will be in radians and to convert it into angle = Radian * 180 / PI.

## Source Code

#include <iostream>

#include <math.h>

int IsPointInBoundingBox(float x1, float y1, float x2, float y2, float px, float py)

{

float left, top, right, bottom; // Bounding Box For Line Segment

// For Bounding Box

if(x1 < x2)

{

left = x1;

right = x2;

}

else

{

left = x2;

right = x1;

}

if(y1 < y2)

{

top = y1;

bottom = y2;

}

else

{

top = y2;

bottom = y1;

}

if( (px+0.01) >= left && (px-0.01) <= right &&

(py+0.01) >= top && (py-0.01) <= bottom )

{

return 1;

}

else

return 0;

}

int LineSegmentIntersection(float l1x1, float l1y1, float l1x2, float l1y2,

float l2x1, float l2y1, float l2x2, float l2y2,

float *m1, float *c1, float *m2, float *c2,

float* intersection_X, float* intersection_Y)

{

float dx, dy;

dx = l1x2 - l1x1;

dy = l1y2 - l1y1;

*m1 = dy / dx;

// y = mx + c

// intercept c = y - mx

*c1 = l1y1 - *m1 * l1x1; // which is same as y2 - slope * x2

dx = l2x2 - l2x1;

dy = l2y2 - l2y1;

*m2 = dy / dx;

// y = mx + c

// intercept c = y - mx

*c2 = l2y1 - *m2 * l2x1; // which is same as y2 - slope * x2

if( (*m1 - *m2) == 0)

return 0;

else

{

*intersection_X = (*c2 - *c1) / (*m1 - *m2);

*intersection_Y = *m1 * *intersection_X + *c1;

}

if(IsPointInBoundingBox(l1x1, l1y1, l1x2, l1y2, *intersection_X, *intersection_Y) == 1 && IsPointInBoundingBox(l2x1, l2y1, l2x2, l2y2, *intersection_X, *intersection_Y) == 1)

{

return 1;

}

else

return 0;

}

int Calculate_Triangle_Type(float ax, float ay, float bx, float by, float cx, float cy, float *angleA, float *angleB, float *angleC)

{

float m1, c1, m2, c2;

float intersection_X, intersection_Y;

// Find the angle between Line AB and BC

LineSegmentIntersection(ax, ay, bx, by, bx, by, cx, cy,

&m1, &c1, &m2, &c2, &intersection_X, &intersection_Y);

*angleB = atan( (m1 - m2) / (1 + m1 * m2)) * 180 / 3.1417;

// Find the angle between Line BC and AC

LineSegmentIntersection(bx, by, cx, cy, cx, cy, ax, ay,

&m1, &c1, &m2, &c2, &intersection_X, &intersection_Y);

*angleC = atan( (m1 - m2) / (1 + m1 * m2)) * 180 / 3.1417;

// Find the angle between Line AC and AB

LineSegmentIntersection(cx, cy, ax, ay, ax, ay, bx, by,

&m1, &c1, &m2, &c2, &intersection_X, &intersection_Y);

*angleA = atan( (m1 - m2) / (1 + m1 * m2)) * 180 / 3.1417;

if(*angleA < 90 && *angleB < 90 && *angleC < 90)

return 1; // Acute Triangle

else if(*angleA > 90 || *angleB > 90 || *angleC > 90)

return 2; // obtuse Triangle

else

return 3; // Rightangle Triangle

}

void main()

{

float m1, c1, m2, c2;

float ax, ay, bx, by, cx, cy;

float angleA, angleB, angleC;

int nCountIntersections = 0;

std::cout << "Program to find the type of a triangle:\n";

std::cout << "Enter Triangle Point A - X: ";

std::cin >> ax;

std::cout << "Enter Triangle Point A - Y: ";

std::cin >> ay;

std::cout << "Enter Triangle Point B - X: ";

std::cin >> bx;

std::cout << "Enter Triangle Point B - Y: ";

std::cin >> by;

std::cout << "Enter Triangle Point C - X: ";

std::cin >> cx;

std::cout << "Enter Triangle Point C - Y: ";

std::cin >> cy;

int type = Calculate_Triangle_Type(ax, ay, bx, by, cx, cy, &angleA, &angleB, &angleC);

float total = angleA + angleB + angleC;

std::cout << "Angle between the lines is " << angleA << ", " << angleB << ", " << angleC << "\n";

if(type == 1)

std::cout << "\nAcute Triangle";

else if(type == 2)

std::cout << "\nObtuse Triangle";

else if(type == 3)

std::cout << "\nRight  Triangle";

std::cout << "\n";

}

## Output

Program to find the type of a triangle:

Enter Triangle Point A - X: 1

Enter Triangle Point A - Y: 1

Enter Triangle Point B - X: 20

Enter Triangle Point B - Y: 10

Enter Triangle Point C - X: 10

Enter Triangle Point C - Y: 20

Angle between the lines is 39.3063, 70.3438, 70.3438

Acute Triangle

Press any key to continue . . .