fast 2dimensional histograming in matlab

Here is my version for a 2D histogram: %# some random data X = randn(2500,1); Y = randn(2500,1)*2; %# bin centers (integers) xbins = floor(min(X)):1:ceil(max(X)); ybins = floor(min(Y)):1:ceil(max(Y)); xNumBins = numel(xbins); yNumBins = numel(ybins); %# map X/Y values to bin indices Xi = round( interp1(xbins, 1:xNumBins, X, ‘linear’, ‘extrap’) ); Yi = round( interp1(ybins, 1:yNumBins, … Read more

Closest point to a given point

Don’t forget that you don’t need to bother with the square root. If you just want to find the nearest one (and not it’s actual distance) just use dx^2 + dy^2, which will give you the distance squared to the each item, which is just as useful. If you have no data structure wrapping this … Read more

Android: looking for a drawArc() method with inner & outer radius

You can do this: Paint paint = new Paint(); final RectF rect = new RectF(); //Example values rect.set(mWidth/2- mRadius, mHeight/2 – mRadius, mWidth/2 + mRadius, mHeight/2 + mRadius); paint.setColor(Color.GREEN); paint.setStrokeWidth(20); paint.setAntiAlias(true); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStyle(Paint.Style.STROKE); canvas.drawArc(rect, -90, 360, false, paint); The key is in paint.setStyle(Paint.Style.STROKE);, it crops the arc’s center with the stroke that you define in … Read more

Changing the Coordinate System in LibGDX (Java)

If you use a Camera (which you should) changing the coordinate system is pretty simple: camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); If you use TextureRegions and/or a TextureAtlas, all you need to do in addition to that is call region.flip(false, true). The reasons we use y-up by default (which you can easily change as … Read more

Java 2d rotation in direction mouse point

Using the Graphics2D rotation method is indeed the easiest way. Here’s a simple implementation: int centerX = width / 2; int centerY = height / 2; double angle = Math.atan2(centerY – mouseY, centerX – mouseX) – Math.PI / 2; ((Graphics2D)g).rotate(angle, centerX, centerY); g.fillRect(…); // draw your rectangle If you want to remove the rotation when … Read more

Swift 3 2d array of Int

It’s not simple really. The line: var arr : [[Int]] = [] Creates a variable of type Array of Array of Int and initially the array is empty. You need to populate this like any other other array in Swift. Let’s step back to a single array: var row : [Int] = [] You now … Read more