C++ 将对象内的文本居中,并根据对象的宽度和高度调整大小
我有一个矩形对象,显示的宽度为50,高度为100 我有一个文本标签,我想在屏幕上的矩形内居中并调整其大小 根据文本大小,文本范围从0-100+到矩形大小 下面是我正在做的:C++ 将对象内的文本居中,并根据对象的宽度和高度调整大小,c++,math,C++,Math,我有一个矩形对象,显示的宽度为50,高度为100 我有一个文本标签,我想在屏幕上的矩形内居中并调整其大小 根据文本大小,文本范围从0-100+到矩形大小 下面是我正在做的: int rectangle_width = 50; int rectangle_height = 100; // i'm calculating the center of the rectangle text_x_positon = rectangle_width/2; text_y_positon = rec
int rectangle_width = 50;
int rectangle_height = 100;
// i'm calculating the center of the rectangle
text_x_positon = rectangle_width/2;
text_y_positon = rectangle_height/2;
// calculating the size of the text label according to rectangle free space
int size = (rectangle_width/rectangle_height)*10;
label(text_x_positon, text_y_positon, size);
问题是,有时文本比矩形小或大,请帮助我解决这个问题,并告诉我正确的方法。尝试使用text\u x\u position=rectangle\u width/2-text\u width/2完成此操作的坦克