Worldwind public release 0.2
[worldwind-tracker.git] / gov / nasa / worldwind / geom / SurfacePolygon.java
blob6d9c4e914b3602b4ab4882f6948f211cf5ec9cb0
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.geom;
9 import java.awt.*;
10 import java.awt.geom.*;
11 import java.awt.image.*;
12 import java.util.*;
14 /**
15 * @author tag
16 * @version $Id: SurfacePolygon.java 1688 2007-05-02 22:03:39Z tgaskins $
18 public class SurfacePolygon extends SurfaceShape
21 public SurfacePolygon(Iterable<LatLon> positions, Color color, Color borderColor)
23 super(positions, color, borderColor);
26 public SurfacePolygon(Iterable<LatLon> positions)
28 super(positions, null, null);
31 protected final BufferedImage drawShape(BufferedImage image)
33 double minLat = this.getSector().getMinLatitude().getDegrees();
34 double minLon = this.getSector().getMinLongitude().getDegrees();
35 double dLat = this.getSector().getDeltaLatDegrees();
36 double dLon = this.getSector().getDeltaLonDegrees();
38 double latScale = dLat > 0 ? image.getHeight() / dLat : 0;
39 double lonScale = dLon > 0 ? image.getWidth() / dLon : 0;
41 GeneralPath path = new GeneralPath();
43 Iterator<LatLon> positions = this.getPositions().iterator();
44 if (!positions.hasNext())
45 return image;
47 LatLon pos = positions.next();
48 path.moveTo(
49 (float) Math.min(lonScale * (pos.getLongitude().getDegrees() - minLon), image.getWidth() - 1),
50 (float) Math.min(latScale * (pos.getLatitude().getDegrees() - minLat), image.getHeight() - 1));
52 double delta = 1d / this.getNumEdgeIntervals();
53 while (positions.hasNext())
55 LatLon posNext = positions.next();
56 for (int i = 0; i < this.getNumEdgeIntervals(); i++)
58 LatLon p = LatLon.interpolate(i * delta, pos, posNext);
59 path.lineTo(
60 (float) Math.min(lonScale * (p.getLongitude().getDegrees() - minLon), image.getWidth() - 1),
61 (float) Math.min(latScale * (p.getLatitude().getDegrees() - minLat), image.getHeight() - 1));
64 // Set the last point directly to terminate any round-off error in the iteration above.
65 path.lineTo(
66 (float) Math.min(lonScale * (posNext.getLongitude().getDegrees() - minLon), image.getWidth() - 1),
67 (float) Math.min(latScale * (posNext.getLatitude().getDegrees() - minLat), image.getHeight() - 1));
69 pos = posNext;
72 Graphics2D g2 = image.createGraphics();
74 if (this.isAntiAlias())
75 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
77 if (this.isDrawInterior())
79 g2.setPaint(this.getPaint());
80 g2.fill(path);
83 if (this.isDrawBorder())
85 g2.setPaint(this.getBorderColor());
86 g2.setStroke(this.getStroke());
87 g2.draw(path);
90 return image;