omdl  v1.0
OpenSCAD Mechanical Design Library
Triangles

Triangle mathematical functions. More...

+ Collaboration diagram for Triangles:

Files

file  triangle.scad
 Triangle shapes, conversions, properties, and tests functions.
 

Shapes

function triangle_ppp2sss (c)
 Compute the side lengths of a triangle given its vertex coordinates. More...
 
function triangle_sas2sss (v)
 Compute the side lengths of a triangle given two sides and the included angle. More...
 
function triangle_asa2sss (v)
 Compute the side lengths of a triangle given a side and two adjacent angles. More...
 
function triangle_aas2sss (v)
 Compute the side lengths of a triangle given a side, one adjacent and the opposite angle. More...
 
function triangle2d_sss2ppp (v, a=x_axis_ci, cw=true)
 Compute a set of vertex coordinates for a triangle given its side lengths in 2D. More...
 

Properties

function triangle2d_area (c, s=false)
 Compute the area of a triangle given its vertex coordinates in 2D. More...
 
function triangle_centroid (c, d=2)
 Compute the centroid of a triangle. More...
 
function triangle2d_incenter (c)
 Compute the center coordinate of a triangle's incircle in 2D. More...
 
function triangle2d_inradius (c)
 Compute the inradius of a triangle's incircle in 2D. More...
 
function triangle2d_excenter (c, vi=1)
 Compute the center coordinate of a triangle's excircle in 2D. More...
 
function triangle2d_exradius (c, vi=1)
 Compute the exradius of a triangle's excircle in 2D. More...
 
function triangle_circumcenter (c, d=2)
 Compute the coordinate of a triangle's circumcenter. More...
 

Tests

function triangle2d_is_cw (c)
 Test the vertex ordering, or orientation, of a triangle in 2D. More...
 
function triangle2d_is_pit (c, p)
 Test if a point is inside a triangle in 2d. More...
 

Rounding

function triangle2d_vround3_center (c, r)
 Compute the rounding center coordinate for a given radius of a triangle vertex in 2D. More...
 
function triangle2d_vround3_tangents (c, r)
 Compute the rounding tangent coordinates for a given radius of a triangle vertex in 2D. More...
 

Usage Details

Triangle mathematical functions.

Validation Summary

filegroupscriptresultsno testskippedpassedfailedwarning
math/triangle.scadTrianglesScriptResults160000

No Failures

See complete validation results.

Requires:
include <omdl-base.scad>;

Conventions

The following conventions apply to all functions in this group.

See Wikipedia for more general information.

Function and/or Module Documentation

◆ triangle_ppp2sss()

function triangle_ppp2sss ( )

Compute the side lengths of a triangle given its vertex coordinates.

Parameters
c<points-3d | points-2d> A list, [v1, v2, v3], the 3d or 2d vertex coordinates.
Returns
<decimal-list-3> A list of side lengths [s1, s2, s3].

Each side length is opposite the corresponding vertex.

Object example

triangle_ppp2sss

See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.

◆ triangle_sas2sss()

function triangle_sas2sss ( )

Compute the side lengths of a triangle given two sides and the included angle.

Parameters
v<decimal-list-3> A list, [s1, a3, s2], the side lengths and the included angle.
Returns
<decimal-list-3> A list of side lengths [s1, s2, s3].

Each side length is opposite the corresponding vertex.

Object example

triangle_sas2sss

See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.
+ Here is the caller graph for this function:

◆ triangle_asa2sss()

function triangle_asa2sss ( )

Compute the side lengths of a triangle given a side and two adjacent angles.

Parameters
v<decimal-list-3> A list, [a1, s3, a2], the side length and two adjacent angles.
Returns
<decimal-list-3> A list of side lengths [s1, s2, s3].

Each side length is opposite the corresponding vertex.

Object example

triangle_asa2sss

No validation is performed on the input values. If a1 and a2 sum to 180° or more, the derived angle a3 = 180 - a1 - a2 is zero or negative and sin(a3) is zero or negative, causing division by zero or producing negative side lengths without warning.

+ Here is the caller graph for this function:

◆ triangle_aas2sss()

function triangle_aas2sss ( )

Compute the side lengths of a triangle given a side, one adjacent and the opposite angle.

Parameters
v<decimal-list-3> A list, [a1, a2, s1], a side length, one adjacent and the opposite angle.
Returns
<decimal-list-3> A list of side lengths [s1, s2, s3].

Each side length is opposite the corresponding vertex.

Object example

triangle_aas2sss

No validation is performed on the input values. If a1 and a2 sum to 180° or more, sin(a3) is zero or negative and division by zero or negative side lengths result without warning. If a1 is zero, division by sin(a1) also produces inf or nan.

+ Here is the caller graph for this function:

◆ triangle2d_sss2ppp()

function triangle2d_sss2ppp ( ,
= x_axis_ci,
cw  = true 
)

Compute a set of vertex coordinates for a triangle given its side lengths in 2D.

Parameters
v<decimal-list-3> A list, [s1, s2, s3], the side lengths.
a<integer> The axis alignment index < x_axis_ci | y_axis_ci >.
cw<boolean> Order vertices clockwise.
Returns
<points-2d> A list of vertex coordinates [v1, v2, v3], when cw = true, else [v1, v3, v2].

Each side length is opposite the corresponding vertex. The triangle will be constructed with v1 at the origin. Side s2 will be on the 'x' axis or side s3 will be on the 'y' axis as determined by parameter a.

Object example

triangle2d_sss2ppp

No verification is performed to ensure that the given sides specify a valid triangle. See Wikipedia for more information.

+ Here is the caller graph for this function:

◆ triangle2d_area()

function triangle2d_area ( ,
= false 
)

Compute the area of a triangle given its vertex coordinates in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
s<boolean> When true, return the signed area; when false (default), return the absolute area.
Returns
<decimal> The area of the given triangle, signed when s = true.

When s = true the sign encodes vertex ordering: negative for clockwise and positive for counter-clockwise, following the same convention as polygon_area(). When s = false the absolute value is returned regardless of vertex ordering.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.

◆ triangle_centroid()

function triangle_centroid ( ,
= 2 
)

Compute the centroid of a triangle.

Parameters
c<points-3d | points-2d> A list of 3d or 2d vertex coordinates [v1, v2, v3].
d<integer> The number of dimensions [2:3].
Returns
<point-3d | point-2d> The centroid coordinate.

The output dimensionality is controlled by d:

  • d = 2 (default): returns a 2d point using only the x and y components of each vertex. When 3d coordinates are passed with the default d = 2, the z-component is silently discarded.
  • d = 3: returns a 3d point using x, y, and z components.
  • d = 0: auto-detects dimensionality as the minimum coordinate length across all three vertices, useful for mixed-dimension input.

See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.
+ Here is the caller graph for this function:

◆ triangle2d_incenter()

function triangle2d_incenter ( )

Compute the center coordinate of a triangle's incircle in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
Returns
<point-2d> The incircle center coordinate [x, y].

The interior point for which distances to the sides of the triangle are equal. See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.
+ Here is the caller graph for this function:

◆ triangle2d_inradius()

function triangle2d_inradius ( )

Compute the inradius of a triangle's incircle in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
Returns
<decimal> The incircle radius.

See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.

◆ triangle2d_excenter()

function triangle2d_excenter ( ,
vi  = 1 
)

Compute the center coordinate of a triangle's excircle in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
vi<integer> The vertex index (1, 2, or 3). Returns the excircle center opposite vertex vi.
Returns
<point-2d> The excircle center coordinate [x, y].

A circle outside of the triangle specified by v1, v2, and v3, tangent to the side opposite vertex vi and tangent to the extensions of the other two sides away from vertex vi. Returns origin2d for any vi value other than 1, 2, or 3. See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.

◆ triangle2d_exradius()

function triangle2d_exradius ( ,
vi  = 1 
)

Compute the exradius of a triangle's excircle in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
vi<integer> The vertex index (1, 2, or 3). Returns the exradius of the excircle opposite vertex vi.
Returns
<decimal> The excircle radius opposite vertex vi, or 0 for any vi value other than 1, 2, or 3.

The exradius opposite vertex vi is computed from the semi-perimeter s and the three side lengths using the formula r_i = sqrt(s * (s-s_j) * (s-s_k) / (s-s_i)), where s_i, s_j, s_k are the sides opposite each vertex in order. See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.

◆ triangle_circumcenter()

function triangle_circumcenter ( ,
= 2 
)

Compute the coordinate of a triangle's circumcenter.

Parameters
c<points-3d | points-2d> A list of 3d or 2d vertex coordinates [v1, v2, v3].
d<integer> The number of dimensions [2:3].
Returns
<point-3d | point-2d> The circumcenter coordinate.

The circumcenter is the center of the circumscribed circle — the circle that passes through all three vertices. The circumradius is the distance from the circumcenter to any vertex.

The output dimensionality is controlled by d:

  • d = 2 (default): returns a 2d point using only the x and y components of each vertex. When 3d coordinates are passed with the default d = 2, the z-component is silently discarded.
  • d = 3: returns a 3d point using x, y, and z components.
  • d = 0: auto-detects dimensionality as the minimum coordinate length across all three vertices, useful for mixed-dimension input.

See Wikipedia for more information.

Note
No validation is performed on the input values. Degenerate or invalid input will produce undef, nan, or inf without warning unless an assert is present.
+ Here is the caller graph for this function:

◆ triangle2d_is_cw()

function triangle2d_is_cw ( )

Test the vertex ordering, or orientation, of a triangle in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
Returns
<boolean> true if the vertices are ordered clockwise, false if the vertices are ordered counterclockwise, and undef if the ordering can not be determined.

◆ triangle2d_is_pit()

function triangle2d_is_pit ( ,
 
)

Test if a point is inside a triangle in 2d.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
p<point-2d> A test point coordinate [x, y].
Returns
<boolean> true when the point is strictly inside the triangle, false when it is strictly outside, and undef when the triangle is degenerate (collinear vertices).

Uses barycentric coordinates to test point inclusion. Points that lie exactly on an edge or vertex of the triangle are considered outside (strict interior test); the boundary is excluded.

Returns undef when the three vertices are collinear (zero-area triangle), matching the convention used by triangle2d_is_cw() and triangle2d_area().

See Wikipedia for more information.

◆ triangle2d_vround3_center()

function triangle2d_vround3_center ( ,
 
)

Compute the rounding center coordinate for a given radius of a triangle vertex in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
r<decimal> The vertex rounding radius.
Returns
<point-2d> The rounding arc center coordinate for vertex v2.

Computes the center of the circular arc of radius r that rounds the corner at vertex v2 (c[1]). The arc is tangent to both edges meeting at v2.

r must satisfy 0 < r < ir, where ir is the triangle's inradius (triangle2d_inradius()). Passing r >= ir causes division by zero and is caught by an internal assert.

Note
Only vertex v2 (c[1]) is supported. To round v1 or v3, rotate the vertex list so that the target vertex is at c[1] before calling.

◆ triangle2d_vround3_tangents()

function triangle2d_vround3_tangents ( ,
 
)

Compute the rounding tangent coordinates for a given radius of a triangle vertex in 2D.

Parameters
c<points-2d> A list of vertex coordinates [v1, v2, v3].
r<decimal> The vertex rounding radius.
Returns
<points-2d> A list [t1, t2] of the two tangent point coordinates where the rounding arc meets the edges at v2.

Computes the two points on the edges adjacent to vertex v2 (c[1]) at which a rounding arc of radius r is tangent. t1 lies on the edge v2 to v1 and t2 lies on the edge v2 to v3.

r must satisfy 0 < r < ir (the triangle's inradius); this constraint is enforced by triangle2d_vround3_center(), which is called internally.

Note
Only vertex v2 (c[1]) is supported. See triangle2d_vround3_center() for the convention on rounding other vertices.