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

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

. 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 [ 4.93 KiB | Viewed 259 times ]