Saturday, May 16, 2009

test

package com.test; import java.util.List; import java.util.stream.DoubleStream; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.chart.Axis; import javafx.scene.chart.CategoryAxis; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.chart.XYChart.Data; import javafx.stage.Stage; public class SeriesChart extends Application { private LineChart lineChart; @Override public void start(Stage stage) { stage.setTitle("Line Chart Sample"); final CategoryAxis xAxis = new CategoryAxis(); final Axis yAxis = new NumberAxis(); xAxis.setLabel("X"); lineChart = new LineChart(xAxis,yAxis); // lineChart.getData().add(createSeries("geometric", Series.geomatricSeries(10, 1.4))); // lineChart.getData().add(createSeries("Arithmatic", Series.arithmaticSeries(100, 200))); // lineChart.getData().add(createSeries("Fibonacci", Series.fibonacciNumbers().mapToDouble(x -> x))); // lineChart.getData().add(createSeries("Primes", Series.primeNumbers().mapToDouble(x -> x * 100))); // lineChart.getData().add(createSeries("arithmeticoGeometric", Series.arithmeticoGeometric(100, 25, 1.2))); // lineChart.getData().add(createSeries("power Series", Series.powerSeries(DoubleStream.iterate(1.0d, i -> i * 1.2), 2.3, 1.0))); // lineChart.getData().add(createSeries("Collatz", Series.collatzSequence(649).asDoubleStream())); // lineChart.setTitle("Mathematical Series"); Scene scene = new Scene(lineChart,800,600); stage.setScene(scene); stage.show(); } public XYChart.Series createSeries(String name, DoubleStream series) { XYChart.Series line = new XYChart.Series<>(); line.setName(name); int[] holder = {0}; series.limit(23) .mapToObj(i -> new XYChart.Data(holder[0]++ + "", Double.valueOf(i))) .collect(() -> line.getData(), List::add, List::addAll); return line; } public static void main(String[] args) { launch(args); } } package com.test; import java.util.Random; import java.util.function.BiPredicate; import java.util.function.BooleanSupplier; public class MonteCarlo { private static Random random = new Random(); //whether the point is in circle of radius r with center at (r,r) public static BiPredicate inCircle(double r) { return (x, y) -> (Math.pow(x-r, 2) + Math.pow(y-r, 2)) <= Math.pow(r, 2); } public static BooleanSupplier randomPointInCircle() { return () -> inCircle(1.0).test(random.nextDouble(), random.nextDouble()); } public static double monteCarlo(BooleanSupplier experiment, int trials) { int pass = 0; int remaining = trials; while (remaining-- > 0) { pass += experiment.getAsBoolean() ? 1 : 0; } return pass/(double)trials; } public static double computePi(int trials) { return monteCarlo(randomPointInCircle(), trials) * 4; } public static int gcd(int m, int n) { return n == 0 ? m : gcd(n, m % n); } public static BooleanSupplier coprime() { return () -> gcd(random.nextInt(Integer.MAX_VALUE), random.nextInt(Integer.MAX_VALUE)) == 1; } public static double computePiCesaro(int trials) { return Math.sqrt(6 / monteCarlo(coprime(), trials)) ; } public static void main(String[] args) { // double piVal1 = computePi(10000000); // System.out.println(piVal1 + " vs " + Math.PI); double piVal2 = computePiCesaro(10000000); System.out.println(piVal2 + " vs " + Math.PI); } } package ui; import java.util.Iterator; import java.util.stream.Stream; import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.stage.Stage; import ui.VogelsModel.CartesianCoordinate; public class VogelsModelVisualization extends Application { private static final long SECOND = 1_000_000_000l; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Drawing Operations Test"); Group root = new Group(); Canvas canvas = new Canvas(900, 600); GraphicsContext gc = canvas.getGraphicsContext2D(); root.getChildren().add(canvas); primaryStage.setScene(new Scene(root)); primaryStage.show(); gc.setFill(Color.SADDLEBROWN); gc.setStroke(Color.BLACK); int x = 450; int y = 290; Stream points = VogelsModel.florets() .map(coor -> coor.move(x, y)) .limit(800); Iterator iter = points.iterator(); new AnimationTimer() { private long lastUpdate = 0; Color[] colors = {Color.DARKGREY, Color.TEAL, Color.BLACK}; int n = 0; @Override public void handle(long now) { if (now - lastUpdate >= SECOND / 10) { lastUpdate = now; if (iter.hasNext()) { CartesianCoordinate point = iter.next(); double radius = 14.5; //gc.setFill(colors[n % colors.length]); gc.fillOval(point.x, point.y, radius, radius); n++; } else { stop(); } } } }.start(); } } package ui; import java.util.stream.IntStream; import java.util.stream.Stream; public class VogelsModel { private static final double C = 10; private static final double GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5)); public static Stream florets() { return IntStream.iterate(0, i -> i + 1) .mapToObj(VogelsModel::floretLocation) .map(PolarCoordinate::toCartesian); } public static PolarCoordinate floretLocation(int n) { return new PolarCoordinate(C * Math.pow(n, 0.5), n * GOLDEN_ANGLE); } public interface Coordinate { } public static class PolarCoordinate implements Coordinate { private double r; private double θ; private PolarCoordinate(double r, double θ) { this.r = r; this.θ = θ; } public CartesianCoordinate toCartesian() { return new CartesianCoordinate(r * Math.sin(θ), r * Math.cos(θ)); } public static double degreeToRadian(double degree) { return degree * Math.PI / 180; } } public static class CartesianCoordinate implements Coordinate { public final double x; public final double y; private CartesianCoordinate(double x, double y) { this.x = x; this.y = y; } public PolarCoordinate toPolor() { return new PolarCoordinate(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), x == 0 ? 0 : Math.atan(y / x)); } public CartesianCoordinate move(int ox, int oy) { return new CartesianCoordinate(x + ox, y + oy); } } }