Hi im new to C++ and im trying to call a function in to a for loop.
What im trying to do is take the DrawLine function (that I put in bold) and have it referenced right before the for loop in the DrawStuff (that i put in bold) This way the DrawStuff will know to use DrawLine to draw more points in a line.
Thanks in advance!
Code:
[B]void DrawLine[/B](int x1, int y1, int x2, int y2, unsigned int color)
{
int dx, dy; // dy / dx is the slope
int x, y; // loop and point variables
// calculate changes in y and x between the points
dy = y2 - y1;
dx = x2 - x1;
if (Abs(dy) > Abs(dx)) {
// since there is a greater change in y than x we must
// loop in y, calculate x and draw
for (y=y1; y != y2; y += Sign(dy)) {
x = x1 + (y - y1) * dx / dy;
PGraphics->AddPoint(x, y, color);
}
}
else {
// since there is a greater (or equal) change in x than y we must
// loop in x, calculate y and draw
for (x=x1; x != x2; x += Sign(dx)) {
y = y1 + (x - x1) * dy / dx;
PGraphics->AddPoint(x, y, color);
}
}
// draw the last pixel
PGraphics->AddPoint(x2, y2, color);
// draw the points
PGraphics->Draw();
}
void DrawStuff() {
COLORREF green = RGB(0, 255, 0); // green color to draw with
COLORREF purple = RGB(255, 0, 255); // purple color to draw with
char str[32]; // string to store user input
int h, k; // parabola vertex
double a; // parabola constant - might be a decimal
int x, y; // loop and point variables
int xPrev, yPrev; // previous point for drawng line segments
int ymin, ymax; // limits for y loop
RECT rect; // rectangle for the output window
// get the user input from the edit boxes and
// convert string input to integer
GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
h = atoi(str);
GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
k = atoi(str);
GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
a = atof(str); // use atof to allow user to enter a decimal
// get the rect for this window
GetClientRect(HOutput, &rect);
// use the rectangle info to set up y loop limits
ymin = -(rect.bottom - rect.top) / 2;
ymax = (rect.bottom - rect.top) / 2;
// clear the scene and add an axis
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->AddAxis(RGB(150, 150, 150), 10);
yPrev = ymin;
xPrev = (int)( a * (yPrev-k) * (yPrev-k) ) + h;
// loop in y, calculate x and draw
[B] for (y = ymin; y <= ymax; y++) {
x = (int)( a * (y-k) * (y-k) ) + h;
PGraphics->AddPoint(x, y, green); [/B]
}
// draw the points
PGraphics->Draw();
}
Thanks in advance!
Comment