Home > Enterprise >  How to create a circle outline in a float array using just one xy pair of for loops
How to create a circle outline in a float array using just one xy pair of for loops

Time:11-18

Problem

I need to create a circle outline using a float array.

The function CircleOutline(int res, int r1, int r2) accomplishes what I want. It creates a float array with a unfilled circle(circle outline) of 1's. All others are 0.

However, I need to do it with just a single set of xy for loops. This section if ((c - x) * (c - x) (c - y) * (c - y) < r1_2) fills everything inside the circumference. I think, but am not sure, I need the inverse of this (filling outside).

Concept

This is the psuedo code for what I think would work:

if (current index is inside circumferenceA) 
   if (current index is outside circumferenceB)
        map.add(1);

CIRCLE OUTLINE FLOAT ARRAY

static float [] CircleOutline(int res, int r1, int r2)
{
    float [] map = new float[res * res];
    float c = res / 2;
    float r1_2 = r1 * r1;
    for (int x = 0; x < res; x  ) 
    {
        for (int y = 0; y < res; y  ) 
        {
            if ((c - x) * (c - x)   (c - y) * (c - y) < r1_2)
            {
                map[x   y * res] = 1f;                    
            }
        }
    }
    float r2_2 = r2 * r2;
    for (int x = 0; x < res; x  ) 
    {
        for (int y = 0; y < res; y  ) 
        {
            if ((c - x) * (c - x)   (c - y) * (c - y) < r2_2)
            {
                map[x   y * res] = 0f;                      
            }
        }
    }
    
    return map;
}

In advance for your time and wisdom, Thank you.

CodePudding user response:

Because there is no answer specifically for outlines using a float array and because I just figured it out, I will post my answer:

float [] map = new float[res * res];
float c = res / 2;
float r1_2 = r1 * r1;
for (int x = 0; x < res; x  ) 
{
    for (int y = 0; y < res; y  ) 
    {
        if ((c - x) * (c - x)   (c - y) * (c - y) < r1_2)
        {
            if ((c - x) * (c - x)   (c - y) * (c - y) > r2_2)
            {
                map[x   y * res] = 1f; 
            }           
        }
    }
}

I figured it out after I wrote my pseduo code.

  • Related