Skip to content

Commit

Permalink
Remove dangerous Point extensions which were used to cover up deeper …
Browse files Browse the repository at this point in the history
…issues.
  • Loading branch information
ignatz committed Nov 2, 2023
1 parent c89e959 commit eceb6dc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class MarkerLayer extends StatelessWidget {
)) continue;

// Apply map camera to marker position
final pos = pxPoint.subtract(map.pixelOrigin);
final pos = pxPoint - map.pixelOrigin;

yield Positioned(
key: m.key,
Expand Down
14 changes: 6 additions & 8 deletions lib/src/layer/overlay_image_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class OverlayImage extends BaseOverlayImage {
}) {
// northWest is not necessarily upperLeft depending on projection
final bounds = Bounds<double>(
camera.project(this.bounds.northWest).subtract(camera.pixelOrigin),
camera.project(this.bounds.southEast).subtract(camera.pixelOrigin),
camera.project(this.bounds.northWest) - camera.pixelOrigin,
camera.project(this.bounds.southEast) - camera.pixelOrigin,
);

return Positioned(
Expand Down Expand Up @@ -111,12 +111,10 @@ class RotatedOverlayImage extends BaseOverlayImage {
required Image child,
required MapCamera camera,
}) {
final pxTopLeft =
camera.project(topLeftCorner).subtract(camera.pixelOrigin);
final pxTopLeft = camera.project(topLeftCorner) - camera.pixelOrigin;
final pxBottomRight =
camera.project(bottomRightCorner).subtract(camera.pixelOrigin);
final pxBottomLeft =
camera.project(bottomLeftCorner).subtract(camera.pixelOrigin);
camera.project(bottomRightCorner) - camera.pixelOrigin;
final pxBottomLeft = camera.project(bottomLeftCorner) - camera.pixelOrigin;

/// calculate pixel coordinate of top-right corner by calculating the
/// vector from bottom-left to top-left and adding it to bottom-right
Expand All @@ -129,7 +127,7 @@ class RotatedOverlayImage extends BaseOverlayImage {

final vectorX = (pxTopRight - pxTopLeft) / bounds.size.x;
final vectorY = (pxBottomLeft - pxTopLeft) / bounds.size.y;
final offset = pxTopLeft.subtract(bounds.topLeft);
final offset = pxTopLeft - bounds.topLeft;

final a = vectorX.x;
final b = vectorX.y;
Expand Down
5 changes: 2 additions & 3 deletions lib/src/layer/tile_layer/wms_tile_layer_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ class WMSTileLayerOptions {
}

String getUrl(TileCoordinates coords, int tileSize, bool retinaMode) {
final tileSizePoint = Point(tileSize, tileSize);
final nwPoint = coords.scaleBy(tileSizePoint);
final sePoint = nwPoint + tileSizePoint;
final nwPoint = coords * tileSize;
final sePoint = nwPoint + Point<int>(tileSize, tileSize);
final nwCoords = crs.pointToLatLng(nwPoint, coords.z.toDouble());
final seCoords = crs.pointToLatLng(sePoint, coords.z.toDouble());
final nw = crs.projection.project(nwCoords);
Expand Down
5 changes: 2 additions & 3 deletions lib/src/map/camera/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ class MapCamera {
/// This will convert a latLng to a position that we could use with a widget
/// outside of FlutterMap layer space. Eg using a Positioned Widget.
Point<double> latLngToScreenPoint(LatLng latLng) {
final nonRotatedPixelOrigin =
(project(center, zoom) - nonRotatedSize / 2.0).round();
final nonRotatedPixelOrigin = project(center, zoom) - nonRotatedSize / 2.0;

var point = crs.latLngToPoint(latLng, zoom);

Expand All @@ -292,7 +291,7 @@ class MapCamera {
point = rotatePoint(mapCenter, point, counterRotation: false);
}

return point.subtract(nonRotatedPixelOrigin);
return point - nonRotatedPixelOrigin;
}

LatLng pointToLatLng(Point localPoint) {
Expand Down
44 changes: 1 addition & 43 deletions lib/src/misc/point_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,54 +67,12 @@ extension PointExtension<T extends num> on Point<T> {
/// Point<int>(1, 2).subtract(1.5) would cause a runtime error because the
/// resulting x/y values are doubles and the return value is a Point<int> since
/// the method returns Point<T>.
///
/// Note that division methods (unscaleBy and the / operator) are defined on
/// Point<T extends num> with a Point<double> return argument because division
/// always returns a double.
extension DoublePointExtension on Point<double> {
/// Subtract [other] from this Point.
@Deprecated('camera.pixelOrigin is now a Point<double>. Prefer operator-.')
Point<double> subtract(Point<num> other) {
return Point(x - other.x, y - other.y);
}

/// Add [other] to this Point.
Point<double> add(Point<num> other) {
return Point(x + other.x, y + other.y);
}

/// Create a new [Point] where [x] and [y] values are scaled by the respective
/// values in [other].
Point<double> scaleBy(Point<num> other) {
return Point<double>(x * other.x, y * other.y);
}
}

/// This extension contains methods which, if defined on Point<T extends num>,
/// could cause a runtime error when called on a Point<int> with a non-int
/// argument. An example:
///
/// Point<int>(1, 2).subtract(1.5) would cause a runtime error because the
/// resulting x/y values are doubles and the return value is a Point<int> since
/// the method returns Point<T>.
///
/// The methods in this extension only take Point<int> arguments to prevent
/// this.
extension IntegerPointExtension on Point<int> {
/// Subtract [other] from this Point.
Point<int> subtract(Point<int> other) {
return Point(x - other.x, y - other.y);
}

/// Add [other] to this Point.
Point<int> add(Point<int> other) {
return Point(x + other.x, y + other.y);
}

/// Create a new [Point] where [x] and [y] values are scaled by the respective
/// values in [other].
Point<int> scaleBy(Point<int> other) {
return Point<int>(x * other.x, y * other.y);
}
}

extension OffsetToPointExtension on Offset {
Expand Down

0 comments on commit eceb6dc

Please sign in to comment.