Created with <3 with dartpad.dev.
Last active
November 22, 2022 06:25
-
-
Save leynier/c383c3b2766bddb894c9730fae9cfd7f to your computer and use it in GitHub Desktop.
intersection-point
This file contains hidden or 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
void main() { | |
final p1 = Point(0, 0); | |
final p2 = Point(-5, -10); | |
final p3 = Point(-10, -5); | |
final p4 = Point(15, -5); | |
final r1 = Line(p1, p2); | |
final r2 = Line(p3, p4); | |
final pi = intersection(r1, r2); | |
print("${pi.x}, ${pi.y}"); | |
} | |
class Point { | |
const Point(this.x, this.y); | |
final double x; | |
final double y; | |
} | |
class Line { | |
const Line(this.one, this.two); | |
final Point one; | |
final Point two; | |
double get m => (two.y - one.y) / (two.x - one.x); | |
} | |
Point intersection(Line r1, Line r2) { | |
final x = (-1 * r1.m * r1.one.x + r2.m * r2.one.x + r1.one.y - r2.one.y) / (r2.m - r1.m); | |
final y = r1.m * (x - r1.one.x) + r1.one.y; | |
return Point(x, y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment