Created with <3 with dartpad.dev.
Last active
September 2, 2023 00:44
-
-
Save ElanDeyan/6675102d1f2f3a4d00d7d73d0ffddf68 to your computer and use it in GitHub Desktop.
bold-aqueduct-4564
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' show pow, sqrt; | |
typedef Point = ({num x, num y}); | |
typedef Triple = (Point, Point, Point); | |
const epsilon = 0.00001; | |
void main() { | |
for (final triple in [ | |
scalenTriangle, | |
isoscelesTriangle, | |
equilateralTriangle, | |
diffPoints, | |
horizontalLine, | |
verticalLine, | |
diagonalLine | |
]) { | |
if (areTheSame(triple) || areAStraightLine(triple)) { | |
print('Invalid triangle'); | |
} else { | |
print('Valid triangle'); | |
final sides = triangleSides(triple); | |
if (isEquilateral(sides, epsilon)) { | |
print('Equilateral'); | |
} else if (isIsosceles(sides, epsilon)) { | |
print('Isosceles'); | |
} else { | |
print('Scalen'); | |
} | |
} | |
} | |
} | |
final equilateralTriangle = ( | |
(x: 0, y: 0), | |
(x: 2, y: 0), | |
(x: 1, y: sqrt(3)), | |
); | |
const diffPoints = ( | |
(x: 0, y: 0), | |
(x: 5, y: 0), | |
(x: 0, y: 5), | |
); | |
const horizontalLine = ( | |
(x: 1, y: 3), | |
(x: 2, y: 3), | |
(x: 5, y: 3), | |
); | |
const verticalLine = ( | |
(x: 2, y: 5), | |
(x: 2, y: 8), | |
(x: 2, y: 4), | |
); | |
const diagonalLine = ( | |
(x: 0, y: 3), | |
(x: 2, y: 7), | |
(x: 36.27, y: 75.54), | |
); | |
const isoscelesTriangle = ( | |
(x: 0, y: 0), | |
(x: 2, y: 0), | |
(x: 1, y: 2), | |
); | |
const scalenTriangle = ( | |
(x: 0, y: 0), | |
(x: 3, y: 0), | |
(x: 2, y: 2), | |
); | |
bool isEquilateral((num, num, num) sides, num epsilon) { | |
return closeEnough(sides.$1, sides.$2, epsilon) && | |
closeEnough(sides.$2, sides.$3, epsilon); | |
} | |
bool isIsosceles((num, num, num) sides, num epsilon) { | |
return closeEnough(sides.$1, sides.$2, epsilon) || | |
closeEnough(sides.$2, sides.$3, epsilon) || | |
closeEnough(sides.$1, sides.$3, epsilon); | |
} | |
bool areTheSame(Triple points) { | |
final (a, b, c) = points; | |
return a == b && b == c; | |
} | |
bool areAStraightLine(Triple points) { | |
final (a, b, c) = points; | |
if (a.x == b.x && b.x == c.x) { | |
return true; | |
} else if (a.y == b.y && b.y == c.y) { | |
return true; | |
} else { | |
final linearEquation = LinearEquation.fromPoints((a, b)); | |
return c.y == linearEquation.getYBy(x: c.x); | |
} | |
} | |
(num, num, num) triangleSides(Triple points) { | |
final (a, b, c) = points; | |
return ( | |
diffBetweenTwo(points: (a, b)), | |
diffBetweenTwo(points: (b, c)), | |
diffBetweenTwo(points: (a, c)), | |
); | |
} | |
num diffBetweenTwo({required (Point, Point) points}) { | |
final (a, b) = points; | |
if (a == b) return 0; | |
final height = (a.y - b.y).abs(); | |
final width = (a.x - b.x).abs(); | |
return sqrt(pow(width, 2) + pow(height, 2)); | |
} | |
bool closeEnough(num a, num b, num epsilon) { | |
return (a - b).abs() < epsilon; | |
} | |
final class LinearEquation { | |
num slope; | |
num intercept; | |
LinearEquation({required this.slope, required this.intercept}); | |
factory LinearEquation.fromPoints((Point, Point) pointPair) { | |
final newSlope = | |
(pointPair.$2.y - pointPair.$1.y) / (pointPair.$2.x - pointPair.$1.x); | |
final (point1, _) = pointPair; | |
final newIntercept = point1.y - (newSlope * point1.x); | |
return LinearEquation(slope: newSlope, intercept: newIntercept); | |
} | |
num getXBy({required num y}) { | |
return (y - intercept) / slope; | |
} | |
num getYBy({required num x}) { | |
return (slope * x) + intercept; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment