Skip to content

Commit

Permalink
Enhancing Triangle Issue#765
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Nov 7, 2024
1 parent 3dbc873 commit f3fa230
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/build123d/objects_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@
from build123d.build_enums import Align, FontStyle, Mode
from build123d.build_sketch import BuildSketch
from build123d.geometry import Axis, Location, Rotation, Vector, VectorLike
from build123d.topology import Compound, Edge, Face, ShapeList, Sketch, Wire, tuplify
from build123d.topology import (
Compound,
Edge,
Face,
ShapeList,
Sketch,
Wire,
tuplify,
TOLERANCE,
topo_explore_common_vertex,
)


class BaseSketchObject(Sketch):
Expand Down Expand Up @@ -720,3 +730,25 @@ def __init__(
triangle.move(Location(-center_of_geometry))
alignment = None if align is None else tuplify(align, 2)
super().__init__(obj=triangle, rotation=rotation, align=alignment, mode=mode)
self.edge_a = self.edges().filter_by(lambda e: abs(e.length - a) < TOLERANCE)[
0
] #: edge 'a'
self.edge_b = self.edges().filter_by(
lambda e: abs(e.length - b) < TOLERANCE and e not in [self.edge_a]
)[
0
] #: edge 'b'
self.edge_c = self.edges().filter_by(
lambda e: e not in [self.edge_a, self.edge_b]
)[
0
] #: edge 'c'
self.vertex_A = topo_explore_common_vertex(
self.edge_b, self.edge_c
) #: vertex 'A'
self.vertex_B = topo_explore_common_vertex(
self.edge_a, self.edge_c
) #: vertex 'B'
self.vertex_C = topo_explore_common_vertex(
self.edge_a, self.edge_b
) #: vertex 'C'
17 changes: 15 additions & 2 deletions tests/test_build_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"""

import unittest
from math import pi, sqrt
from math import pi, sqrt, atan2, degrees
from build123d import *


Expand Down Expand Up @@ -395,8 +395,21 @@ def test_trapezoid(self):
self.assertAlmostEqual(test.sketch.area, 8 * (12 + 4) / 2, 5)

def test_triangle(self):
tri = Triangle(a=3, b=4, c=5)
tri = Triangle(a=3, b=4, c=5, align=Align.MIN)
self.assertAlmostEqual(tri.area, (3 * 4) / 2, 5)
self.assertAlmostEqual(tri.A, degrees(atan2(3, 4)), 5)
self.assertAlmostEqual(tri.B, degrees(atan2(4, 3)), 5)
self.assertAlmostEqual(tri.C, 90, 5)
self.assertAlmostEqual(tri.a, 3, 5)
self.assertAlmostEqual(tri.b, 4, 5)
self.assertAlmostEqual(tri.c, 5, 5)
self.assertAlmostEqual(tri.edge_a.length, 3, 5)
self.assertAlmostEqual(tri.edge_b.length, 4, 5)
self.assertAlmostEqual(tri.edge_c.length, 5, 5)
self.assertTupleAlmostEquals(tri.vertex_A, (3, 4, 0), 5)
self.assertTupleAlmostEquals(tri.vertex_B, (0, 0, 0), 5)
self.assertTupleAlmostEquals(tri.vertex_C, (3, 0, 0), 5)

tri = Triangle(c=5, C=90, a=3)
self.assertAlmostEqual(tri.area, (3 * 4) / 2, 5)

Expand Down

0 comments on commit f3fa230

Please sign in to comment.