Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrootcpp committed Jun 24, 2024
1 parent 336014c commit 9442ab0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
64 changes: 64 additions & 0 deletions highpymath/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,70 @@ def circle(r: any, r_is_d: bool = False, return_area: bool = False, return_circu
return _area
elif return_circumference:
return _circumference

@staticmethod
def trapezoid(a: any, b: any, c: any = None, d: any = None, h: any = None, return_area: bool = False, return_circumference: bool = False, return_both: bool = True, return_int: bool = False, return_string: bool = False):
"""
Calculate the Area or the Circumference of a Trapezoid.
"""
from .highpymath import trapezoid_area as _ta
from .highpymath import trapezoid_circumference as _tc
if not return_area and not return_circumference and not return_both:
raise MathValueError("You need to specify one of the 3 arguments")
if return_area and return_circumference:
raise MathValueError("You need to specify one of the 3 arguments")
if return_area and return_both:
return_both = False
if return_circumference and return_both:
return_both = False
return_float = True
if return_int:
return_float = False
if return_area and h is None:
raise MathValueError("You need to specify h")
if return_circumference or return_both and c is None:
raise MathValueError("You need to specify c")
if return_circumference or return_both and d is None:
raise MathValueError("You need to specify d")
if not isinstance(a, (int, float)):
raise MathTypeError("a must be a number")
if not isinstance(b, (int, float)):
raise MathTypeError("b must be a number")
if return_area or return_both and isinstance(h, int):
h = float(h)
if return_circumference or return_both and isinstance(c, int):
c = float(c)
if return_circumference or return_both and isinstance(d, int):
d = float(d)
if isinstance(a, int):
a = float(a)
if isinstance(b, int):
b = float(b)
if return_area or return_both:
_area = _ta(a=a, b=b, h=h)
if return_circumference or return_both:
_circumference = _tc(a=a, b=b, c=c, d=d)
if return_int:
if _area:
_area = int(_area)
if _circumference:
_circumference = int(_circumference)
elif return_float:
if _area:
_area = float(_area)
if _circumference:
_circumference = float(_circumference)
if return_string:
if _area:
_area = str(_area)
if _circumference:
_circumference = str(_circumference)
if return_both:
return _area, _circumference
elif return_area:
return _area
elif return_circumference:
return _circumference

GeometricProperties2D = GeometricProperties2D()

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ fn circle_circumference(r: PyFloat) -> PyResult<PyFloat> {
Ok(_result)
}

#[pyfunction]
fn trapezoid_area(a: PyFloat, b: PyFloat, h: PyFloat) -> PyResult<PyFloat> {
Ok((a + b) * h / 2.0)
}

#[pyfunction]
fn trapezoid_circumference(a: PyFloat, b: PyFloat, c: PyFloat, d: PyFloat) -> PyResult<PyFloat> {
Ok(a + b + c + d)
}

/// A Python module implemented in Rust.
#[pymodule]
fn highpymath(m: &PyModule) -> PyResult<()> {
Expand Down Expand Up @@ -313,6 +323,8 @@ fn highpymath(m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(quadratic_circumference, m)?)?;
m.add_function(wrap_pyfunction!(circle_area, m)?)?;
m.add_function(wrap_pyfunction!(circle_circumference, m)?)?;
m.add_function(wrap_pyfunction!(trapezoid_area, m)?)?;
m.add_function(wrap_pyfunction!(trapezoid_circumference, m)?)?;
m.add("MathTypeError", m.py().get_type::<MathTypeError>())?;
m.add("MathValueError", m.py().get_type::<MathValueError>())?;
m.add("GeometryError", m.py().get_type::<GeometryError>())?;
Expand Down

0 comments on commit 9442ab0

Please sign in to comment.