Find Center of Rectangle [closed]

泄露秘密 提交于 2019-12-13 11:30:47

问题


How do you find the center coordinates of a Rectangle drawn in the canvas? The only items I have is the left, right, bottom, and top values.

Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);

回答1:


Just use Rect methods centerX() and centerY()

Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);
int xCenter = newRect.centerX();
int yCenter = newRect.centerY();

Or if you need a float value for better precision, use exactCenterX() and exactCenterY()

float xCenter = newRect.exactCenterX();
float yCenter = newRect.exactCenterY();



回答2:


center = 0.5(left+right), 0.5(bottom+top)



来源:https://stackoverflow.com/questions/20573124/find-center-of-rectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!