Planet Chili
http://forum.planetchili.net/

Simple Rectangle Drawing Program Causes Major Issues
http://forum.planetchili.net/viewtopic.php?f=3&t=4003
Page 1 of 1

Author:  BugSquisher [ April 19th, 2017, 11:32 pm ]
Post subject:  Simple Rectangle Drawing Program Causes Major Issues

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.

Author:  albinopapa [ April 19th, 2017, 11:57 pm ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

Never heard of the losing video signal accept for changing screen resolutions to one that isn't supported by your monitor.

Author:  chili [ April 20th, 2017, 2:31 pm ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

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

Author:  BugSquisher [ April 20th, 2017, 6:06 pm ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

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.

Author:  albinopapa [ April 20th, 2017, 8:22 pm ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

Well, hopefully that was the issue. Welcome to the forums btw.

Author:  krautersuppe [ April 22nd, 2017, 3:40 am ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

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:
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:
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.

Attachment:
rect1.png
rect1.png [ 4.93 KiB | Viewed 141 times ]

Author:  albinopapa [ April 22nd, 2017, 11:55 pm ]
Post subject:  Re: Simple Rectangle Drawing Program Causes Major Issues

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/