Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ build/

# Directory created by dartdoc
doc/api/
/.vs
39 changes: 15 additions & 24 deletions example/conversion.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import 'package:poly/poly.dart';

main() {
int correct_count = 0;
var correct_count = 0;
// Because of Error that List<dynamic> isn't subtype of List<int> :
// Either use List<num> or cast it to List<num> while passing to respective function
List<num> c1 = [1, 2];
List<List<num>> c2 = [
var c1 = <num>[1, 2];
var c2 = <List<num>>[
[1, 2],
[3, 4],
[5, 6]
];
List<List<num>> c3 = []
..addAll(c2)
..addAll([
[7, 8]
]);
var c3 = <List<num>>[...c2, [7, 8]];
// Or use -
// List<List<num>> c3 = [
// c2,
Expand All @@ -26,30 +22,25 @@ main() {
//Example of `toPoint()` & `pointToList()`
//* `toPoint()` converts `List<num>` to `Point<num>`
// * `pointToList()` converts `Point<num>` to `List<num>`
Point pointFromC1 = toPoint(c1);
List<num> listFromPoint = pointToList(pointFromC1);
print(
"${++correct_count}. toPoint(c1 = [1,2])= ${pointFromC1} with type:${pointFromC1.runtimeType} & pointToList() gives us ${listFromPoint} with type:${listFromPoint.runtimeType}");
var pointFromC1 = toPoint(c1);
var listFromPoint = pointToList(pointFromC1);
print('${++correct_count}. toPoint(c1 = [1,2])= $pointFromC1 with type:${pointFromC1.runtimeType} & pointToList() gives us $listFromPoint with type:${listFromPoint.runtimeType}');

//Example of `toListOfPoint()` & `pointsToList()`
//* `toListOfPoint()` converts `List<List<num>>` to `List<Point<num>>`
//* `pointsToList()` converts `List<Point<num>>` to `List<List<num>>`
List<Point> pointsFromC2 = toListOfPoint(c2);
List<List<num>> listFromPoints = pointsToList(pointsFromC2);
print(
"${++correct_count}. toListOfPoint(c2 = [ [1,2], [3,4], [5,6] ])= ${pointsFromC2} with type:${pointsFromC2.runtimeType} & pointsToList() gives us ${listFromPoints} with type:${listFromPoints.runtimeType}");
var pointsFromC2 = toListOfPoint(c2);
var listFromPoints = pointsToList(pointsFromC2);
print('${++correct_count}. toListOfPoint(c2 = [ [1,2], [3,4], [5,6] ])= $pointsFromC2 with type:${pointsFromC2.runtimeType} & pointsToList() gives us $listFromPoints with type:${listFromPoints.runtimeType}');

//Example of `toPolyFromListOfList()` & `listOfList()`
//* `toPolyFromListOfList()` converts `List<List<num>>` to `Polygon`
// * `listOfList()` returns `Polygon.points` as `List<List<num>>`
//* Print status of List of List if they are inside our Polygon using `getList_IsListOfListInside`
//* Print if All points in List of List inside Polygon using
Polygon polygonC2 = toPolyFromListOfList(c2);
List<List<num>> backToListC2 = polygonC2.listOfList();
print(
"${++correct_count}. toPolyFromListOfList() gives ${polygonC2.runtimeType} & backToListC2() gives back ${backToListC2.runtimeType}");
print(
"${++correct_count}. Is C3= [ C2, [7,8] ] inside Polygon C2 = ${polygonC2.getList_IsListOfListInside(c3)}");
print(
"${++correct_count}. Are All Points in C3= [ C2, [7,8] ] present inside C2 = ${polygonC2.areAllPointsInsidePolygon_List(c3)}");
var polygonC2 = toPolyFromListOfList(c2);
var backToListC2 = polygonC2.listOfList();
print('${++correct_count}. toPolyFromListOfList() gives ${polygonC2.runtimeType} & backToListC2() gives back ${backToListC2.runtimeType}');
print('${++correct_count}. Is C3= [ C2, [7,8] ] inside Polygon C2 = ${polygonC2.getList_IsListOfListInside(c3)}');
print('${++correct_count}. Are All Points in C3= [ C2, [7,8] ] present inside C2 = ${polygonC2.areAllPointsInsidePolygon_List(c3)}');
}
65 changes: 31 additions & 34 deletions example/easy_casting.dart
Original file line number Diff line number Diff line change
@@ -1,96 +1,93 @@
import 'package:poly/poly.dart';

main() {
int correct_count = 0;
int exception_count = 0;
var correct_count = 0;
var exception_count = 0;

//1. Correct casting
// * casting `List<dynamic> to `List<num>`
List correctDynamicList = [1, 2];
Point correctPointFromDynamicList = toPoint(correctDynamicList.cast<num>());
var correctDynamicList = [1, 2];
var correctPointFromDynamicList = toPoint(correctDynamicList.cast<num>());
print(
"${++correct_count}. Type of correctPointFromDynamicList is ${correctPointFromDynamicList.runtimeType}, while correctDynamicList was ${correctDynamicList.runtimeType} \t //See casting worked as desired");
'${++correct_count}. Type of correctPointFromDynamicList is ${correctPointFromDynamicList.runtimeType}, while correctDynamicList was ${correctDynamicList.runtimeType} \t //See casting worked as desired');

// * casting `List<dynamic>` to `List<List<num>>`
List correctDynamicListOfList = [
var correctDynamicListOfList = [
[1, 2],
[3, 4]
];
List<Point> listPointFromDynamicList =
var listPointFromDynamicList =
toListOfPoint(correctDynamicListOfList.cast<List<num>>());
print(
"${++correct_count}. Type of listPointFromDynamicList is ${listPointFromDynamicList.runtimeType}, while correctDynamicListOfList was ${correctDynamicListOfList.runtimeType} \t //See casting worked as desired");
'${++correct_count}. Type of listPointFromDynamicList is ${listPointFromDynamicList.runtimeType}, while correctDynamicListOfList was ${correctDynamicListOfList.runtimeType} \t //See casting worked as desired');

// * Using `toListListNum`
List<List<num>> l = toListListNum([correctDynamicList]);
List<List<num?>> l = toListListNum([correctDynamicList]);
print(
"${++correct_count}. ${l} has type:${l.runtimeType} & has length:${l.length}");
'${++correct_count}. $l has type:${l.runtimeType} & has length:${l.length}');

// Without casting `List<dynamic>` to `List<num>` `TypeError`is thrown as shown below :
try {
List wrongDynamicList = [1, 2];
var wrongDynamicList = [1, 2];
var wrongPoint = toPoint(wrongDynamicList);
print(wrongPoint.runtimeType);
} on TypeError catch (e) {
print(
"\n${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'\n${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}

// 2. Passing `List` instead of `List<List>`
// * Passing `List` instead of `List<List>` & casting it : throws `CastError` as shown below :
// * type `int` is not a subtype of type `List<num>` in type cast
try {
List wrongDynamicList = [1, 2];
var wrongPoint = toListOfPoint(wrongDynamicList);
var wrongDynamicList = [1, 2];
var wrongPoint = toListOfPoint(wrongDynamicList as List<List<num>>);
print(wrongPoint.runtimeType);
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}
// * Passing `List` instead of `List<List>` but, without casting it : throws `TypeError` as shown below :
// * type `List<dynamic>` is not a subtype of type `List<List<num>>`
try {
List wrongDynamicList = [1, 2];
var wrongDynamicList = [1, 2];
var wrongPoint = toListOfPoint(wrongDynamicList.cast());
print(wrongPoint.runtimeType);
} on CastError catch (e) {
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}
// type `List<dynamic>` is not a subtype of type `List<num>` in type cast
try {
var l = [correctDynamicList];
print("${l.runtimeType}");
List<Point> wrongListOfList = toListOfPoint(l.cast());
} on CastError catch (e) {
print('${l.runtimeType}');
var wrongListOfList = toListOfPoint(l.cast());
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}

//type `List<List<dynamic>>` is not a subtype of type `List<List<num>>` in type cast
try {
var l = [correctDynamicList] as List<List<num>>;
print("${l.runtimeType}");
List<Point> wrongListOfList = toListOfPoint(l.cast());
} on CastError catch (e) {
var l = [correctDynamicList];
print('${l.runtimeType}');
var wrongListOfList = toListOfPoint(l.cast());
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}

// * Using from
// > type `List<dynamic>` is not a subtype of type `List<num>`
try {
var l =
List<List<num>>.from([correctDynamicList].toList() as List<List<num>>)
List<List<num>>.from([correctDynamicList].toList())
.cast()
.toList();
print("${l.runtimeType}");
List<Point> wrongListOfList = toListOfPoint(l.cast());
print('${l.runtimeType}');
var wrongListOfList = toListOfPoint(l.cast());
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
} on CastError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}
}
30 changes: 15 additions & 15 deletions example/exception_handling.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'package:poly/poly.dart';

main() {
int exception_count = 0;
var exception_count = 0;
// Because of Error that List<dynamic> isn't subtype of List<int>
// So, List a can't be used without casting
List<num> w1 = [1, 2, 3];
List<List<num>> w2 = [
var w1 = <num>[1, 2, 3];
var w2 = <List<num>>[
[1, 2],
[3, 4]
];
List<List<num>> c2 = [
var c2 = <List<num>>[
[1, 2],
[3, 4],
[5, 6]
Expand All @@ -18,43 +18,43 @@ main() {
// Exception - 1
/// NeedsAtLeastThreePoints is thrown if Polygon.points contains less than 3 points
try {
Polygon less_than_3 = toPolyFromListOfList(w2);
var less_than_3 = toPolyFromListOfList(w2);
} on NeedsAtLeastThreePoints catch (e) {
print("\n");
print('\n');
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 Please provide three or more points. Current number of Points: ${e.currentPointsInPolygon()}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 Please provide three or more points. Current number of Points: ${e.currentPointsInPolygon()}');
}
// Exception - 2
///WrongSizeForPoint is thrown if to_Point has more or less than 2 element. Point has only x and y.
try {
Point pointWithMoreThanXY = toPoint(w1);
var pointWithMoreThanXY = toPoint(w1);
} on WrongSizeForPoint catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 Wrong size, List must have 2 element. Current Size: ${e.inputListLength()}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 Wrong size, List must have 2 element. Current Size: ${e.inputListLength()}');
}

// Exception - 3
/// `TypeError` is thrown if `List<dynamic>` is passed instead of `List<num>`
/// e.g. type 'List<dynamic>' is not a subtype of type 'List<num>'
try {
List dy = [1, 2];
var ttemp = toPoint(dy);
var dy = [1, 2];
var ttemp = toPoint(dy as List<num>);
print(ttemp.runtimeType);
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
}

// Exception - 4
/// `CastError` example - List<num> to List<List<int>>
try {
Polygon wrongPoly = toPolyFromListOfList(w1.cast());
var wrongPoly = toPolyFromListOfList(w1.cast());
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
'${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 $e');
} catch (e) {
print(
"${++exception_count}. ${e.runtimeType} handled : \n \t \u2022 ${e} || ");
'${++exception_count}. ${e.runtimeType} handled : \n \t \u2022 $e || ');
}
// Correct Error 1
// Error: The argument type 'List<num>' can't be assigned to the parameter type 'List<List<num>>'.
Expand Down
24 changes: 12 additions & 12 deletions example/poly_example.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:poly/poly.dart';

main() {
int example_count = 0;
List<Point<num>> l = [
var example_count = 0;
var l = <Point<num>>[
Point(18.4851825, 73.8498851),
Point(18.4849214, 73.8498675),
Point(18.4855965, 73.8520493),
Expand All @@ -12,48 +12,48 @@ main() {
];

/// List of Points can be passed as parameter to constructor Polygon()
Polygon testPolygon = Polygon(l);
var testPolygon = Polygon(l);
// Or Just Pass List inside constructor
Polygon copyOfFirstPolygon = Polygon([
var copyOfFirstPolygon = Polygon([
Point(18.4851825, 73.8498851),
Point(18.4849214, 73.8498675),
Point(18.4855965, 73.8520493),
Point(18.4859711, 73.8512369),
Point(18.4857828, 73.8500299),
Point(18.4851825, 73.8498851)
]);
Point in1 = Point(18.48569, 73.85067);
var in1 = Point(18.48569, 73.85067);

/// Example of `hasSamePoint()`
/// *Checks if 2 `Polygon` have same vertices i.e. `points`
// * Should print `true`
print(
"${++example_count}. `testPolygon` and `copyOfFirstPolygon` are same : ${testPolygon.hasSamePoints(copyOfFirstPolygon)}");
'${++example_count}. `testPolygon` and `copyOfFirstPolygon` are same : ${testPolygon.hasSamePoints(copyOfFirstPolygon)}');

/// A Point can be checked if it's present inside the Polygon by passing [Point i] to isPointInside function
// Should Print true
print(
"${++example_count}. in1=(18.48569, 73.85067) is inside testWyse - ${testPolygon.isPointInside(in1)}");
'${++example_count}. in1=(18.48569, 73.85067) is inside testWyse - ${testPolygon.isPointInside(in1)}');

/// X,Y can be checked if they are present inside polygon by passing [x,y] to contains function
// Should Print true
print(
"${++example_count}. Polygon a contains :(18.48569, 73.85067) - ${copyOfFirstPolygon.contains(18.48569, 73.85067)}");
'${++example_count}. Polygon a contains :(18.48569, 73.85067) - ${copyOfFirstPolygon.contains(18.48569, 73.85067)}');

/// Multiple Points inside a List can be tested by passing it to areAllPointsInsidePolygon_ListPoint
// Should Print true
print(
"${++example_count}. All points in l are within testWyse - ${testPolygon.areAllPointsInsidePolygon_ListPoint(l)}");
'${++example_count}. All points in l are within testWyse - ${testPolygon.areAllPointsInsidePolygon_ListPoint(l)}');

///areAllPointsInsidePolygon_ListPoint returns true if all Point<num> are inside Polygon
// Should Print false
List<Point<num>> notInsidePoints = []..addAll(l)..addAll([Point(75, 90)]);
var notInsidePoints = <Point<num>>[...l]..addAll([Point(75, 90)]);
print(
"${++example_count}. All points in notInsidePoints are within testWyse - ${testPolygon.areAllPointsInsidePolygon_ListPoint(notInsidePoints)}");
'${++example_count}. All points in notInsidePoints are within testWyse - ${testPolygon.areAllPointsInsidePolygon_ListPoint(notInsidePoints)}');
// print("${notInsidePoints}, ${notInsidePoints.runtimeType}");

/// getList_IsListOfPointInside returns List<bool> as List<Point> if List<Point> are inside Polygon
// Should Print: [true, true, true, true, true, true, false]
print(
"${++example_count}. Status of notInsidePoints - ${testPolygon.getList_IsListOfPointInside(notInsidePoints)}");
'${++example_count}. Status of notInsidePoints - ${testPolygon.getList_IsListOfPointInside(notInsidePoints)}');
}
Loading