Triangles

Contruction

class Triangle2D(A, B, C, index=0, force_counterclockwise=False)

Create a triangle \(ABC\), where A, B and C are Point2D.

index is an optional integer index for this triangle.

If force_counterclockwise is True, points A, B and C may be reordered so that triangle is counterclockwise

Read-write attributes

A: Point2D
B: Point2D
C: Point2D

Triangle points. They may be replaced by another points, for example:

ABC.A = Point2D(1, 2)

or coordinate may be changed inplace, for example:

ABC.A.y = -2

In either case, the method recompute() must be called after change(s).

Read only attributes

area: float

Triangle area.

center: Point2D

Triangle center.

counterclockwise: bool

Whether triangle is counterclockwise.

Methods

recompute(self)

Recompute triangle properties (signed area, ...). Must be called when a triangle point is changed, or its coordinate is chanded.

includes_point(self, P, edge_width=0.)

Test if the triangle includes (contains) the point \(P\).

If point \(P\) is on one of the edge of triangle, due to numerical accuracy issues, the test may failed.

To solve this issue, if edge_width_square is not 0, triangle will be considered to include \(P\) if distance between \(P\) and one of triangle edge is less than edge_width (the root square of edge_width_square).

interpolate(self, data: double[3], P: Point2D)

Interpolate data defined on triangle vertices, to a point P inside the triangle.

plot(self, style='b-')

Plot triangle points using matplotlib

Structures

CTriangle2D
CPoint2D* A

\(A\) is the first triangle vertice.

CPoint2D* B

\(B\) is the second triangle vertice.

CPoint2D* C

\(C\) is the third triangle vertice.

CTriangle2D* new_triangle2d()

Allocate a new CTriangle2D.

This do not allocate members A, B, and C.

void del_triangle2d(CTriangle2D* ctri2d)

Delete a CTriangle2D.

This do not delete members A, B, and C.

void triangle2d_set(CTriangle2D* ABC, CPoint2D* A, CPoint2D* B, CPoint2D* C)

Set triangle points \(A\), \(B\), \(C\).

Computational functions

bint triangle2d_includes_point2d(CTriangle2D* ABC, CPoint2D* P, double edge_width_square)

Test if the triangle \(ABC\) includes (contains) the point \(P\).

If point \(P\) is on one of the edge of triangle \(ABC\), due to numerical accuracy issues, the test may failed.

To solve this issue, if edge_width_square is not 0, ABC will be considered to include \(P\) if distance between \(P\) and one of \(ABC\) edge is less than edge_width (the root square of edge_width_square).

int triangle2d_on_edges(CTriangle2D* ABC, CPoint2D* P, double edge_width_square)

Test if point \(P\) is on one of the edge of triangle \(ABC\).

\(P\) will be considered to be on one of the edge of triangle \(ABC\). if distance between \(P\) and one of \(ABC\) edge is less than edge_width (the root square of edge_width_square).

Returns:
  • 0 if \(P\) is on edge \([AB]\),
  • 1 if \(P\) is on edge \([BC]\),
  • 2 if \(P\) is on edge \([CA]\),
  • -2 if \(P\) is not on any edge.
double triangle2d_signed_area(CTriangle2D* T)

Compute the signed area of triangle \(T\).

Triangle area is the absolute value of the signed area.

  • If signed area is positive, triangle is counterclockwise.
  • If signed area is negative, triangle is clockwise.
  • If signed area is zero, triangle is degenerated.
double triangle2d_area(CTriangle2D* T)

Compute the area of triangle \(T\).

void triangle2d_center(CTriangle2D* T, CPoint2D* C)

Compute the center \(C\) of triangle \(T\).

Variable C must be already allocated.

void triangle2d_gradx_grady_det(CTriangle2D* tri, double signed_area, double gradx[3], double grady[3], double det[3])

Compute factors for linear interpolation of data defined on triangle vertices \(A\), \(B\) and \(C\) to points included in the triangle.

Relations between Python and Cython API

digraph Triangle2D {
Triangle2D -> "CTriangle2D"

Triangle2D -> "Point2D A"
Triangle2D -> "Point2D B"
Triangle2D -> "Point2D C"

CTriangle2D -> "CPoint2D* A"
CTriangle2D -> "CPoint2D* B"
CTriangle2D -> "CPoint2D* C"

"Point2D A" -> "CPoint2D* A"
"Point2D B" -> "CPoint2D* B"
"Point2D C" -> "CPoint2D* C"

"CPoint2D* A" -> "ax"
"CPoint2D* A" -> "ay"

"CPoint2D* B" -> "bx"
"CPoint2D* B" -> "by"

"CPoint2D* C" -> "cx"
"CPoint2D* C" -> "cy"

"CTriangle2D" [fillcolor=gray,style="rounded,filled"]

"CPoint2D* A" [fillcolor=gray,style="rounded,filled"]
"CPoint2D* B" [fillcolor=gray,style="rounded,filled"]
"CPoint2D* C" [fillcolor=gray,style="rounded,filled"]

"ax" [fillcolor=gray,style="rounded,filled"]
"ay" [fillcolor=gray,style="rounded,filled"]

"bx" [fillcolor=gray,style="rounded,filled"]
"by" [fillcolor=gray,style="rounded,filled"]

"cx" [fillcolor=gray,style="rounded,filled"]
"cy" [fillcolor=gray,style="rounded,filled"]
}
In [ ]: