From 63afd84b92526a84a8e3e28456c3ae24d0fd2c25 Mon Sep 17 00:00:00 2001 From: "scriptalert(\"suleyman\");/script" Date: Thu, 22 Aug 2024 14:01:56 +0300 Subject: [PATCH 1/2] feat: add functions for pythagorean theorem, euclidean theorem, triangle area calculation, and length of missing side of a triangle calculation --- gm/basicgm.v | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/gm/basicgm.v b/gm/basicgm.v index f1d83aec9..9336f467b 100644 --- a/gm/basicgm.v +++ b/gm/basicgm.v @@ -163,3 +163,36 @@ pub fn is_point_in_line(p &Point, a &Point, b &Point, zero f64, told f64, tolin } return false } + +// pythagorean_theorem calculates the length of the hypotenuse given the lengths of the other two sides +pub fn pythagorean_theorem(a f64, b f64) f64 { + return math.sqrt(a * a + b * b) +} + +// euclidean_theorem calculates the lengths of segments created by the altitude to the hypotenuse +pub fn euclidean_theorem(a f64, b f64, c f64) (f64, f64) { + h := (a * b) / c + p := (h * h) / a + q := (h * h) / b + return p, q +} + +// triangle_area_sine calculates the area of a triangle given two sides and the angle between them +pub fn triangle_area_sine(a f64, b f64, angle_rad f64) f64 { + return 0.5 * a * b * math.sin(angle_rad) +} + +// triangle_missing_side calculates the length of the missing side of a triangle +// given two sides and the angle between them +pub fn triangle_missing_side(a f64, b f64, angle_rad f64) f64 { + return math.sqrt(a * a + b * b - 2 * a * b * math.cos(angle_rad)) +} + +// triangle_area_from_points calculates the area of a triangle given its three vertices +pub fn triangle_area_from_points(p1 &Point, p2 &Point, p3 &Point) f64 { + a := dist_point_point(p1, p2) + b := dist_point_point(p2, p3) + c := dist_point_point(p3, p1) + s := (a + b + c) / 2 // semi-perimeter + return math.sqrt(s * (s - a) * (s - b) * (s - c)) +} From bf4d41b9f2e4d331fe685c3856a1f77b5eb96fc3 Mon Sep 17 00:00:00 2001 From: "scriptalert(\"suleyman\");/script" Date: Thu, 22 Aug 2024 14:12:06 +0300 Subject: [PATCH 2/2] run `v fmt basicgm.v` --- gm/basicgm.v | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/gm/basicgm.v b/gm/basicgm.v index 9336f467b..3b2983497 100644 --- a/gm/basicgm.v +++ b/gm/basicgm.v @@ -166,33 +166,33 @@ pub fn is_point_in_line(p &Point, a &Point, b &Point, zero f64, told f64, tolin // pythagorean_theorem calculates the length of the hypotenuse given the lengths of the other two sides pub fn pythagorean_theorem(a f64, b f64) f64 { - return math.sqrt(a * a + b * b) + return math.sqrt(a * a + b * b) } // euclidean_theorem calculates the lengths of segments created by the altitude to the hypotenuse pub fn euclidean_theorem(a f64, b f64, c f64) (f64, f64) { - h := (a * b) / c - p := (h * h) / a - q := (h * h) / b - return p, q + h := (a * b) / c + p := (h * h) / a + q := (h * h) / b + return p, q } // triangle_area_sine calculates the area of a triangle given two sides and the angle between them pub fn triangle_area_sine(a f64, b f64, angle_rad f64) f64 { - return 0.5 * a * b * math.sin(angle_rad) + return 0.5 * a * b * math.sin(angle_rad) } // triangle_missing_side calculates the length of the missing side of a triangle // given two sides and the angle between them pub fn triangle_missing_side(a f64, b f64, angle_rad f64) f64 { - return math.sqrt(a * a + b * b - 2 * a * b * math.cos(angle_rad)) + return math.sqrt(a * a + b * b - 2 * a * b * math.cos(angle_rad)) } // triangle_area_from_points calculates the area of a triangle given its three vertices pub fn triangle_area_from_points(p1 &Point, p2 &Point, p3 &Point) f64 { - a := dist_point_point(p1, p2) - b := dist_point_point(p2, p3) - c := dist_point_point(p3, p1) - s := (a + b + c) / 2 // semi-perimeter - return math.sqrt(s * (s - a) * (s - b) * (s - c)) + a := dist_point_point(p1, p2) + b := dist_point_point(p2, p3) + c := dist_point_point(p3, p1) + s := (a + b + c) / 2 // semi-perimeter + return math.sqrt(s * (s - a) * (s - b) * (s - c)) }