Skip to content

Commit

Permalink
+optimize imports
Browse files Browse the repository at this point in the history
import 'dart:math' show Point;
import 'dart:convert' show utf8;
- Optimized imports
- Added `Polygon.hasSamePoint()`
- [Issue raised for `List<List<dyanamic>>` to `List<List<num>>` casting exception](dart-lang/sdk#36614 "Dart-lang List<List<dyanamic>> to List<List<num>> Casting Issue")
  • Loading branch information
Sacchid committed Apr 13, 2019
1 parent 885a625 commit cc82d71
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 158 deletions.
197 changes: 104 additions & 93 deletions .idea/workspace.xml

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
## 1.0.0
- Initial version, created by Stagehand
- Had `Polygon`, `contains()`
## 1.0.1
As suggested by pub.dartlang.org analysis
- Ran dartfmt on poly.dart &
Expand All @@ -13,4 +14,8 @@ Added following functions & examples for -
* [Easy Casting](example/easy_casting.dart)
* [Simple CSV](example/simple_csv.dart)
* [Conversion](example/conversion.dart)
## 1.0.4
- Optimized imports
- Added `Polygon.hasSamePoint()`
- [Issue raised for `List<List<dyanamic>>` to `List<List<num>>` casting exception](https://github.com/dart-lang/sdk/issues/36614 "Dart-lang List<List<dyanamic>> to List<List<num>> Casting Issue")

5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -3,10 +3,11 @@ A library for checking if given point(s) is present inside Polygon or not.
## Usage
### [1. A very simple usage example ](example/poly_example.dart "Basic example")
1. Creates 2 Polygons from `List<Point>`
2. Checks if 2 `Polygon` have same vertices i.e. `points` : using `hasSamePoint()`
2. Prints if given Points are inside Polygon
3. Prints if all points in list are inside Polygon or not
4. Prints list of result for List of points if they are inside Polygon or not
* Here `isPointInside(Point( ))`, `contains(x,y)`, `areAllPointsInsidePolygon_ListPoint()` & `getList_IsListOfPointInside()` are used.
* Here `hasSamePoint()`, `isPointInside(Point( ))`, `contains(x,y)`, `areAllPointsInsidePolygon_ListPoint()` & `getList_IsListOfPointInside()` are used.

### [2. Example of Conversions `List <=> Point`, `List<Point> <=> List<List>` etc.](example/conversion.dart)
1. Example of `toPoint()`
Expand Down Expand Up @@ -62,7 +63,7 @@ A library for checking if given point(s) is present inside Polygon or not.
> type `int` is not a subtype of type `List<num>` in type cast
* 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>>`
* ****Note: currently casting `List<List<dynamic>>` to `List<List<num>>` gives following `CastError` exception:****
* [****Note: currently casting `List<List<dynamic>>` to `List<List<num>>` gives following `CastError` exception:****](https://github.com/dart-lang/sdk/issues/36614 "Dart-lang List<List<dyanamic>> to List<List<num>> Casting Issue")
* without `as List<List<num>>` -
> type `List<dynamic>` is not a subtype of type `List<num>` in type cast
* with `as List<List<num>>` -
Expand Down
9 changes: 4 additions & 5 deletions example/easy_casting.dart
@@ -1,7 +1,6 @@
import 'package:poly/poly.dart';

main() {
//TODO while coping contains run dartfmt on ost code
int correct_count = 0;
int exception_count = 0;

Expand Down Expand Up @@ -34,7 +33,7 @@ main() {

// 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
// * type `int` is not a subtype of type `List<num>` in type cast
try {
List wrongDynamicList = [1, 2];
var wrongPoint = toListOfPoint(wrongDynamicList);
Expand All @@ -44,7 +43,7 @@ main() {
"${++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>>'
// * type `List<dynamic>` is not a subtype of type `List<List<num>>`
try {
List wrongDynamicList = [1, 2];
var wrongPoint = toListOfPoint(wrongDynamicList.cast());
Expand All @@ -54,7 +53,7 @@ main() {
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
}
//TODO - Check how to List to List<List> or add List as an element in List<List>
// type 'List<dynamic>' is not a subtype of type 'List<num>' in type cast
// type `List<dynamic>` is not a subtype of type `List<num>` in type cast
try {
var l = [correctDynamicList];
print("${l.runtimeType}");
Expand All @@ -64,7 +63,7 @@ main() {
"${++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
//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}");
Expand Down
12 changes: 2 additions & 10 deletions example/exception_handling.dart
Expand Up @@ -4,7 +4,6 @@ main() {
int 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> c1 = [1, 2];
List<num> w1 = [1, 2, 3];
List<List<num>> w2 = [
[1, 2],
Expand All @@ -15,13 +14,6 @@ main() {
[3, 4],
[5, 6]
];
List<List<num>> c3 = [
c2,
[
[7, 8]
]
].expand((x) => x).toList();
// print("c3:$c3");

// Exception - 1
/// NeedsAtLeastThreePoints is thrown if Polygon.points contains less than 3 points
Expand All @@ -35,7 +27,7 @@ main() {
// Exception - 2
///WrongSizeForPoint is thrown if to_Point has more or less than 2 element. Point has only x and y.
try {
var b = toPoint(w1);
Point 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()}");
Expand All @@ -56,7 +48,7 @@ main() {
// Exception - 4
/// `CastError` example - List<num> to List<List<int>>
try {
Polygon ooo = toPolyFromListOfList(w1.cast());
Polygon wrongPoly = toPolyFromListOfList(w1.cast());
} on TypeError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
Expand Down
10 changes: 8 additions & 2 deletions example/poly_example.dart
Expand Up @@ -14,7 +14,7 @@ main() {
/// List of Points can be passed as parameter to constructor Polygon()
Polygon testPolygon = Polygon(l);
// Or Just Pass List inside constructor
Polygon a = Polygon([
Polygon copyOfFirstPolygon = Polygon([
Point(18.4851825, 73.8498851),
Point(18.4849214, 73.8498675),
Point(18.4855965, 73.8520493),
Expand All @@ -24,6 +24,12 @@ main() {
]);
Point 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.hasSamePoint(copyOfFirstPolygon)}");

/// A Point can be checked if it's present inside the Polygon by passing [Point i] to isPointInside function
// Should Print true
print(
Expand All @@ -32,7 +38,7 @@ main() {
/// 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) - ${a.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
Expand Down
104 changes: 60 additions & 44 deletions lib/poly.dart
Expand Up @@ -4,9 +4,11 @@ library poly;

export 'dart:math';

import 'dart:math';
import 'dart:math' show Point;
import 'package:csv/csv.dart';
import 'dart:convert';
import 'dart:convert' show utf8;
import 'package:collection/collection.dart' as checkList show DeepCollectionEquality;


// Appends 2 or 3 Strings - DO not use - problem

Expand Down Expand Up @@ -101,10 +103,13 @@ Polygon toPolyFromListOfList(List<List<num>> list_of_list) {
//import 'package:json_serializable/json_serializable.dart';
// TODO: Add json support ??
// TODO: Polygon Name ??

/// A class for representing two-dimensional Polygon defined with `List<Point<num>> points`.
class Polygon {
final List<Point<num>> points;
String name;

///Create a `Polygon` with vertices at `points`.
/// Pass a `List<Point<num>>`
Polygon(List<Point<num>> points) : points = points.toList(growable: false) {
var _number_of_point = this.points.length;
Expand Down Expand Up @@ -142,6 +147,18 @@ class Polygon {
return (depth & 1) == 1;
}

/// Checks if 2 `Polygon` have same vertices i.e. `points`
bool hasSamePoint(Polygon anotherPolygon){
bool same = (points.length == anotherPolygon.points.length);
if(!same){
return false;
}
else{
// int _length = points.length;
Function deepEq = const checkList.DeepCollectionEquality.unordered().equals;
return deepEq(points, anotherPolygon.points);
}
}
/// returns `true` if `Point` is present inside `Polygon`
bool isPointInside(Point i) {
return contains(i.x, i.y);
Expand Down Expand Up @@ -276,48 +293,6 @@ Future<List<List>> csvToListOfList(var csvString,
return listOfList;
}

//Future<Polygon> csvToPoly_casting_issue(var input, {bool isReversed = false}) async {
// List<List<num>> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
// final List<List<num>> numList = fields.cast<List<num>>();
// List<List<num>> rever = [];
// for (int i = 0; i < numList.length; i++) {
// num x = numList[i][1].toDouble();
// num y = numList[i][0].toDouble();
// rever.add([x, y]);
// }
// Polygon _out = to_poly_from_list_of_list(numList);
// return _out;
//}
////WOrks - type double
//Future<void> csvToTemp(var input, {bool isReversed = false}) async {
// List<List> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
//// final List<List<num>> numList = fields.cast<List<num>>();
//
// List<List<num>> rever = [];
// for (int i = 0; i <fields.length; i++) {
// var x = fields[i][1];
// var y = fields[i][0];
// print(x.runtimeType);
// rever.add([x, y]);
// }
//// return x;
//// Polygon _out = to_poly_from_list_of_list(rever);
//// return _out;
//}
// Works

/// * Returns `Future<Polygon>` based on `csvString`
/// * `csvString` may or may not contain header row
/// * This function checks if `latitude,longitude` or `x,y` are reversed
Expand Down Expand Up @@ -361,3 +336,44 @@ Future<Polygon> csvToPoly(var csvString, {bool isReversed = false}) async {
Polygon _out = toPolyFromListOfList(numList);
return _out;
}

//Future<Polygon> csvToPoly_casting_issue(var input, {bool isReversed = false}) async {
// List<List<num>> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
// final List<List<num>> numList = fields.cast<List<num>>();
// List<List<num>> rever = [];
// for (int i = 0; i < numList.length; i++) {
// num x = numList[i][1].toDouble();
// num y = numList[i][0].toDouble();
// rever.add([x, y]);
// }
// Polygon _out = to_poly_from_list_of_list(numList);
// return _out;
//}
////WOrks - type double
//Future<void> csvToTemp(var input, {bool isReversed = false}) async {
// List<List> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
//// final List<List<num>> numList = fields.cast<List<num>>();
//
// List<List<num>> rever = [];
// for (int i = 0; i <fields.length; i++) {
// var x = fields[i][1];
// var y = fields[i][0];
// print(x.runtimeType);
// rever.add([x, y]);
// }
//// return x;
//// Polygon _out = to_poly_from_list_of_list(rever);
//// return _out;
//}
5 changes: 3 additions & 2 deletions pubspec.yaml
@@ -1,14 +1,15 @@
name: poly
description: A library for checking if given point(s) is present inside Polygon or not.
version: 1.0.3
version: 1.0.4
homepage: https://github.com/Sacchid/poly
author: sacchi <email@example.com>

environment:
sdk: '>=2.1.0 <3.0.0'

dependencies:
csv: ^4.0.2
csv: ^4.0.3
collection: ^1.14.11
# json_serializable: ^2.1.0
# path: ^1.4.1

Expand Down

0 comments on commit cc82d71

Please sign in to comment.