List of usage examples for org.apache.commons.math3.geometry.euclidean.twod Vector2D Vector2D
public Vector2D(double a, Vector2D u)
From source file:de.thkwalter.et.ortskurve.Jakobimatrix2d.java
/** * Dieses Feld berechnet die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen). * //from w w w. j av a 2s . c o m * @param kreisparameter Die Parameterwerte der Kreisgleichungen. Das 0-te Element ist die x-Koordinate des * Kreismittelpunkts, das 1-te Element ist der Radius des Kreises. * * @return Die Jakobi-Matrix der Modellgleichungen. Der erste Index des Feldes luft ber die Gleichungen, der zweite * Index ber die Kreisparameter. * * @see org.apache.commons.math3.analysis.MultivariateMatrixFunction#value(double[]) */ @Override public double[][] value(double[] kreisparameter) { // Der Vektor fr den Mittelpunkt der Ortskurve wird erzeugt. Vector2D mittelpunkt = new Vector2D(kreisparameter[0], 0.0); // Das Feld fr die Jakobi-Matrix wird deklariert. double[][] jakobiMatrix = new double[this.messpunkte.length][2]; // In dieser Schleife wird die Jakobi-Matrix initialisiert. double abstandMesspunktMittelpunkt = Double.NaN; for (int i = 0; i < this.messpunkte.length; i++) { // Der Abstand des Messpunktes vom Mittelpunkt wird berechnet. abstandMesspunktMittelpunkt = this.messpunkte[i].distance(mittelpunkt); // Falls der Messpunkt mit dem Mittelpunkt identisch ist wird eine JSFAusnahme geworfen, da sonst das Inverse // des Abstands unendlich gro wird. Da der Vergleich zweier double-Wert jedoch sinnlos ist, wird der Abstand mit // einem Prozent des aktuell vermuteten Radius verglichen. if (abstandMesspunktMittelpunkt < 0.01 * kreisparameter[1]) { // Die Fehlermeldung fr den Entwickler wird erzeugt und protokolliert. String fehlermeldung = "Der Punkt " + this.messpunkte[i].toString() + " ist fast identisch mit dem " + " Mittelpunkt " + mittelpunkt.toString() + "!"; Jakobimatrix2d.logger.severe(fehlermeldung); // Die Ausnahme wird erzeugt und mit der Fehlermeldung fr den Benutzer initialisiert. String jsfMeldung = "Der Punkt " + this.messpunkte[i].toString() + " scheint in der Nhe des Kreismittelpunktes zu liegen! berprfen Sie bitte diesen Punkt."; ApplicationRuntimeException applicationRuntimeException = new ApplicationRuntimeException( jsfMeldung); throw applicationRuntimeException; } // Das Inverse des Abstands des Messpunkts vom Mittelpunkt wird berechnet. double inverserAbstandMesspunktMittelpunkt = 1.0 / abstandMesspunktMittelpunkt; // Die Elemente der Jakobi-Matrix werden initialisiert. jakobiMatrix[i][0] = inverserAbstandMesspunktMittelpunkt * (mittelpunkt.getX() - this.messpunkte[i].getX()); jakobiMatrix[i][1] = -1.0; } return jakobiMatrix; }
From source file:de.thkwalter.et.ortskurve.Jakobimatrix.java
/** * Dieses Feld berechnet die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen). * // www . ja v a 2s . c o m * @param kreisparameter Die Parameterwerte der Kreisgleichungen. Das 0-te Element ist die x-Koordinate des * Kreismittelpunkts, das 1-te Element ist die y-Koordinate des Kreismittelpunkts, das 2-te Element ist der * Radius des Kreises. * * @return Die Jakobi-Matrix der Modellgleichungen. Der erste Index des Feldes luft ber die Gleichungen, der zweite * Index ber die Kreisparameter. * * @see org.apache.commons.math3.analysis.MultivariateMatrixFunction#value(double[]) */ @Override public double[][] value(double[] kreisparameter) { // Der Vektor fr den Mittelpunkt der Ortskurve wird erzeugt. Vector2D mittelpunkt = new Vector2D(kreisparameter[0], kreisparameter[1]); // Das Feld fr die Jakobi-Matrix wird deklariert. double[][] jakobiMatrix = new double[this.messpunkte.length][3]; // In dieser Schleife wird die Jakobi-Matrix initialisiert. double abstandMesspunktMittelpunkt = Double.NaN; for (int i = 0; i < this.messpunkte.length; i++) { // Der Abstand des Messpunktes vom Mittelpunkt wird berechnet. abstandMesspunktMittelpunkt = this.messpunkte[i].distance(mittelpunkt); // Falls der Messpunkt mit dem Mittelpunkt identisch ist wird eine JSFAusnahme geworfen, da sonst das Inverse // des Abstands unendlich gro wird. Da der Vergleich zweier double-Wert jedoch sinnlos ist, wird der Abstand mit // einem Prozent des aktuell vermuteten Radius verglichen. if (abstandMesspunktMittelpunkt < 0.01 * kreisparameter[2]) { // Die Fehlermeldung fr den Entwickler wird erzeugt und protokolliert. String fehlermeldung = "Der Punkt " + this.messpunkte[i].toString() + " ist fast identisch mit dem " + " Mittelpunkt " + mittelpunkt.toString() + "!"; Jakobimatrix.logger.severe(fehlermeldung); // Die Ausnahme wird erzeugt und mit der Fehlermeldung fr den Benutzer initialisiert. String jsfMeldung = "Der Punkt " + this.messpunkte[i].toString() + " scheint in der Nhe des Kreismittelpunktes zu liegen! berprfen Sie bitte diesen Punkt."; ApplicationRuntimeException applicationRuntimeException = new ApplicationRuntimeException( jsfMeldung); throw applicationRuntimeException; } // Das Inverse des Abstands des Messpunkts vom Mittelpunkt wird berechnet. double inverserAbstandMesspunktMittelpunkt = 1.0 / abstandMesspunktMittelpunkt; // Die Elemente der Jakobi-Matrix werden initialisiert. jakobiMatrix[i][0] = inverserAbstandMesspunktMittelpunkt * (mittelpunkt.getX() - this.messpunkte[i].getX()); jakobiMatrix[i][1] = inverserAbstandMesspunktMittelpunkt * (mittelpunkt.getY() - this.messpunkte[i].getY()); jakobiMatrix[i][2] = -1.0; } return jakobiMatrix; }
From source file:haxball.networking.ConnectionHandler.java
public ConnectionHandler(@NonNull Socket socket, @NonNull Dimension fieldSize, @NonNull Goal goals[], byte id) { this.socket = socket; this.fieldSize = fieldSize; this.id = id; try {//from ww w. j a va2 s . c o m in = socket.getInputStream(); out = socket.getOutputStream(); // send dimension size out.write(serializeDimension(getFieldSize())); out.flush(); out.write(id); out.write(goals.length); for (Goal g : goals) { out.write(serializeGoal(g)); } // read name from client byte b[] = new byte[in.read()]; if (in.read(b) != b.length) { throw new IOException(); } name = new String(b, StandardCharsets.UTF_8); System.out.println("Connected client " + getSocket() + " as name " + name); // create player player = new Player(getId(), getName(), fieldSize); player.position = new Vector2D(100, 100); } catch (Exception ioe) { ioe.printStackTrace(); try { socket.close(); } catch (Exception e) { } } }
From source file:de.r2soft.empires.framework.test.ObjectTreeTest.java
@Test public void testMove() { Object obj = new Object(); tree.insert(new Vector2D(50, 50), obj); tree.move(new Vector2D(50, 50), new Vector2D(60, 60)); Object objT = tree.search(new Vector2D(60, 60)); Assert.assertEquals(obj, objT);// w w w . j a va2 s . c om }
From source file:de.r2soft.empires.framework.map.SolarSystem.java
/** Minimalistic constructor that only takes a Star and a claim. Claim can be null. */ public SolarSystem(Star star, Player claim) { this(star);//from w w w. j a v a2 s . c o m if (claim == null) logger.info("Why not use the base constructor next time?"); this.claim = claim; // Tree Demo objectPositions.insert(new Vector2D(10, 10), new Fleet(new HashSet<Ship>())); BaseObject object = objectPositions.nearest(new Vector2D(10, 10)); objectPositions.move(new Vector2D(10, 10), new Vector2D(20, 20)); }
From source file:haxball.networking.ServerMainLoop.java
@Override public void run() { resetField();//from w w w. j av a 2s. c o m byte score0 = 0, score1 = 0; while (!stopped) { // Move players for (Player player : players) { byte input = player.getLastInput(); float vx = 0; float vy = 0; if ((input & 0b00_00_00_01) != 0) { vy += speed; } if ((input & 0b00_00_00_10) != 0) { vx -= speed; } if ((input & 0b00_00_01_00) != 0) { vy -= speed; } if ((input & 0b00_00_10_00) != 0) { vx += speed; } if ((input & 0b00_01_00_00) != 0) { player.setShooting(true); } else { player.setShooting(false); } Vector2D v = new Vector2D(vx, vy); if (v.distance(Vector2D.ZERO) > 0) { v = v.normalize().scalarMultiply(speed); } player.velocity = player.velocity.add(v.subtract(player.velocity).scalarMultiply(acceleration)); player.position = player.position.add(player.velocity); } // Move ball ball.velocity = ball.velocity.scalarMultiply(1 - friction); ball.position = ball.position.add(ball.velocity); // Check for collisions for (Player p : players) { // collisions between players for (Player p0 : players) { if (!p.equals(p0)) { p.uncollide(p0); } } p.uncollide(ball); p.setInsideMap(true); if (p.isShooting() && p.position.distance(ball.position) < (p.getRadius() + ball.getRadius()) * 1.4f) { Vector2D norm = ball.position.subtract(p.position).normalize(); ball.velocity = ball.velocity.add(norm.scalarMultiply(shootingPower)); } } int result; if ((result = ball.setInsideMap(false)) >= 0) { if (result == 0) { score0++; } else { score1++; } resetField(); } // send position to every connection for (ConnectionHandler handler : connectionHandlers) { handler.writeState(ball, score0, score1, players); } // sleep try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } // while !stopped }
From source file:io.lonelyrobot.empires.fw.game.obj.BaseObject.java
/** * This function is called recursively from the {@link Star}, updating it's children * with an offset of {0, 0}. These children then move themselves according to the * position of their parent and then pass this offset to their children. These children * move themselves by the offset and then calculate their additional offset. * //from w w w . j a v a 2 s . c om * @param parentOffset */ public void updateOrbits(Vector2D parentOffset) { Vector2D offset = new Vector2D(0, 0); if (orbit != null && orbit.getParent() != null) { Vector2D p = orbit.getParent().getSolPos(); /** Calculate own offset */ double x = p.getX() + orbit.getRadius() * Math.cos(orbit.getStep()); double y = p.getY() + orbit.getRadius() * Math.sin(orbit.getStep()); orbit.setStep(orbit.getStep() + orbit.getStepSpeed()); Vector2D newPos = new Vector2D(x, y); newPos.add(parentOffset); // offset.add(new Vector2D(x, y)); /** Add the parent offset on top */ // offset.add(parentOffset); offset.add(newPos); /** Apply position to self */ this.setSolPos(newPos); } /** Only propagate orbital offsets if self is Orbitable */ if (!(this instanceof Orbitable)) return; /** Call function for all children with the new (accumilated) offset */ Tools.safe(orbit.getChildren()).forEach((i) -> i.updateOrbits(offset)); }
From source file:edu.stanford.cfuller.imageanalysistools.filter.VoronoiFilter.java
/** * Applies the Filter to the supplied Image. * @param im The Image to whose labeled region will be converted to a Voronoi diagram. *///from w w w . ja v a2 s . co m @Override public void apply(WritableImage im) { Map<Integer, Vector2D> regionCentroids = new java.util.HashMap<Integer, Vector2D>(); for (ImageCoordinate ic : im) { int label = (int) im.getValue(ic); if (label == 0) continue; if (!regionCentroids.containsKey(label)) { regionCentroids.put(label, new Vector2D(0, 0)); } regionCentroids.put(label, regionCentroids.get(label) .add(new Vector2D(ic.get(ImageCoordinate.X), ic.get(ImageCoordinate.Y)))); } Histogram h = new Histogram(im); java.util.List<Integer> regions = new java.util.ArrayList<Integer>(); for (Integer i : regionCentroids.keySet()) { regionCentroids.put(i, regionCentroids.get(i).scalarMultiply(1.0 / h.getCounts(i))); regions.add(i); } java.util.Collections.sort(regions); java.util.List<Vector2D> orderedCentroids = new java.util.ArrayList<Vector2D>(); for (Integer i : regions) { orderedCentroids.add(regionCentroids.get(i)); } WritableImage regionCentroidsIm = ImageFactory.createWritable(im.getDimensionSizes(), 0.0f); WritableImage diagram = ImageFactory.createWritable(im.getDimensionSizes(), 1.0f); ImageCoordinate cenTemp = ImageCoordinate.createCoordXYZCT(0, 0, 0, 0, 0); for (Integer i : regions) { Vector2D cen = regionCentroids.get(i); cenTemp.set(ImageCoordinate.X, (int) cen.getX()); cenTemp.set(ImageCoordinate.Y, (int) cen.getY()); regionCentroidsIm.setValue(cenTemp, i); } cenTemp.recycle(); for (ImageCoordinate ic : diagram) { if (isOnEightConnectedBoundary(orderedCentroids, ic)) { diagram.setValue(ic, 0.0f); } } LabelFilter lf = new LabelFilter(); OneToOneLabelBySeedFilter oolbsf = new OneToOneLabelBySeedFilter(); lf.apply(diagram); oolbsf.setReferenceImage(regionCentroidsIm); oolbsf.apply(diagram); im.copy(diagram); }
From source file:gdsc.core.utils.ConvexHull.java
/** * Create a new convex hull from the given coordinates. * * @param xbase//from w w w.ja v a 2 s. c o m * the x base coordinate (origin) * @param ybase * the y base coordinate (origin) * @param xCoordinates * the x coordinates * @param yCoordinates * the y coordinates * @param n * the number of coordinates * @return the convex hull * @throws NullPointerException * if the inputs are null * @throws ArrayIndexOutOfBoundsException * if the yCoordinates are smaller than the xCoordinates */ public static ConvexHull create(float xbase, float ybase, float[] xCoordinates, float[] yCoordinates, int n) { // This algorithm is not suitable for floating point coords //return createGiftWrap(xbase, ybase, xCoordinates, yCoordinates, n); // Use Apache Math to do this MonotoneChain chain = new MonotoneChain(); TurboList<Vector2D> points = new TurboList<Vector2D>(n); for (int i = 0; i < n; i++) points.add(new Vector2D(xbase + xCoordinates[i], ybase + yCoordinates[i])); ConvexHull2D hull = null; try { hull = chain.generate(points); } catch (ConvergenceException e) { } if (hull == null) return null; Vector2D[] v = hull.getVertices(); if (v.length == 0) return null; int size = v.length; float[] xx = new float[size]; float[] yy = new float[size]; int n2 = 0; for (int i = 0; i < size; i++) { xx[n2] = (float) v[i].getX(); yy[n2] = (float) v[i].getY(); n2++; } return new ConvexHull(0, 0, xx, yy); }
From source file:de.thkwalter.et.schlupfbezifferung.SchlupfresiduumTest.java
/** * Diese Methode initialisiert die Tests. * //from www. j a v a 2 s .c o m * @throws Exception */ @Before public void setUp() throws Exception { // Die in den Tests verwendeten Steigungen der Strahlen vom Inversionszentrum zu den Betriebspunkten werden erzeugt. this.testSteigungen = new double[] { -0.21148, -0.34511, -0.53453 }; // Die in den Tests verwendeten Betriebspunkte, die zur Bestimmung der Schlupfbezifferung verwendet werden, werden // erzeugt. this.testBetriebspunkte = new Betriebspunkt[3]; testBetriebspunkte[0] = new Betriebspunkt(1.8843, 0.22026); testBetriebspunkte[1] = new Betriebspunkt(1.6135, 1.2989); testBetriebspunkte[2] = new Betriebspunkt(1.6639, 2.7199); // Das in den Tests verwendete Inversionszentrum (in A) wird erzeugt. this.testInversionszentrum = new Vector2D(9.2570, -1.3389); // Der in den Tests verwendete Drehpunkt der Schlupfgerade (in A) wird erzeugt. this.testDrehpunktSchlupfgerade = new Vector2D(6.0768, -2.6562); // Der Prfling wird erzeugt. this.schlupfresiduum = new Schlupfresiduum(this.testSteigungen, this.testBetriebspunkte, this.testInversionszentrum, this.testDrehpunktSchlupfgerade); }