Simple Rectangle Drawing Program Causes Major Issues

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
BugSquisher
Posts: 5
Joined: April 19th, 2017, 11:27 pm

Simple Rectangle Drawing Program Causes Major Issues

Post by BugSquisher » April 19th, 2017, 11:32 pm

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.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by albinopapa » April 19th, 2017, 11:57 pm

Never heard of the losing video signal accept for changing screen resolutions to one that isn't supported by your monitor.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by chili » April 20th, 2017, 2:31 pm

That is a super weird one that I've not heard before either.
Chili

BugSquisher
Posts: 5
Joined: April 19th, 2017, 11:27 pm

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by BugSquisher » April 20th, 2017, 6:06 pm

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.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by albinopapa » April 20th, 2017, 8:22 pm

Well, hopefully that was the issue. Welcome to the forums btw.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
krautersuppe
Posts: 91
Joined: September 14th, 2015, 10:58 pm
Location: Istanbul

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by krautersuppe » April 22nd, 2017, 3:40 am

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 2582 times
DSU
Discord: dsu1, GitHub: https://github.com/DSpUz

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Simple Rectangle Drawing Program Causes Major Issues

Post by albinopapa » April 22nd, 2017, 11:55 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply