From 7f71c40d7bb8aecf196146aed18191b2f86957c2 Mon Sep 17 00:00:00 2001 From: Hamidreza Bayat Date: Sat, 30 Apr 2022 07:19:46 +0430 Subject: [PATCH 1/2] Add dart project --- source/dart/.gitignore | 10 ++++++ source/dart/CHANGELOG.md | 3 ++ source/dart/README.md | 39 ++++++++++++++++++++++ source/dart/analysis_options.yaml | 30 +++++++++++++++++ source/dart/example/astronomy_example.dart | 6 ++++ source/dart/lib/astronomy.dart | 8 +++++ source/dart/lib/src/astronomy_base.dart | 6 ++++ source/dart/pubspec.yaml | 15 +++++++++ source/dart/test/astronomy_test.dart | 16 +++++++++ 9 files changed, 133 insertions(+) create mode 100644 source/dart/.gitignore create mode 100644 source/dart/CHANGELOG.md create mode 100644 source/dart/README.md create mode 100644 source/dart/analysis_options.yaml create mode 100644 source/dart/example/astronomy_example.dart create mode 100644 source/dart/lib/astronomy.dart create mode 100644 source/dart/lib/src/astronomy_base.dart create mode 100644 source/dart/pubspec.yaml create mode 100644 source/dart/test/astronomy_test.dart diff --git a/source/dart/.gitignore b/source/dart/.gitignore new file mode 100644 index 00000000..65c34dc8 --- /dev/null +++ b/source/dart/.gitignore @@ -0,0 +1,10 @@ +# Files and directories created by pub. +.dart_tool/ +.packages + +# Conventional directory for build outputs. +build/ + +# Omit committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/source/dart/CHANGELOG.md b/source/dart/CHANGELOG.md new file mode 100644 index 00000000..effe43c8 --- /dev/null +++ b/source/dart/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/source/dart/README.md b/source/dart/README.md new file mode 100644 index 00000000..8b55e735 --- /dev/null +++ b/source/dart/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/source/dart/analysis_options.yaml b/source/dart/analysis_options.yaml new file mode 100644 index 00000000..dee8927a --- /dev/null +++ b/source/dart/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/source/dart/example/astronomy_example.dart b/source/dart/example/astronomy_example.dart new file mode 100644 index 00000000..e794846b --- /dev/null +++ b/source/dart/example/astronomy_example.dart @@ -0,0 +1,6 @@ +import 'package:astronomy/astronomy.dart'; + +void main() { + var awesome = Awesome(); + print('awesome: ${awesome.isAwesome}'); +} diff --git a/source/dart/lib/astronomy.dart b/source/dart/lib/astronomy.dart new file mode 100644 index 00000000..3f11c7d9 --- /dev/null +++ b/source/dart/lib/astronomy.dart @@ -0,0 +1,8 @@ +/// Support for doing something awesome. +/// +/// More dartdocs go here. +library astronomy; + +export 'src/astronomy_base.dart'; + +// TODO: Export any libraries intended for clients of this package. diff --git a/source/dart/lib/src/astronomy_base.dart b/source/dart/lib/src/astronomy_base.dart new file mode 100644 index 00000000..e8a6f159 --- /dev/null +++ b/source/dart/lib/src/astronomy_base.dart @@ -0,0 +1,6 @@ +// TODO: Put public facing types in this file. + +/// Checks if you are awesome. Spoiler: you are. +class Awesome { + bool get isAwesome => true; +} diff --git a/source/dart/pubspec.yaml b/source/dart/pubspec.yaml new file mode 100644 index 00000000..296cb8b5 --- /dev/null +++ b/source/dart/pubspec.yaml @@ -0,0 +1,15 @@ +name: astronomy +description: A starting point for Dart libraries or applications. +version: 1.0.0 +# homepage: https://www.example.com + +environment: + sdk: '>=2.16.2 <3.0.0' + + +# dependencies: +# path: ^1.8.0 + +dev_dependencies: + lints: ^1.0.0 + test: ^1.16.0 diff --git a/source/dart/test/astronomy_test.dart b/source/dart/test/astronomy_test.dart new file mode 100644 index 00000000..33da90bd --- /dev/null +++ b/source/dart/test/astronomy_test.dart @@ -0,0 +1,16 @@ +import 'package:astronomy/astronomy.dart'; +import 'package:test/test.dart'; + +void main() { + group('A group of tests', () { + final awesome = Awesome(); + + setUp(() { + // Additional setup goes here. + }); + + test('First Test', () { + expect(awesome.isAwesome, isTrue); + }); + }); +} From 9257b937f174870f0bac575a9fd7d69347849806 Mon Sep 17 00:00:00 2001 From: Hamidreza Bayat Date: Sat, 30 Apr 2022 13:53:48 +0430 Subject: [PATCH 2/2] Add TerseVector --- source/dart/example/astronomy_example.dart | 6 -- source/dart/lib/astronomy.dart | 92 ++++++++++++++++++++-- source/dart/lib/src/astronomy_base.dart | 6 -- source/dart/test/astronomy_test.dart | 18 ++--- 4 files changed, 95 insertions(+), 27 deletions(-) delete mode 100644 source/dart/example/astronomy_example.dart delete mode 100644 source/dart/lib/src/astronomy_base.dart diff --git a/source/dart/example/astronomy_example.dart b/source/dart/example/astronomy_example.dart deleted file mode 100644 index e794846b..00000000 --- a/source/dart/example/astronomy_example.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:astronomy/astronomy.dart'; - -void main() { - var awesome = Awesome(); - print('awesome: ${awesome.isAwesome}'); -} diff --git a/source/dart/lib/astronomy.dart b/source/dart/lib/astronomy.dart index 3f11c7d9..9716e6a7 100644 --- a/source/dart/lib/astronomy.dart +++ b/source/dart/lib/astronomy.dart @@ -1,8 +1,90 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. library astronomy; -export 'src/astronomy_base.dart'; +import 'dart:math'; -// TODO: Export any libraries intended for clients of this package. +class TerseVector { + final double x; + final double y; + final double z; + + const TerseVector({ + required this.x, + required this.y, + required this.z, + }); + + static final TerseVector zero = TerseVector(x: 0.0, y: 0.0, z: 0.0); + + TerseVector operator +(TerseVector other) { + return TerseVector(x: x + other.x, y: y + other.y, z: z + other.z); + } + + TerseVector operator -(TerseVector other) { + return TerseVector(x: x - other.x, y: y - other.y, z: z - other.z); + } + + TerseVector operator *(double s) { + return TerseVector(x: s * x, y: s * y, z: s * z); + } + + TerseVector operator /(double s) { + return TerseVector(x: x / s, y: y / s, z: z / s); + } + + double quadrature() { + return x * x + y * y + z * z; + } + + double magnitude() { + return sqrt(quadrature()); + } + +// + + @override + bool operator ==(Object other) => + identical(this, other) || + (other is TerseVector && + runtimeType == other.runtimeType && + x == other.x && + y == other.y && + z == other.z); + + @override + int get hashCode => x.hashCode ^ y.hashCode ^ z.hashCode; + + @override + String toString() { + return 'TerseVector{' + ' x: $x,' + ' y: $y,' + ' z: $z,' + '}'; + } + + TerseVector copyWith({ + double? x, + double? y, + double? z, + }) { + return TerseVector( + x: x ?? this.x, + y: y ?? this.y, + z: z ?? this.z, + ); + } + + Map toMap() { + return { + 'x': this.x, + 'y': this.y, + 'z': this.z, + }; + } + + factory TerseVector.fromMap(Map map) { + return TerseVector( + x: map['x'] as double, + y: map['y'] as double, + z: map['z'] as double, + ); + } + +// +} diff --git a/source/dart/lib/src/astronomy_base.dart b/source/dart/lib/src/astronomy_base.dart deleted file mode 100644 index e8a6f159..00000000 --- a/source/dart/lib/src/astronomy_base.dart +++ /dev/null @@ -1,6 +0,0 @@ -// TODO: Put public facing types in this file. - -/// Checks if you are awesome. Spoiler: you are. -class Awesome { - bool get isAwesome => true; -} diff --git a/source/dart/test/astronomy_test.dart b/source/dart/test/astronomy_test.dart index 33da90bd..1c012fcb 100644 --- a/source/dart/test/astronomy_test.dart +++ b/source/dart/test/astronomy_test.dart @@ -2,15 +2,13 @@ import 'package:astronomy/astronomy.dart'; import 'package:test/test.dart'; void main() { - group('A group of tests', () { - final awesome = Awesome(); - - setUp(() { - // Additional setup goes here. - }); - - test('First Test', () { - expect(awesome.isAwesome, isTrue); - }); + test('TerseVector methods', () { + final ones = TerseVector(x: 1.0, y: 1.0, z: 1.0); + expect(ones, ones + TerseVector.zero); + expect(ones, ones + TerseVector.zero); + expect(TerseVector(x: 6.0, y: 8.0, z: 4.0), TerseVector(x: 3.0, y: 4.0, z: 2.0) * 2.0); + expect(TerseVector(x: -1.5, y: 2.0, z: -1.0), TerseVector(x: -3.0, y: 4.0, z: -2.0) / 2.0); + expect(29.0, TerseVector(x: -3.0, y: 4.0, z: -2.0).quadrature()); + expect(5.744562646538029, TerseVector(x: -2.0, y: -2.0, z: 5.0).magnitude()); }); }