@Override public Canvas clipRect(float x, float y, float width, final float height) { final int cx = MathUtil.ifloor(x), cy = MathUtil.ifloor(y); final int cwidth = MathUtil.iceil(width), cheight = MathUtil.iceil(height); currentState().clipper = new JavaCanvasState.Clipper() { public void setClip(Graphics2D g2d) { g2d.setClip(cx, cy, cwidth, cheight); } }; return this; }
@Override public Surface setAlpha(float alpha) { int ialpha = (int)(0xFF * MathUtil.clamp(alpha, 0, 1)); this.tint = (ialpha << 24) | (tint & 0xFFFFFF); return this; }
/** * Linearly interpolates between two angles, taking the shortest path around the circle. * This assumes that both angles are in [-pi, +pi]. */ public static float lerpa (float a1, float a2, float t) { float ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2); float d = Math.abs(a2 - a1), md = Math.abs(ma1 - ma2); return (d <= md) ? lerp(a1, a2, t) : mirrorAngle(lerp(ma1, ma2, t)); }
private static Bitmap createErrorBitmap(float fwidth, float fheight) { int height = MathUtil.iceil(fheight), width = MathUtil.iceil(fwidth); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); android.graphics.Canvas c = new android.graphics.Canvas(bitmap); android.graphics.Paint p = new android.graphics.Paint(); p.setColor(android.graphics.Color.RED); for (int yy = 0; yy <= height / 15; yy++) { for (int xx = 0; xx <= width / 45; xx++) { c.drawText("ERROR", xx * 45, yy * 15, p); } } return bitmap; } }
/** * Adds {@code child} to this group layer, adjusting its translation (relative to this group * layer) to {@code floor(tx), floor(ty)}. This is useful for adding layers which display text a * text can become blurry if it is positioned on sub-pixel boundaries. */ public void addFloorAt (Layer child, float tx, float ty) { add(child.setTranslation(MathUtil.ifloor(tx), MathUtil.ifloor(ty))); }
/** * Sets this to a random rotation obtained from a completely uniform distribution. */ public Quaternion randomize (Random rand) { // pick angles according to the surface area distribution return fromAngles(MathUtil.lerp(-FloatMath.PI, +FloatMath.PI, rand.nextFloat()), FloatMath.asin(MathUtil.lerp(-1f, +1f, rand.nextFloat())), MathUtil.lerp(-FloatMath.PI, +FloatMath.PI, rand.nextFloat())); }
/** * Returns the (shortest) difference between two angles, assuming that both angles are in * [-pi, +pi]. */ public static float angularDifference (float a1, float a2) { float ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2); float diff = a1 - a2, mdiff = ma2 - ma1; return (Math.abs(diff) < Math.abs(mdiff)) ? diff : mdiff; }
private static Bitmap createErrorBitmap(float fwidth, float fheight) { int height = MathUtil.iceil(fheight), width = MathUtil.iceil(fwidth); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); android.graphics.Canvas c = new android.graphics.Canvas(bitmap); android.graphics.Paint p = new android.graphics.Paint(); p.setColor(android.graphics.Color.RED); for (int yy = 0; yy <= height / 15; yy++) { for (int xx = 0; xx <= width / 45; xx++) { c.drawText("ERROR", xx * 45, yy * 15, p); } } return bitmap; } }
/** Returns the supplied length scaled by our scale factor and rounded down. */ public int scaledFloor(float length) { return MathUtil.ifloor(scaled(length)); }
/** * Sets this to a random rotation obtained from a completely uniform distribution. */ public Quaternion randomize (Random rand) { // pick angles according to the surface area distribution return fromAngles(MathUtil.lerp(-FloatMath.PI, +FloatMath.PI, rand.nextFloat()), FloatMath.asin(MathUtil.lerp(-1f, +1f, rand.nextFloat())), MathUtil.lerp(-FloatMath.PI, +FloatMath.PI, rand.nextFloat())); }
/** * Returns the (shortest) difference between two angles, assuming that both angles are in * [-pi, +pi]. */ public static float angularDifference (float a1, float a2) { float ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2); float diff = a1 - a2, mdiff = ma2 - ma1; return (Math.abs(diff) < Math.abs(mdiff)) ? diff : mdiff; }
@Override public Canvas clipRect(float x, float y, float width, final float height) { final int cx = MathUtil.ifloor(x), cy = MathUtil.ifloor(y); final int cwidth = MathUtil.iceil(width), cheight = MathUtil.iceil(height); currentState().clipper = new JavaCanvasState.Clipper() { public void setClip(Graphics2D gfx) { gfx.clipRect(cx, cy, cwidth, cheight); } }; return this; }
protected BufferedImage scaleImage(BufferedImage image, float viewImageRatio) { int swidth = MathUtil.iceil(viewImageRatio * image.getWidth()); int sheight = MathUtil.iceil(viewImageRatio * image.getHeight()); BufferedImage scaled = new BufferedImage(swidth, sheight, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D gfx = scaled.createGraphics(); gfx.drawImage(image.getScaledInstance(swidth, sheight, java.awt.Image.SCALE_SMOOTH), 0, 0, null); gfx.dispose(); return scaled; }
/** Sets the alpha component of {@code tint} to {@code alpha}. * @return the new tint. */ public static int setAlpha(int tint, float alpha) { int ialpha = (int)(0xFF * MathUtil.clamp(alpha, 0, 1)); return (ialpha << 24) | (tint & 0xFFFFFF); }
/** Returns the supplied length scaled by our scale factor and rounded down. */ public int scaledFloor(float length) { return MathUtil.ifloor(scaled(length)); }
/** * Linearly interpolates between two angles, taking the shortest path around the circle. * This assumes that both angles are in [-pi, +pi]. */ public static float lerpa (float a1, float a2, float t) { float ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2); float d = Math.abs(a2 - a1), md = Math.abs(ma1 - ma2); return (d <= md) ? lerp(a1, a2, t) : mirrorAngle(lerp(ma1, ma2, t)); }
@Override protected void paint (Clock clock, Entities entities) { float alpha = clock.alpha; Point op = _oldPos, p = _pos; for (int ii = 0, ll = entities.size(); ii < ll; ii++) { int eid = entities.get(ii); // interpolate between opos and pos and use that to update the sprite position opos.get(eid, op); pos.get(eid, p); // wrap our interpolated position as we may interpolate off the screen sprite.get(eid).setTranslation(wrapx(MathUtil.lerp(op.x, p.x, alpha)), wrapy(MathUtil.lerp(op.y, p.y, alpha))); } }
/** * Returns the (shortest) distance between two angles, assuming that both angles are in * [-pi, +pi]. */ public static float angularDistance (float a1, float a2) { float ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2); return Math.min(Math.abs(a1 - a2), Math.abs(ma1 - ma2)); }
@Override public Canvas clipRect(float x, float y, float width, final float height) { final int cx = MathUtil.ifloor(x), cy = MathUtil.ifloor(y); final int cwidth = MathUtil.iceil(width), cheight = MathUtil.iceil(height); currentState().clipper = new JavaCanvasState.Clipper() { public void setClip(Graphics2D g2d) { g2d.setClip(cx, cy, cwidth, cheight); } }; return this; }
protected BufferedImage scaleImage(BufferedImage image, float viewImageRatio) { int swidth = MathUtil.iceil(viewImageRatio * image.getWidth()); int sheight = MathUtil.iceil(viewImageRatio * image.getHeight()); BufferedImage scaled = new BufferedImage(swidth, sheight, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D gfx = scaled.createGraphics(); gfx.drawImage(image.getScaledInstance(swidth, sheight, java.awt.Image.SCALE_SMOOTH), 0, 0, null); gfx.dispose(); return scaled; }