The trianglelib API Reference

Routines for working with triangles.

The two modules inside of this package are packed with useful features for the programmer who needs to support triangles:

shape

This module provides a full-fledged Triangle object that can be instantiated and then asked to provide all sorts of information about its properties.

utils

For the programmer in a hurry, this module offers quick functions that take as arguments the three side lengths of a triangle, and perform a quick computation without the programmer having to make the extra step of creating an object.

The “shape” module

Use the triangle class to represent triangles.

class trianglelib.shape.Triangle(a, b, c)[source]

A Triangle object is a three-sided polygon.

You instantiate a Triangle by providing exactly three lengths a, b, and c.

They can either be intergers or floating-point numbers, and should be listed clockwise around the triangle.

If the three lengths cannot make a valid triangle, then ValueError will be raised instead.

>>> from trianglelib.shape import Triangle
>>> t = Triangle(3, 4, 5)
>>> print(t.is_equilateral())
False
>>> print(t.area())
6.0

Triangles support the following attributes, operators, and methods.

a
b
c

The three side lengths provided during instantiation.

triangle1 == triangle2

Returns true if the two triangles have sides of the same lengths, in the same order. Note that it is okay if the two triangles happen to start their list of sides at a different corner; 3,4,5 is the same triangle as 4,5,3 but neither of these are the same triangle as their mirror image 5,4,3.

__init__(a, b, c)[source]

Create a Triangle object with sides of lengths a, b, and c.

Raises ValueError if the three length values provided cannot actually form a triangle.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Raises
  • ValueError – side lengths must all be positive

  • ValueError – one side is too long to make a triangle

is_equivalent(triangle)[source]

Return whether this triangle equals another triangle.

Parameters

triangle (Triangle) – another Triangle object

Returns

whether the two Triangle objects are equivalent

Return type

bool

is_similar(triangle)[source]

Return whether this Triangle object is similar to another triangle.

Parameters

triangle (Triangle) – another Triangle object

Returns

whether the two Triangle objects are similar

Return type

bool

is_equilateral()[source]

Return whether this Triangle object is equilateral.

Returns

whether the Triangle object is equilateral

Return type

bool

is_isosceles()[source]

Return whether this Triangle object is isosceles.

Returns

whether the Triangle object is isosceles

Return type

bool

perimeter()[source]

Return the perimeter of this Triangle object.

Returns

the perimeter of the Triangle object.

Return type

float

area()[source]

Return the area of this Triangle object.

Returns

the area of the Triangle object.

Return type

float

scale(factor)[source]

Return a new Triangle object, factor times the size of this one.

Parameters

factor (float) – scaling factor

Returns

a scaled new Triangle object

Return type

Triangle

The “utils” module

Routines to test triangle properties without explicit instantiation.

trianglelib.utils.compute_area(a, b, c)[source]

Return the area of the triangle with side lengths a, b, and c.

If the three lengths provided cannot be the sides of a triangle, then the area 0 is returned.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Returns

area. If the three lengths provided cannot be the sides of a triangle, then the perimeter 0 is returned.

Return type

float

trianglelib.utils.compute_perimeter(a, b, c)[source]

Return the perimeter of the triangle with side lengths a, b, and c.

If the three lengths provided cannot be the sides of a triangle, then the perimeter 0 is returned.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Returns

perimeter. If the three lengths provided cannot be the sides of a triangle, then the perimeter 0 is returned.

Return type

float

trianglelib.utils.is_equilateral(a, b, c)[source]

Return whether lengths a, b, and c are an equilateral triangle.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Returns

whether lengths a, b, and c are an equilateral triangle

Return type

bool

trianglelib.utils.is_isosceles(a, b, c)[source]

Return whether lengths a, b, and c are an isosceles triangle.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Returns

whether lengths a, b, and c are an isosceles triangle

Return type

bool

trianglelib.utils.is_triangle(a, b, c)[source]

Return whether lengths a, b, c can be the sides of a triangle.

Parameters
  • a (float) – side length one

  • b (float) – side length two

  • c (float) – side length three

Returns

whether lengths a, b, c can be the sides of a triangle

Return type

bool