area.dart 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'dart:math' as math;
  2. import 'package:fis_measure/interfaces/date_types/point.dart';
  3. import 'package:fis_measure/interfaces/process/calculators/output.dart';
  4. import 'package:fis_measure/interfaces/process/items/measure_terms.dart';
  5. import 'package:fis_measure/process/primitives/ellipse.dart';
  6. import 'package:vid/us/vid_us_unit.dart';
  7. import '../primitives/poyline.dart';
  8. import 'calculator.dart';
  9. class PolyLineAreaCal extends Calculator<PolyLine, double> {
  10. PolyLineAreaCal(PolyLine ref) : super(ref);
  11. @override
  12. void calculate() {
  13. if (ref.feature == null) return;
  14. // TODO:xxx
  15. final viewport = ref.feature!.hostVisualArea!.viewport!;
  16. final points =
  17. ref.feature!.innerPoints.map((e) => viewport.convert(e)).toList();
  18. double value = clacArea(points);
  19. final outputItem = createOutput(MeasureTerms.Area, value, VidUsUnit.cm2);
  20. final description = "${ref.description}: ${roundDouble(value)}cm²";
  21. outputItem.updateDescription(description: description);
  22. output = outputItem;
  23. }
  24. static double clacArea(List<DPoint> points) {
  25. if (points.isEmpty) {
  26. return 0;
  27. }
  28. double sum = 0;
  29. var ax = points[0].x;
  30. var ay = points[0].y;
  31. for (var i = 1; i < points.length - 1; i++) {
  32. var bx = points[i].x;
  33. var by = points[i].y;
  34. var cx = points[i + 1].x;
  35. var cy = points[i + 1].y;
  36. sum += ax * by - ay * bx + ay * cx - ax * cy + bx * cy - cx * by;
  37. }
  38. return (-sum / 2).abs();
  39. }
  40. }