How can I get points from a two dimensional array in a circular pattern?

Issue

I’m just playing with Android Studio bitmaps and have created a dotted background for my application through iteration.

constant = 60;

int padding_X = (int) Math.floor((width % constant)/2f);
if (padding_X == 0) {
    padding_X = (int) Math.floor(constant / 2);
}
int padding_Y = (int) Math.floor((height % constant)/2f);
if (padding_Y == 0) {
    padding_Y = (int) Math.floor(constant/2);
}

System.out.println("padding X: "+padding_X);
System.out.println("padding Y: "+padding_Y);

int max_xn = Math.round((width-(padding_X*2)) / constant);
int max_yn = Math.round((height-(padding_Y*2)) / constant);

System.out.println("max xn: "+max_xn);
System.out.println("max yn: "+max_yn);

point_matrix = new int[max_xn+1][max_yn+1][2];
lens = new int[2];

for (int yn = 0; yn <= max_yn; yn++) {
    int y = (int) (padding_Y + (yn*constant));
    for (int xn = 0; xn <= max_xn; xn++) {
        int x = (int) (padding_X + (xn*constant));

        System.out.println("point @ x: "+x+" y: "+y);
        canvas.setPixel(x,y,Color.parseColor("#ffffff"));

        point_matrix[xn][yn][0] = x;
        point_matrix[xn][yn][1] = y;
    }
}

runOnUiThread(() -> {
    iv0.setImageBitmap(canvas);
});

lens[0] = max_xn+1;
lens[1] = max_yn+1;

I have also added each white pixel to a 3 dimensional array int[][][]. The array holds xn and yn for indexing the dots. Last array holds the coordinates onscreen. Example: {5, 1, {100,250}} 5 is the dots index on x axis, 1 is the index on y axis and 100 and 250 are coordinates on the bitmap.

I’m hoping to find a way for finding all dots on the 3 dim. array on a certain radius from the center.

A plan I had was iterating through all elements in the 3dim array and calculating the distance to the center with pythagoras theorem or something like that but that would be really inefficient seeing as this would have to be done multiple times.

The final plan is to have all of the dots to dissapear in a circular motion starting from the center. With a delay between each "radius interval".

Solution

Use trigonometric functions 🙂

static double i = 0;
static double pi = Math.PI;
static int q = 5;     // half size of array
static double x;
static double y;
static double cx = 5; // offset center x 
static double cy = 5; // offset center y 
public static void main(String[] args) {
    while (i < pi * 2) {    // pi*2 is full angle of circle
        x = Math.round (cx + Math.sin(i) * q);
        y = Math.round (cy + Math.cos(i) * q);
        System.out.print(String.format("X = %4f", x) + String.format("Y = %4f", y) + "\n");
        i+=pi/180;
    }
}

Answered By – Michael Nicholson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published