AI2 Component  (Version nb184)
Rectangle.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright © 2016-2017 Massachusetts Institute of Technology, All rights reserved.
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
25 
26 import org.locationtech.jts.geom.Geometry;
27 import org.osmdroid.util.GeoPoint;
28 
31 
36 @DesignerComponent(version = YaVersion.RECTANGLE_COMPONENT_VERSION,
37  category = ComponentCategory.MAPS,
38  description = "Rectangle")
39 @SimpleObject
40 public class Rectangle extends PolygonBase implements MapRectangle {
41  private double east = 0;
42  private double west = 0;
43  private double north = 0;
44  private double south = 0;
45 
46  private static final MapFeatureVisitor<Double> distanceComputation = new MapFeatureVisitor<Double>() {
47  @Override
48  public Double visit(MapMarker marker, Object... arguments) {
49  if ((Boolean) arguments[1]) {
50  return GeometryUtil.distanceBetweenCentroids(marker, (Rectangle) arguments[0]);
51  } else {
52  return GeometryUtil.distanceBetweenEdges(marker, (Rectangle) arguments[0]);
53  }
54  }
55 
56  @Override
57  public Double visit(MapLineString lineString, Object... arguments) {
58  if ((Boolean) arguments[1]) {
59  return GeometryUtil.distanceBetweenCentroids(lineString, (Rectangle) arguments[0]);
60  } else {
61  return GeometryUtil.distanceBetweenEdges(lineString, (Rectangle) arguments[0]);
62  }
63  }
64 
65  @Override
66  public Double visit(MapPolygon polygon, Object... arguments) {
67  if ((Boolean) arguments[1]) {
68  return GeometryUtil.distanceBetweenCentroids(polygon, (Rectangle) arguments[0]);
69  } else {
70  return GeometryUtil.distanceBetweenEdges(polygon, (Rectangle) arguments[0]);
71  }
72  }
73 
74  @Override
75  public Double visit(MapCircle circle, Object... arguments) {
76  if ((Boolean) arguments[1]) {
77  return GeometryUtil.distanceBetweenCentroids(circle, (Rectangle) arguments[0]);
78  } else {
79  return GeometryUtil.distanceBetweenEdges(circle, (Rectangle) arguments[0]);
80  }
81  }
82 
83  @Override
84  public Double visit(MapRectangle rectangle, Object... arguments) {
85  if ((Boolean) arguments[1]) {
86  return GeometryUtil.distanceBetweenCentroids(rectangle, (Rectangle) arguments[0]);
87  } else {
88  return GeometryUtil.distanceBetweenEdges(rectangle, (Rectangle) arguments[0]);
89  }
90  }
91  };
92 
94  super(container, distanceComputation);
95  container.addFeature(this);
96  }
97 
98  @Override
100  description = "Returns the type of the feature. For rectangles, this returns the text "
101  + "\"Rectangle\".")
102  public String Type() {
103  return MapFactory.MapFeatureType.TYPE_RECTANGLE;
104  }
105 
111  @Override
113  defaultValue = "0")
115  description = "The east edge of the rectangle, in decimal degrees east "
116  + "of the prime meridian.")
117  public void EastLongitude(double east) {
118  this.east = east;
119  clearGeometry();
121  }
122 
126  @Override
128  public double EastLongitude() {
129  return east;
130  }
131 
137  @Override
139  defaultValue = "0")
141  description = "The north edge of the rectangle, in decimal degrees north"
142  + " of the equator.")
143  public void NorthLatitude(double north) {
144  this.north = north;
145  clearGeometry();
147  }
148 
152  @Override
154  public double NorthLatitude() {
155  return north;
156  }
157 
163  @Override
165  defaultValue = "0")
167  description = "The south edge of the rectangle, in decimal degrees north"
168  + " of the equator.")
169  public void SouthLatitude(double south) {
170  this.south = south;
171  clearGeometry();
173  }
174 
178  @Override
180  public double SouthLatitude() {
181  return south;
182  }
183 
189  @Override
191  defaultValue = "0")
193  description = "The west edge of the rectangle, in decimal degrees east"
194  + " of the equator.")
195  public void WestLongitude(double west) {
196  this.west = west;
197  clearGeometry();
199  }
200 
204  @Override
206  public double WestLongitude() {
207  return west;
208  }
209 
213  @Override
214  @SimpleFunction(description = "Returns the center of the Rectangle as a list of the form " +
215  "(Latitude Longitude).")
216  public YailList Center() {
218  }
219 
223  @Override
224  @SimpleFunction(description = "Returns the bounding box of the Rectangle in the format " +
225  "((North West) (South East)).")
226  public YailList Bounds() {
227  YailList nw = YailList.makeList(new Double[] { north, west });
228  YailList se = YailList.makeList(new Double[] { south, east });
229  return YailList.makeList(new YailList[] { nw, se });
230  }
231 
239  @Override
240  @SimpleFunction(description = "Moves the Rectangle so that it is centered on the given " +
241  "latitude and longitude while attempting to maintain the width and height of the Rectangle " +
242  "as measured from the center to the edges.")
243  public void SetCenter(double latitude, double longitude) {
244  if (latitude < -90 || latitude > 90) {
245  container.$form().dispatchErrorOccurredEvent(this, "SetCenter", ErrorMessages.ERROR_INVALID_POINT, latitude, longitude);
246  return;
247  }
248  if (longitude < -180 || longitude > 180) {
249  container.$form().dispatchErrorOccurredEvent(this, "SetCenter", ErrorMessages.ERROR_INVALID_POINT, latitude, longitude);
250  return;
251  }
252  GeoPoint currentCenter = getCentroid();
253  GeoPoint northPoint = new GeoPoint(north, currentCenter.getLongitude());
254  GeoPoint southPoint = new GeoPoint(south, currentCenter.getLongitude());
255  GeoPoint eastPoint = new GeoPoint(currentCenter.getLatitude(), east);
256  GeoPoint westPoint = new GeoPoint(currentCenter.getLatitude(), west);
257  double latExtent2 = GeometryUtil.distanceBetween(northPoint, southPoint) / 2.0;
258  double longExtent2 = GeometryUtil.distanceBetween(eastPoint, westPoint) / 2.0;
259  currentCenter.setCoords(latitude, longitude);
260  north = currentCenter.destinationPoint(latExtent2, 0.0f).getLatitude();
261  south = currentCenter.destinationPoint(latExtent2, 180.0f).getLatitude();
262  east = currentCenter.destinationPoint(longExtent2, 90.0f).getLongitude();
263  west = currentCenter.destinationPoint(longExtent2, 270.0f).getLongitude();
264  clearGeometry();
266  }
267 
268  @Override
269  public <T> T accept(MapFeatureVisitor<T> visitor, Object... arguments) {
270  return visitor.visit(this, arguments);
271  }
272 
273  @Override
274  protected Geometry computeGeometry() {
275  return GeometryUtil.createGeometry(north, east, south, west);
276  }
277 
278  @Override
279  public void updateBounds(double north, double west, double south, double east) {
280  this.north = north;
281  this.west = west;
282  this.south = south;
283  this.east = east;
284  clearGeometry();
285  }
286 }
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_FLOAT
static final String PROPERTY_TYPE_FLOAT
Definition: PropertyTypeConstants.java:76
com.google.appinventor.components.runtime.util.YailList
Definition: YailList.java:26
com.google.appinventor.components.runtime.util.MapFactory.MapCircle
Definition: MapFactory.java:1028
com.google.appinventor.components.runtime.util.GeometryUtil.asYailList
static YailList asYailList(IGeoPoint point)
Definition: GeometryUtil.java:80
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer.addFeature
void addFeature(MapFeature feature)
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.Rectangle
Definition: Rectangle.java:40
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.util.MapFactory
Definition: MapFactory.java:30
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetween
static double distanceBetween(IGeoPoint a, IGeoPoint b)
Definition: GeometryUtil.java:229
com.google.appinventor.components.runtime.util.MapFactory.MapController.updateFeaturePosition
void updateFeaturePosition(MapMarker marker)
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.runtime.util.YailList.makeList
static YailList makeList(Object[] objects)
Definition: YailList.java:59
com.google.appinventor.components.runtime.util.MapFactory.MapLineString
Definition: MapFactory.java:1373
com.google.appinventor.components.runtime.util.MapFactory.MapPolygon
Definition: MapFactory.java:1410
com.google.appinventor.components.runtime.Map.getController
MapController getController()
Definition: Map.java:667
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenEdges
static double distanceBetweenEdges(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:249
com.google.appinventor.components.runtime.Rectangle.SetCenter
void SetCenter(double latitude, double longitude)
Definition: Rectangle.java:243
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.Rectangle.computeGeometry
Geometry computeGeometry()
Definition: Rectangle.java:274
com.google.appinventor.components.runtime.util.MapFactory.MapRectangle
Definition: MapFactory.java:1099
com.google.appinventor.components.runtime.PolygonBase
Definition: PolygonBase.java:17
com.google.appinventor.components.runtime.util.GeometryUtil.distanceBetweenCentroids
static double distanceBetweenCentroids(MapMarker marker, MapLineString lineString)
Definition: GeometryUtil.java:327
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor.visit
T visit(MapMarker marker, Object... arguments)
com.google.appinventor.components.runtime.Rectangle.EastLongitude
double EastLongitude()
Definition: Rectangle.java:128
com.google.appinventor.components.runtime.MapFeatureBase.container
MapFeatureContainer container
Definition: MapFeatureBase.java:33
com.google.appinventor.components.runtime.Rectangle.Type
String Type()
Definition: Rectangle.java:102
com.google.appinventor.components.runtime.MapFeatureBase.map
Map map
Definition: MapFeatureBase.java:34
com.google.appinventor.components.runtime.util.MapFactory.MapMarker
Definition: MapFactory.java:1205
com.google.appinventor.components.runtime.Rectangle.Rectangle
Rectangle(MapFactory.MapFeatureContainer container)
Definition: Rectangle.java:93
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.GeometryUtil
Definition: GeometryUtil.java:41
com.google.appinventor.components.runtime.Rectangle.Bounds
YailList Bounds()
Definition: Rectangle.java:226
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Rectangle.updateBounds
void updateBounds(double north, double west, double south, double east)
Definition: Rectangle.java:279
com.google.appinventor.components.runtime.util.GeometryUtil.createGeometry
static Geometry createGeometry(GeoPoint point)
Definition: GeometryUtil.java:120
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Rectangle.NorthLatitude
double NorthLatitude()
Definition: Rectangle.java:154
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Rectangle.SouthLatitude
double SouthLatitude()
Definition: Rectangle.java:180
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureType
Definition: MapFactory.java:1483
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.util.MapFactory.MapFeature.getCentroid
GeoPoint getCentroid()
com.google
com
com.google.appinventor.components.runtime.MapFeatureBase.clearGeometry
final synchronized void clearGeometry()
Definition: MapFeatureBase.java:430
com.google.appinventor.components.runtime.Rectangle.WestLongitude
double WestLongitude()
Definition: Rectangle.java:206
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_INVALID_POINT
static final int ERROR_INVALID_POINT
Definition: ErrorMessages.java:234
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureContainer
Definition: MapFactory.java:800
com.google.appinventor.components.runtime.Rectangle.Center
YailList Center()
Definition: Rectangle.java:216
com.google.appinventor.components.runtime.util.MapFactory.MapFeatureVisitor
Definition: MapFactory.java:745
com.google.appinventor.components.annotations.PropertyCategory.APPEARANCE
APPEARANCE
Definition: PropertyCategory.java:16
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor