public static void analysis(Vector X, Supplier<double[]> function) { long start = System.nanoTime(); double[] r = function.get(); long end = System.nanoTime(); System.out.println("Time:" + (end - start) / 1000000f + "ms"); Vector R = new Vector(r); System.out.println("Norm2:" + X.sub(R).getNorm2()); pw.print(","); pw.print((end - start) / 1000000f); pw.print(","); pw.print(X.sub(R).getNorm2()); pw.println(); pw.flush(); }
public void toAt(Vector cam, Vector obj) { if (cam == null || obj == null) throw new NullPointerException(); dir.set(0, 0, 0); dir.set(obj); dir.sub(cam); dir.mult(-1f); dir.norm(); dir.get(dirArray); right.set(0, 0, 0); right.cross(worldUp, dir); right.norm(); right.get(rightArray); up.set(0, 0, 0); up.cross(dir, right); up.norm(); up.get(upArray); set( rightArray[0], rightArray[1], rightArray[2], upArray[0], upArray[1], upArray[2], dirArray[0], dirArray[1], dirArray[2]); }
public static Matrix lookAtLH(Vector eye, Vector target, Vector up) { Matrix res = new Matrix(); Vector zaxis = Vector.sub(target, eye).normalize(); if (zaxis.length() == 0) zaxis.Z = 1; Vector xaxis = Vector.cross(up, zaxis).normalize(); if (xaxis.length() == 0) { zaxis.X += 0.000001; xaxis = Vector.cross(up, zaxis).normalize(); } Vector yaxis = Vector.cross(zaxis, xaxis); res.m[0] = xaxis.X; res.m[1] = xaxis.Y; res.m[2] = xaxis.Z; res.m[3] = -Vector.dot(xaxis, eye); res.m[4] = yaxis.X; res.m[5] = yaxis.Y; res.m[6] = yaxis.Z; res.m[7] = -Vector.dot(yaxis, eye); res.m[8] = zaxis.X; res.m[9] = zaxis.Y; res.m[10] = zaxis.Z; res.m[11] = -Vector.dot(zaxis, eye); // already set during initialization of matrix /* * res.m[12] = 0; res.m[13] = 0; res.m[14] = 0; res.m[15] = 1; */ return res; }
private void drawLine(Graphics g, Vector p, Vector q) { // FIXME: maybe eliminate class instantiations // from this method for performance reasons: // use static point arrays for transformations Vector u = rotation.apply(p.sub(origin)); Vector v = rotation.apply(q.sub(origin)); Point2D.Double from = new Point2D.Double(u.getX(), u.getZ()); Point2D.Double to = new Point2D.Double(v.getX(), v.getZ()); Point ifrom = new Point(); Point ito = new Point(); transform.transform(from, ifrom); transform.transform(to, ito); // g.drawLine(xInt(u.getX()),yInt(u.getZ()), // xInt(v.getX()),yInt(v.getZ())); g.drawLine(ifrom.x, ifrom.y, ito.x, ito.y); }
public void update(float mouseX, float mouseY) { Vector d = new Vector(0, 0); for (int i = balls.size() - 1; i >= 0; i--) { Ball ball = balls.get(i); d.x = mouseX - ball.pos.x; d.y = mouseY - ball.pos.y; if (d.magSquared() < 100 * 100) { Vector.sub(ball.pos, d, ball.goal); } else { ball.goal.set(ball.startPos); } ball.update(); } }
@Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); int[] colors = new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW}; // color = colors[new Random().nextInt(colors.length)]; // radius = new Random().nextInt(30) + 50; // 引力 Vector touch = new Vector(x, y); acc = Vector.sub(touch, loca); acc.normalize(); acc.mult(15.0f); return super.onTouchEvent(event); }
public static Line create(Vector p1, Vector p2, Vector v1, Vector v2, int f) { Function func; switch (f) { case Function.CURVE: func = new Curve(p1, p2, v1, v2); break; case Function.CURVE_LL: v1 = new Vector(0, 0); func = new Curve(p1, p2, v1, v2); break; case Function.CURVE_LR: v2 = new Vector(0, 0); func = new Curve(p1, p2, v1, v2); break; case Function.CURVE_T: v2 = Vector.sub(Vector.add(p1, v1), p2); func = new Curve(p1, p2, v1, v2); break; case Function.LINEAR: func = new Linear(p1, p2); break; case Function.HALF_SINE: func = new HalfSine(p1, p2, v1); break; case Function.CONSTANT: func = new Constant(p1); break; default: return null; } float dx = p2.x - p1.x; float step; if (dx > 0) { step = 1 / dx; } else { step = 0; } return new Line(func, step); }