Page 1 of 1

Simple Rectangle Drawing Program Causes Major Issues

Posted: April 19th, 2017, 11:32 pm
by BugSquisher
Hi, I recently came back to the tutorial series and decided to make a small program through the framework in VS 2017 to get back into things. It simply draws a new rectangle every time I click the mouse. However, sometimes when I click the mouse, I lose the signal from my DVI port and have to reboot the computer to get back to normal. I don't think this is expected behavior from the framework, even if some variable is writing out of bounds. Could anyone chime in maybe if there have been similar issues? Thanks.

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 19th, 2017, 11:57 pm
by albinopapa
Never heard of the losing video signal accept for changing screen resolutions to one that isn't supported by your monitor.

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 20th, 2017, 2:31 pm
by chili
That is a super weird one that I've not heard before either.

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 20th, 2017, 6:06 pm
by BugSquisher
Okay guys, I uninstalled and reinstalled Visual Studio and my graphics drivers. Problem appears fixed. I can only assume it was some corrupted driver or file somewhere.

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 20th, 2017, 8:22 pm
by albinopapa
Well, hopefully that was the issue. Welcome to the forums btw.

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 22nd, 2017, 3:40 am
by krautersuppe
BugSquisher wrote: It simply draws a new rectangle every time I click the mouse. .
Yes I also thought that drawing a rectangle is a simple matter. In my case it didn't cause issues for my computer but it did cause issues for my head :lol:

I tried to make a function to draw a rectangle of given height and width at given angle.
I used chili's DrawLine function (old series tutorial 10):

Code: Select all

void Graphics::DrawLine(int x1, int y1, int x2, int y2, int r, int g, int bl)//  for elaborate explanation see old beginner series lesson 10
{
	int dx = x2 - x1;
	int dy = y2 - y1;

	if (abs(dy)>abs(dx)) { // calculates the x values relative to y - its an mirror function of the y = f(x)
		if (y1>y2) {
			int temp = y2;//swaps the values of y1 and y2 so that drawing loop gets executed for this condition
			y2 = y1;
			y1 = temp;
			temp = x2;//swaps the values of x2 and x1
			x2 = x1;
			x1 = temp;
		}

		float m = (float)dx / (float)dy; // calculates the steepness of the line
		float b = x1 - m*y1;					// calculates the y-intercept
		for (int y = y1; y <= y2; y++) {
			int x = m*y + b+ 0.5f; // 0.5f corrects the rounding effect of compiler(see video)
			PutPixel(x, y, r, g, bl);
		}
	}
	else {
		if (x1>x2) {
			int temp = y2;//swaps the values of y1 and y2 so that drawing loop gets executed for this condition(see video)
			y2 = y1;
			y1 = temp;
			temp = x2;//swaps the values of x2 and x1
			x2 = x1;
			x1 = temp;
		}

		float m = (float)dy / (float)dx; // calculates the steepness of the line
		float b = y1 - m*x1;					// calculates the y-intercept
		for (int x = x1; x <= x2; x++) {
			int y = m*x + b + 0.5f;
			PutPixel(x, y, r, g, bl);
		}
	}
}
but i was unable to draw a rectangle with single
loop - some holes and gaps get drawn because of breaking lines :x
. I use the second loop to fill them out:

Code: Select all

void Graphics::DrawRectangle(int cx, int cy, int height, int width, int angle, int r, int g, int b)
{
	const float PI = 3.14159;
	const float ra = angle* PI / 180.0;//angle in radians
	const int hw = width / 2; //half of width
	const int hh = height / 2; //half of height


			const int Ax = cx - hw*cos(ra) - hh*sin(ra);
			const int Ay = cy - hw*sin(ra) + hh*cos(ra);

			const int Bx = cx + hw*cos(ra) - hh*sin(ra);
			const int By = cy + hw*sin(ra) + hh*cos(ra);


			const int Cx = cx - hw*cos(ra) + hh*sin(ra);
			const int Cy = cy - hw*sin(ra) - hh*cos(ra);
			
			float y1 = Ay, x2 = Cx, y2 = Cy, x1=Ax;
			for (  x1 = x1; x1 <= Bx; x1 = x1+ cos(ra)) {
				DrawLine(x1, y1, x2, y2, r, g, b);
				x2 = x2 + cos(ra);
				y1 = y1 + sin(ra);
				y2 = y2 + sin(ra);
			}

			x1 = Ax, x2 = Bx, y2 = By;
			for ( y1 = Ay; y1 >= Cy; y1 = y1 - cos(ra)) { // this loop shouldn't be necessary - it's just meant to fill out holes
				DrawLine(x1, y1, x2, y2, r, g, b);
				x1= x1 + sin(ra);
				x2 = x2 + sin(ra);
				y2 = y2 - cos(ra);
			}
I know that there are built-in functions for rotation and i try to reinvent the wheel here but maybe
someone here finds this interesting and has some tips.
Besides that it doesn't function properly if height>width or if angle>90 degrees because fucking sine is odd and cosine even.
rect1.png
rect1.png (4.93 KiB) Viewed 2563 times

Re: Simple Rectangle Drawing Program Causes Major Issues

Posted: April 22nd, 2017, 11:55 pm
by albinopapa
If you want to draw rects that rotate, check out chili's old Advanced tuts or his new 3D fundamentals tuts. He covers that very topic.