# Unable to center a rectangle

**URL:** <https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019>\
**Category:** Uncategorized\
**Created:** [July 6, 2022, 1:05pm UTC](https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019 "2022-07-06T13:05:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Archspect](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/archspect/32/991_2.png) [@Archspect](https://discourse.libcinder.org/u/Archspect)\
**Post date:** [July 6, 2022, 1:05pm UTC](https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019/1 "2022-07-06T13:05:29Z")

</div>

So I’ve been playing around with this library and I came across a noob-ish issue when it comes to graphics programming. what I wanted was to be able to center a rectangle of specified width and height regardless of the window size. I tried different ways of doing it, and I asked a bunch more less mathematically-illiterate people and I still cannot get it to be in the center.

```auto
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include <cinder/gl/draw.h>
#include <cinder/gl/wrapper.h>

using namespace ci;
using namespace ci::app;

class BasicApp : public App {
  public:

	void setup() override;
	void update() override;
	void draw() override;

	float Cx,Cy;
	float width,height;
	float TopLeftX, TopLeftY;
	float BottomRightX, BottomRightY;
	int screenWidth, screenHeight;
};

void BasicApp::setup()
{
	width = 50.0f;
	height = 50.0f;

	screenWidth = getWindowWidth();
	screenHeight = getWindowHeight();

	Cx = width / 2 + TopLeftX;
	Cy = height / 2 + TopLeftY;

	TopLeftX = screenWidth - width/2;
	BottomRightX = screenWidth + width/2;

	TopLeftY = screenHeight - height/2;
	BottomRightY = screenHeight + height/2;
}

void prepareSettings( BasicApp::Settings* settings ) {}
void BasicApp::update() {}

void BasicApp::draw()
{
	gl::drawStrokedRect(Rectf(TopLeftX,TopLeftY,BottomRightX,BottomRightY), 7.0);
}

CINDER_APP( BasicApp, RendererGl, prepareSettings )

```

Though when I compile and run the app, it just looks like this

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/libcinder/original/1X/79d153a6cd944fd122889b1998fc719a259a9697.png)

I am on arch linux if that somehow matters.

---

<div class="post-metadata">

**Author:** ![lithium](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/lithium/32/38_2.png) [@lithium](https://discourse.libcinder.org/u/lithium)\
**Post date:** [July 6, 2022, 1:16pm UTC](https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019/2 "2022-07-06T13:16:57Z")

</div>

```cpp
void BasicApp::draw()
{
	gl::setMatricesWindow ( getWindowSize() );

	vec2 rectSize { 128.0f, 64.0f };
	vec2 center = app::getWindowCenter ( );

	gl::drawStrokedRect ( Rectf ( center - rectSize / 2.0f, center + rectSize / 2.0f ), 7.0f );
}

```

---

<div class="post-metadata">

**Author:** ![Archspect](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/archspect/32/991_2.png) [@Archspect](https://discourse.libcinder.org/u/Archspect)\
**Post date:** [July 6, 2022, 1:22pm UTC](https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019/3 "2022-07-06T13:22:03Z")

</div>

😮  
Thank you so much! 😀

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [July 15, 2022, 9:46am UTC](https://discourse.libcinder.org/t/unable-to-center-a-rectangle/2019/4 "2022-07-15T09:46:04Z")

</div>

Additional note: after the window has been created, Cinder will call `setup()` first and `resize()` next (if your application provides an override of this function). It will also call `resize()` every time the window size changes. If you only call `getWindowWidth()` in your `setup()`, you will not be notified of this.

So, either add a `resize()` function to update `screenWidth` and `screenHeight`, or just call `getWindowWidth()` in your `draw()` method.

~Paul
