#alternative to this?

1 messages · Page 1 of 1 (latest)

fiery plover
#
int* get_center_coordinates(int window_width, int window_height, int image_width, int image_height) {
  static int pos[2];
  pos[0] = (int)(window_width / 2 - image_width / 2);
  pos[1] = (int)(window_height / 2 - image_height / 2);
  return pos;
}

I know I can use the & operator and make this a void instead of a int*, but I don't know how to approach it.

cinder trellis
#

if you want a function parameter to be populated with the result, you're right that you have to pass it in by reference using the & (if you're using C++). if you intend to stick with C, then you'll approach it in a similar way, just passing a pointer into the function instead.

instead of using a c array stuff like this tends to be much more readable with a simple struct that actually describes the data it represents so I tossed the Position struct in there as an example of that...

struct Position
{
    int x{ 0 };
    int y{ 0 };
};

// cpp-ish apprach that uses references instead...
void get_center_coordinates_cpp(const int window_width, const int window_height,
                                const int image_width, const int image_height, Position& pos)
{
    pos.x = (int)(window_width / 2 - image_width / 2);
    pos.y = (int)(window_height / 2 - image_height / 2);
}

// c-style(ish) that takes in a pointer...
void get_center_coordinates_c_style(const int window_width, const int window_height,
                                    const int image_width, const int image_height, Position* pos)
{
    pos->x = (int)(window_width / 2 - image_width / 2);
    pos->y = (int)(window_height / 2 - image_height / 2);
}

int main()
{
    Position pos1{};
    get_center_coordinates_c_style(100, 100, 10, 10, &pos1);

    Position pos2{};
    get_center_coordinates_cpp(100, 100, 10, 10, pos2);
}
#

if this is a C++ program you're working on, you could also approach this type of thing using custom operators as just one many different examples for this type of thing which helps readability a bit more and makes things easier to debug if you end up doing a lot of these types of operations on common primitives/geometries (points, lines, rect, circle, colors, etc),

struct Position
{
    int x{ 0 };
    int y{ 0 };
};

struct Dimensions
{
    int width{ 0 };
    int height{ 0 };

    constexpr Dimensions(int width, int height)
        : width{ std::forward<int>(width) }
        , height{ std::forward<int>(height) }
    {
    }

    constexpr Dimensions(const Dimensions& other)
        : width{ other.width }
        , height{ other.height }
    {
    }

    constexpr Dimensions operator-(Dimensions other) const
    {
        return Dimensions{
            width - other.width,
            height - other.height,
        };
    }

    constexpr Position operator/(int div) const
    {
        return Position{
            width / div,
            height / div,
        };
    }
};

int main()
{
    constexpr Dimensions image_size{ 10, 10 };
    constexpr Dimensions window_size{ 100, 100 };
    
    // with this approach you can subtract the dimensions directly, 
    // as well as dividng Dimensions by integers to end up with a Position...
    constexpr Position center{ (window_size - image_size) / 2 };
}