commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t.@apache.org
Subject svn commit: r1563409 - in /commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry: ConvexHullExample.java EnclosingBallExample.java
Date Sat, 01 Feb 2014 12:50:42 GMT
Author: tn
Date: Sat Feb  1 12:50:41 2014
New Revision: 1563409

URL: http://svn.apache.org/r1563409
Log:
Add EnclosingBall example, update convex hull.

Added:
    commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
  (with props)
Modified:
    commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java

Modified: commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java?rev=1563409&r1=1563408&r2=1563409&view=diff
==============================================================================
--- commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java
(original)
+++ commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java
Sat Feb  1 12:50:41 2014
@@ -16,7 +16,8 @@ import javax.swing.JComponent;
 
 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
 import org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHull2D;
-import org.apache.commons.math3.geometry.euclidean.twod.hull.GrahamScan;
+import org.apache.commons.math3.geometry.euclidean.twod.hull.ConvexHullGenerator2D;
+import org.apache.commons.math3.geometry.euclidean.twod.hull.MonotoneChain;
 import org.apache.commons.math3.random.MersenneTwister;
 import org.apache.commons.math3.random.RandomGenerator;
 import org.apache.commons.math3.userguide.ExampleUtils;
@@ -28,7 +29,7 @@ import org.apache.commons.math3.userguid
 public class ConvexHullExample {
 
     public static List<Vector2D> createRandomPoints(int size) {
-        RandomGenerator random = new MersenneTwister(10);
+        RandomGenerator random = new MersenneTwister();
 
         // create the cloud container
         List<Vector2D> points = new ArrayList<Vector2D>(size);
@@ -43,12 +44,12 @@ public class ConvexHullExample {
     public static class Display extends ExampleFrame {
         
         public Display() {
-            setTitle("Commons-Math: Convex Hull examples");
+            setTitle("Commons-Math: Convex Hull example");
             setSize(400, 400);
             
             setLayout(new FlowLayout());
             
-            GrahamScan generator = new GrahamScan();
+            ConvexHullGenerator2D generator = new MonotoneChain();
             Collection<Vector2D> cloud = createRandomPoints(150);
             ConvexHull2D hull = generator.generate(cloud);
 

Added: commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java?rev=1563409&view=auto
==============================================================================
--- commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
(added)
+++ commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
Sat Feb  1 12:50:41 2014
@@ -0,0 +1,127 @@
+package org.apache.commons.math3.userguide.geometry;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.RenderingHints;
+import java.awt.geom.Ellipse2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import javax.swing.JComponent;
+
+import org.apache.commons.math3.geometry.enclosing.Encloser;
+import org.apache.commons.math3.geometry.enclosing.EnclosingBall;
+import org.apache.commons.math3.geometry.enclosing.WelzlEncloser;
+import org.apache.commons.math3.geometry.euclidean.twod.DiskGenerator;
+import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D;
+import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+import org.apache.commons.math3.random.MersenneTwister;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.userguide.ExampleUtils;
+import org.apache.commons.math3.userguide.ExampleUtils.ExampleFrame;
+
+/**
+ * Shows how to generate the convex hull of a point cloud in 2D.
+ */
+public class EnclosingBallExample {
+
+    public static List<Vector2D> createRandomPoints(int size) {
+        RandomGenerator random = new MersenneTwister();
+
+        // create the cloud container
+        List<Vector2D> points = new ArrayList<Vector2D>(size);
+        // fill the cloud with a random distribution of points
+        for (int i = 0; i < size; i++) {
+            points.add(new Vector2D(random.nextDouble() - 0.5, random.nextDouble() - 0.5));
+        }
+        return points;
+    }
+
+    @SuppressWarnings("serial")
+    public static class Display extends ExampleFrame {
+        
+        public Display() {
+            setTitle("Commons-Math: Enclosing Ball example");
+            setSize(400, 400);
+            
+            setLayout(new FlowLayout());
+
+            Collection<Vector2D> cloud = createRandomPoints(150);
+            Encloser<Euclidean2D, Vector2D> encloser = new WelzlEncloser<Euclidean2D,
Vector2D>(1e-10, new DiskGenerator());
+            EnclosingBall<Euclidean2D, Vector2D> ball = encloser.enclose(cloud);
+
+            add(new Plot(cloud, ball));
+        }
+    }
+
+    @SuppressWarnings("serial")
+    public static class Plot extends JComponent {
+
+        private static double PAD = 10;
+
+        private Iterable<Vector2D> cloud;
+        private EnclosingBall<Euclidean2D, Vector2D> ball;
+
+        public Plot(Iterable<Vector2D> cloud, EnclosingBall<Euclidean2D, Vector2D>
ball) {
+            this.cloud = cloud;
+            this.ball = ball;
+        }
+        
+        @Override
+        protected void paintComponent(Graphics g) {
+            super.paintComponent(g);
+            Graphics2D g2 = (Graphics2D)g;
+            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+                                RenderingHints.VALUE_ANTIALIAS_ON);
+
+            int w = getWidth();
+            int h = getHeight();
+
+            g2.clearRect(0, 0, w, h);
+            
+            g2.setPaint(Color.black);
+            g2.drawRect(0, 0, w - 1, h - 1);
+            
+            for (Vector2D point : cloud) {
+                drawPoint(g2, point, w, h);
+            }
+            
+            g.setColor(Color.RED);
+            drawCircle(g2, ball.getCenter(), ball.getRadius(), w, h);
+        }        
+
+        private void drawPoint(Graphics2D g2, Vector2D point, int width, int height) {
+            Vector2D p = transform(point, width, height);
+            double[] arr = p.toArray();
+            g2.draw(new Rectangle2D.Double(arr[0] - 1, arr[1] - 1, 2, 2));
+        }
+
+        public void drawCircle(Graphics2D g2, Vector2D center, double radius, int width,
int height) {
+            Vector2D c = transform(center, width, height);
+            double[] arr = c.toArray();
+            double rx = (radius) / 2 * (width - 2 * PAD);
+            double ry = (radius) / 2 * (height - 2 * PAD);
+            g2.draw(new Ellipse2D.Double(arr[0] - rx, arr[1] - ry, rx * 2, ry * 2));
+        }
+
+        @Override
+        public Dimension getPreferredSize() {
+            return new Dimension(300, 300);
+        }
+
+        private Vector2D transform(Vector2D point, int width, int height) {
+            double[] arr = point.toArray();
+            return new Vector2D(new double[] { PAD + (arr[0] + 0.5) / 2 * (width - 2 * PAD)
+ width / 4,
+                                               height - PAD - (arr[1] + 0.5) / 2 * (height
- 2 * PAD) - height / 4 });
+        }
+    }
+
+    public static void main(String[] args) {
+        ExampleUtils.showExampleFrame(new Display());
+    }
+}

Propchange: commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
------------------------------------------------------------------------------
    svn:keywords = Id Revision HeadURL

Propchange: commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message