Author: tn
Date: Sun Feb 9 22:10:04 2014
New Revision: 1566450
URL: http://svn.apache.org/r1566450
Log:
Simplify geometry examples by using piccolo2d.
Added:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
(with props)
Removed:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/ConvexHullExample.java
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/EnclosingBallExample.java
Modified:
commons/proper/math/trunk/src/userguide/pom.xml
Added:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java?rev=1566450&view=auto
==============================================================================
---
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
(added)
+++
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
Sun Feb 9 22:10:04 2014
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.userguide.geometry;
+
+import java.awt.Color;
+import java.awt.geom.Point2D;
+import java.util.ArrayList;
+import java.util.List;
+
+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.Segment;
+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.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.util.FastMath;
+import org.piccolo2d.PCamera;
+import org.piccolo2d.PCanvas;
+import org.piccolo2d.PNode;
+import org.piccolo2d.event.PBasicInputEventHandler;
+import org.piccolo2d.event.PInputEvent;
+import org.piccolo2d.extras.PFrame;
+import org.piccolo2d.nodes.PPath;
+import org.piccolo2d.nodes.PText;
+
+/**
+ * Simple example illustrating some parts of the geometry package.
+ *
+ * TODO:
+ * - add user interface to re-generate points
+ * - select convex hull algorithm
+ * - select tolerance level
+ * - allow editing of the point set
+ * - pre-defined shapes, e.g. circle, cross, ...
+ */
+public class GeometryExample extends PFrame {
+
+ private static final long serialVersionUID = 1L;
+
+ public GeometryExample() {
+ this(null);
+ }
+
+ public GeometryExample(final PCanvas aCanvas) {
+ super("Geometry example", false, aCanvas);
+ setSize(600, 600);
+ }
+
+ 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(FastMath.round(random.nextDouble() * 400 +
100),
+ FastMath.round(random.nextDouble() * 400 +
100)));
+ }
+ return points;
+ }
+
+ public void initialize() {
+ List<Vector2D> points = createRandomPoints(100);
+ PNode pointSet = new PNode();
+ for (Vector2D point : points) {
+ final PNode n1 = PPath.createEllipse(point.getX() - 1,
point.getY() - 1, 2, 2);
+ n1.addAttribute("tooltip", point);
+ n1.setPaint(Color.gray);
+ pointSet.addChild(n1);
+ }
+
+ getCanvas().getLayer().addChild(pointSet);
+
+ ConvexHullGenerator2D generator = new MonotoneChain(true, 1e-6);
+ ConvexHull2D hull = generator.generate(points);
+
+ PNode hullNode = new PNode();
+ for (Vector2D vertex : hull.getVertices()) {
+ final PPath node = PPath.createEllipse(vertex.getX() - 1,
vertex.getY() - 1, 2, 2);
+ node.addAttribute("tooltip", vertex);
+ node.setPaint(Color.red);
+ node.setStrokePaint(Color.red);
+ hullNode.addChild(node);
+ }
+
+ for (Segment line : hull.getLineSegments()) {
+ final PPath node = PPath.createLine(line.getStart().getX(),
line.getStart().getY(),
+ line.getEnd().getX(),
line.getEnd().getY());
+ node.setPaint(Color.red);
+ node.setStrokePaint(Color.red);
+ hullNode.addChild(node);
+ }
+
+ getCanvas().getLayer().addChild(hullNode);
+
+ Encloser<Euclidean2D, Vector2D> encloser = new
WelzlEncloser<Euclidean2D, Vector2D>(1e-10, new DiskGenerator());
+ EnclosingBall<Euclidean2D, Vector2D> ball = encloser.enclose(points);
+
+ final double radius = ball.getRadius();
+ PPath ballNode = PPath.createEllipse(ball.getCenter().getX() - radius,
ball.getCenter().getY() - radius, radius * 2, radius * 2);
+ ballNode.setTransparency(1.0f);
+ ballNode.setStrokePaint(Color.blue);
+ getCanvas().getLayer().addChild(0, ballNode);
+
+ final PCamera camera = getCanvas().getCamera();
+
+ final PText tooltipNode = new PText();
+
+ tooltipNode.setPickable(false);
+ camera.addChild(tooltipNode);
+
+ camera.addInputEventListener(new PBasicInputEventHandler() {
+ public void mouseMoved(final PInputEvent event) {
+ updateToolTip(event);
+ }
+
+ public void mouseDragged(final PInputEvent event) {
+ updateToolTip(event);
+ }
+
+ public void updateToolTip(final PInputEvent event) {
+ final PNode n = event.getPickedNode();
+ final Object object = (Object) n.getAttribute("tooltip");
+ if (object != null) {
+ final String tooltipString = object.toString();
+ final Point2D p = event.getCanvasPosition();
+
+ event.getPath().canvasToLocal(p, camera);
+
+ tooltipNode.setText(tooltipString);
+ tooltipNode.setOffset(p.getX() + 8, p.getY() - 8);
+ } else {
+ tooltipNode.setText(null);
+ }
+ }
+ });
+ }
+
+ public static void main(final String[] argv) {
+ new GeometryExample();
+ }
+}
\ No newline at end of file
Propchange:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange:
commons/proper/math/trunk/src/userguide/java/org/apache/commons/math3/userguide/geometry/GeometryExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: commons/proper/math/trunk/src/userguide/pom.xml
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/userguide/pom.xml?rev=1566450&r1=1566449&r2=1566450&view=diff
==============================================================================
--- commons/proper/math/trunk/src/userguide/pom.xml (original)
+++ commons/proper/math/trunk/src/userguide/pom.xml Sun Feb 9 22:10:04 2014
@@ -56,9 +56,19 @@
<dependency>
<groupId>com.xeiam.xchart</groupId>
<artifactId>xchart</artifactId>
- <version>2.2.1</version>
+ <version>2.3.0</version>
</dependency>
<dependency>
+ <groupId>org.piccolo2d</groupId>
+ <artifactId>piccolo2d-core</artifactId>
+ <version>3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.piccolo2d</groupId>
+ <artifactId>piccolo2d-extras</artifactId>
+ <version>3.0</version>
+ </dependency>
+ <dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>